Skip to content
1 change: 1 addition & 0 deletions opencv.cc
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,7 @@ const zend_function_entry opencv_functions[] = {
ZEND_NS_NAMED_FE(OPENCV_NS, bilateralFilter, ZEND_FN(opencv_bilateral_filter), opencv_bilateral_filter_arginfo)
ZEND_NS_NAMED_FE(OPENCV_NS, dilate, ZEND_FN(opencv_dilate), opencv_dilate_arginfo)
ZEND_NS_NAMED_FE(OPENCV_NS, erode, ZEND_FN(opencv_erode), opencv_erode_arginfo)
ZEND_NS_NAMED_FE(OPENCV_NS, getStructuringElement, ZEND_FN(opencv_get_structuring_element), opencv_get_structuring_element_arginfo)
ZEND_NS_NAMED_FE(OPENCV_NS, threshold, ZEND_FN(opencv_threshold), opencv_threshold_arginfo)
PHP_FE_END /* Must be the last line in opencv_functions[] */
};
Expand Down
19 changes: 19 additions & 0 deletions source/opencv2/core/opencv_mat.cc
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,24 @@ PHP_METHOD(opencv_mat, empty)
RETURN_LONG(obj->mat->empty());
}

PHP_METHOD(opencv_mat, ones)
{
long rows, cols, flags;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "lll", &rows, &cols, &flags) == FAILURE) {
RETURN_NULL();
}
zval instance;
object_init_ex(&instance, opencv_mat_ce);
opencv_mat_object *obj = Z_PHP_MAT_OBJ_P(&instance);

Mat im = Mat::ones((int)rows, (int)cols, (int)flags);

obj->mat=new Mat(im);
//update php Mat object property
opencv_mat_update_property_by_c_mat(&instance, obj->mat);

RETURN_ZVAL(&instance,0,0); //return php Mat object
}

