php-opencv / php-opencv

opencv 4.5+ with dnn module for php 7/8

Home Page:https://github.com/php-opencv/php-opencv-examples

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Try ....

BbenWeb1 opened this issue · comments

i try write code function matchTemplate .please correct any error . thanx
add in file opencv_imgproc.cc

void opencv_imgproc_init(int module_number)
{
    opencv_color_conversion_code_init(module_number);
    opencv_line_type_init(module_number);
    opencv_morph_shapes_init(module_number);
    opencv_morph_types_init(module_number);
    opencv_flood_fill_flags_init(module_number);
    opencv_threshold_types_init(module_number);
    opencv_adaptive_threshold_types_init(module_number);
    opencv_retrieval_modes_init(module_number);
    opencv_contour_approximation_modes_init(module_number);
    opencv_type_template_matching_operation_init(module_number);
}
PHP_FUNCTION(opencv_matchTemplate){

    zval *image_zval ,*result_zval, *templ_zval;
    long method;

    if (zend_parse_parameters(ZEND_NUM_ARGS(), "OOzl",
                                &image_zval, opencv_mat_ce,
                                &templ_zval, opencv_mat_ce,
                                &result_zval,
                                &method
    ) == FAILURE) {
        RETURN_NULL();
    }

    opencv_mat_object *image_obj , *templ_obj,*result_object;

    image_obj = Z_PHP_MAT_OBJ_P(image_zval);
    templ_obj = Z_PHP_MAT_OBJ_P(templ_zval);
    zval *result_real_zval = Z_REFVAL_P(result_zval);
    if(Z_TYPE_P(result_real_zval) == IS_OBJECT && Z_OBJCE_P(result_real_zval)==opencv_mat_ce){
        result_object = Z_PHP_MAT_OBJ_P(result_real_zval);
    } else{
        zval_ptr_dtor(result_real_zval);
        zval instance;
        Mat result;
        object_init_ex(&instance,opencv_mat_ce);
        ZVAL_COPY_VALUE(result_real_zval, &instance);
        result_object = Z_PHP_MAT_OBJ_P(result_real_zval);
        result_object->mat = new Mat(result);
    }
    try {
        matchTemplate(*image_obj->mat,*templ_obj->mat,*result_object->mat,(int)method);
        opencv_mat_update_property_by_c_mat(result_real_zval, result_object->mat);

    }catch (Exception e){
        opencv_throw_exception(e.what());
    }
    RETURN_NULL();

}
void opencv_type_template_matching_operation_init(int module_number){
    REGISTER_NS_LONG_CONSTANT(OPENCV_NS, "TM_SQDIFF", TM_SQDIFF, CONST_CS | CONST_PERSISTENT);
    REGISTER_NS_LONG_CONSTANT(OPENCV_NS, "TM_SQDIFF_NORMED", TM_SQDIFF_NORMED, CONST_CS | CONST_PERSISTENT);
    REGISTER_NS_LONG_CONSTANT(OPENCV_NS, "TM_CCORR", TM_CCORR, CONST_CS | CONST_PERSISTENT);
    REGISTER_NS_LONG_CONSTANT(OPENCV_NS, "TM_CCORR_NORMED", TM_CCORR_NORMED, CONST_CS | CONST_PERSISTENT);
    REGISTER_NS_LONG_CONSTANT(OPENCV_NS, "TM_CCOEFF", TM_CCOEFF, CONST_CS | CONST_PERSISTENT);
    REGISTER_NS_LONG_CONSTANT(OPENCV_NS, "TM_CCOEFF_NORMED", TM_CCOEFF_NORMED, CONST_CS | CONST_PERSISTENT);
}

add in opencv_imgproc.h

ZEND_BEGIN_ARG_INFO_EX(opencv_matchTemplate_arginfo, 0, 0, 4)
                ZEND_ARG_INFO(0, image)
                ZEND_ARG_INFO(0, templ)
                ZEND_ARG_INFO(1, result)
                ZEND_ARG_INFO(0, method)
ZEND_END_ARG_INFO()
PHP_FUNCTION(opencv_matchTemplate);
void opencv_imgproc_init(int module_number);
void opencv_color_conversion_code_init(int module_number);
void opencv_line_type_init(int module_number);
void opencv_morph_shapes_init(int module_number);
void opencv_morph_types_init(int module_number);
void opencv_flood_fill_flags_init(int module_number);
void opencv_threshold_types_init(int module_number);
void opencv_adaptive_threshold_types_init(int module_number);
void opencv_retrieval_modes_init(int module_number);
void opencv_contour_approximation_modes_init(int module_number);
void opencv_type_template_matching_operation_init(int module_number);

add in opencv.cc line 229
ZEND_NS_NAMED_FE(OPENCV_NS, matchTemplate, ZEND_FN(opencv_matchTemplate), opencv_matchTemplate_arginfo)

did you fix it?

Yes this is an example MatchTemplate-php-opencv