PHP_METHOD(opencv_mat, zeros)
{
Expand Down Expand Up @@ -445,6 +463,7 @@ const zend_function_entry opencv_mat_methods[] = {
PHP_ME(opencv_mat, print, NULL, ZEND_ACC_PUBLIC)
PHP_ME(opencv_mat, size, NULL, ZEND_ACC_PUBLIC)
PHP_ME(opencv_mat, clone, NULL, ZEND_ACC_PUBLIC)
PHP_ME(opencv_mat, ones, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_ME(opencv_mat, zeros, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_MALIAS(opencv_mat, zerosBySize ,zeros_by_size, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
PHP_MALIAS(opencv_mat, isContinuous ,is_continuous, NULL, ZEND_ACC_PUBLIC)
Expand Down
39 changes: 38 additions & 1 deletion source/opencv2/opencv_imgproc.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ PHP_FUNCTION(opencv_cv_t_color){
RETURN_ZVAL(&instance,0,0); //return php Mat object
}


/**
* CV\ellipse
* @param CV\Mat $img, Mat of original picture
Expand Down Expand Up @@ -676,6 +677,42 @@ PHP_FUNCTION(opencv_threshold){
RETURN_DOUBLE(threshold(*src_object->mat, *dst_object->mat, thresh, maxval, (int)type));
}

/**
* CV\getStructuringElement
* @param execute_data
* @param return_value
*/
PHP_FUNCTION(opencv_get_structuring_element){
long shape;
zval *ksize_zval, *anchor_zval = NULL;
Point anchor = Point(-1,-1);
opencv_mat_object *dst_object;
Mat dst;
if (zend_parse_parameters(ZEND_NUM_ARGS(), "lO|O",
&shape,
&ksize_zval, opencv_size_ce,
&anchor_zval, opencv_point_ce) == FAILURE) {
RETURN_NULL();
}

opencv_size_object *ksize_object = Z_PHP_SIZE_OBJ_P(ksize_zval);
if(anchor_zval != NULL){
opencv_point_object *anchor_object = Z_PHP_POINT_OBJ_P(anchor_zval);
anchor = *anchor_object->point;
}

dst = getStructuringElement((int)shape, *ksize_object->size, anchor);

zval instance;
object_init_ex(&instance,opencv_mat_ce);
opencv_mat_object *dst_obj = Z_PHP_MAT_OBJ_P(&instance);

dst_obj->mat=new Mat(dst);
opencv_mat_update_property_by_c_mat(&instance,dst_obj->mat);

RETURN_ZVAL(&instance,0,0); //return php Mat object
}

/**
* color conversion code in CV\cvtColor,opencv enum ColorConversionCodes
* @param module_number
Expand Down Expand Up @@ -940,4 +977,4 @@ void opencv_line_type_init(int module_number){
REGISTER_NS_LONG_CONSTANT(OPENCV_NS, "LINE_4", LINE_4, CONST_CS | CONST_PERSISTENT);
REGISTER_NS_LONG_CONSTANT(OPENCV_NS, "LINE_8", LINE_8, CONST_CS | CONST_PERSISTENT);
REGISTER_NS_LONG_CONSTANT(OPENCV_NS, "LINE_AA", LINE_AA, CONST_CS | CONST_PERSISTENT);
}
}
7 changes: 7 additions & 0 deletions source/opencv2/opencv_imgproc.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,13 @@ ZEND_BEGIN_ARG_INFO_EX(opencv_erode_arginfo, 0, 0, 7)
ZEND_END_ARG_INFO()
PHP_FUNCTION(opencv_erode);

ZEND_BEGIN_ARG_INFO_EX(opencv_get_structuring_element_arginfo, 0, 0, 3)
ZEND_ARG_INFO(0, shape)
ZEND_ARG_INFO(0, ksize)
ZEND_ARG_INFO(0, anchor)
ZEND_END_ARG_INFO()
PHP_FUNCTION(opencv_get_structuring_element);

ZEND_BEGIN_ARG_INFO_EX(opencv_threshold_arginfo, 0, 0, 5)
ZEND_ARG_INFO(0, src)
ZEND_ARG_INFO(1, dst)
Expand Down
33 changes: 33 additions & 0 deletions tests/get_structuring_element.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--TEST--
get_structuring_element function test
--SKIPIF--
<?php if (!extension_loaded("opencv")) print "skip"; ?>
--FILE--
<?php
namespace CV;

$morph_elem = 2;
$morph_size = 3;
$element = getStructuringElement($morph_elem, new Size(2*$morph_size + 1, 2*$morph_size+1), new Point($morph_size, $morph_size));

var_dump($element);
$element->print();

--EXPECT--
object(CV\Mat)#3 (4) {
["type":"CV\Mat":private]=>
int(0)
["rows"]=>
int(7)
["cols"]=>
int(7)
["dims"]=>
int(2)
}
[ 0, 0, 0, 1, 0, 0, 0;
0, 1, 1, 1, 1, 1, 0;
1, 1, 1, 1, 1, 1, 1;
1, 1, 1, 1, 1, 1, 1;
1, 1, 1, 1, 1, 1, 1;
0, 1, 1, 1, 1, 1, 0;
0, 0, 0, 1, 0, 0, 0]
25 changes: 24 additions & 1 deletion tests/mat.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ $mat->print(Formatter::FMT_PYTHON);
$zeros = Mat::zeros(10,10,CV_8UC1);
var_dump($zeros);
$zeros->print(Formatter::FMT_PYTHON);
$ones = Mat::ones(10,10,CV_8UC1);
var_dump($ones);
$ones->print(Formatter::FMT_PYTHON);
?>
--EXPECT--
object(CV\Mat)#2 (4) {
Expand Down Expand Up @@ -51,4 +54,24 @@ object(CV\Mat)#3 (4) {
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
object(CV\Mat)#4 (4) {
["type":"CV\Mat":private]=>
int(0)
["rows"]=>
int(10)
["cols"]=>
int(10)
["dims"]=>
int(2)
}
[[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]