Skip to content

Commit dc2819c

Browse files
authored
Merge pull request #9 from MaleicAcid/master
add CV\getStructuringElement and its phpt file
2 parents 9ef5df2 + b2ac4bf commit dc2819c

File tree

6 files changed

+122
-2
lines changed

6 files changed

+122
-2
lines changed

opencv.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,6 +222,7 @@ const zend_function_entry opencv_functions[] = {
222222
ZEND_NS_NAMED_FE(OPENCV_NS, bilateralFilter, ZEND_FN(opencv_bilateral_filter), opencv_bilateral_filter_arginfo)
223223
ZEND_NS_NAMED_FE(OPENCV_NS, dilate, ZEND_FN(opencv_dilate), opencv_dilate_arginfo)
224224
ZEND_NS_NAMED_FE(OPENCV_NS, erode, ZEND_FN(opencv_erode), opencv_erode_arginfo)
225+
ZEND_NS_NAMED_FE(OPENCV_NS, getStructuringElement, ZEND_FN(opencv_get_structuring_element), opencv_get_structuring_element_arginfo)
225226
ZEND_NS_NAMED_FE(OPENCV_NS, threshold, ZEND_FN(opencv_threshold), opencv_threshold_arginfo)
226227
PHP_FE_END /* Must be the last line in opencv_functions[] */
227228
};

source/opencv2/core/opencv_mat.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,24 @@ PHP_METHOD(opencv_mat, empty)
128128
RETURN_LONG(obj->mat->empty());
129129
}
130130

131+
PHP_METHOD(opencv_mat, ones)
132+
{
133+
long rows, cols, flags;
134+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "lll", &rows, &cols, &flags) == FAILURE) {
135+
RETURN_NULL();
136+
}
137+
zval instance;
138+
object_init_ex(&instance, opencv_mat_ce);
139+
opencv_mat_object *obj = Z_PHP_MAT_OBJ_P(&instance);
140+
141+
Mat im = Mat::ones((int)rows, (int)cols, (int)flags);
142+
143+
obj->mat=new Mat(im);
144+
//update php Mat object property
145+
opencv_mat_update_property_by_c_mat(&instance, obj->mat);
146+
147+
RETURN_ZVAL(&instance,0,0); //return php Mat object
148+
}
131149

132150
PHP_METHOD(opencv_mat, zeros)
133151
{
@@ -445,6 +463,7 @@ const zend_function_entry opencv_mat_methods[] = {
445463
PHP_ME(opencv_mat, print, NULL, ZEND_ACC_PUBLIC)
446464
PHP_ME(opencv_mat, size, NULL, ZEND_ACC_PUBLIC)
447465
PHP_ME(opencv_mat, clone, NULL, ZEND_ACC_PUBLIC)
466+
PHP_ME(opencv_mat, ones, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
448467
PHP_ME(opencv_mat, zeros, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
449468
PHP_MALIAS(opencv_mat, zerosBySize ,zeros_by_size, NULL, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
450469
PHP_MALIAS(opencv_mat, isContinuous ,is_continuous, NULL, ZEND_ACC_PUBLIC)

source/opencv2/opencv_imgproc.cc

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ PHP_FUNCTION(opencv_cv_t_color){
5656
RETURN_ZVAL(&instance,0,0); //return php Mat object
5757
}
5858

59+
5960
/**
6061
* CV\ellipse
6162
* @param CV\Mat $img, Mat of original picture
@@ -676,6 +677,42 @@ PHP_FUNCTION(opencv_threshold){
676677
RETURN_DOUBLE(threshold(*src_object->mat, *dst_object->mat, thresh, maxval, (int)type));
677678
}
678679

680+
/**
681+
* CV\getStructuringElement
682+
* @param execute_data
683+
* @param return_value
684+
*/
685+
PHP_FUNCTION(opencv_get_structuring_element){
686+
long shape;
687+
zval *ksize_zval, *anchor_zval = NULL;
688+
Point anchor = Point(-1,-1);
689+
opencv_mat_object *dst_object;
690+
Mat dst;
691+
if (zend_parse_parameters(ZEND_NUM_ARGS(), "lO|O",
692+
&shape,
693+
&ksize_zval, opencv_size_ce,
694+
&anchor_zval, opencv_point_ce) == FAILURE) {
695+
RETURN_NULL();
696+
}
697+
698+
opencv_size_object *ksize_object = Z_PHP_SIZE_OBJ_P(ksize_zval);
699+
if(anchor_zval != NULL){
700+
opencv_point_object *anchor_object = Z_PHP_POINT_OBJ_P(anchor_zval);
701+
anchor = *anchor_object->point;
702+
}
703+
704+
dst = getStructuringElement((int)shape, *ksize_object->size, anchor);
705+
706+
zval instance;
707+
object_init_ex(&instance,opencv_mat_ce);
708+
opencv_mat_object *dst_obj = Z_PHP_MAT_OBJ_P(&instance);
709+
710+
dst_obj->mat=new Mat(dst);
711+
opencv_mat_update_property_by_c_mat(&instance,dst_obj->mat);
712+
713+
RETURN_ZVAL(&instance,0,0); //return php Mat object
714+
}
715+
679716
/**
680717
* color conversion code in CV\cvtColor,opencv enum ColorConversionCodes
681718
* @param module_number
@@ -940,4 +977,4 @@ void opencv_line_type_init(int module_number){
940977
REGISTER_NS_LONG_CONSTANT(OPENCV_NS, "LINE_4", LINE_4, CONST_CS | CONST_PERSISTENT);
941978
REGISTER_NS_LONG_CONSTANT(OPENCV_NS, "LINE_8", LINE_8, CONST_CS | CONST_PERSISTENT);
942979
REGISTER_NS_LONG_CONSTANT(OPENCV_NS, "LINE_AA", LINE_AA, CONST_CS | CONST_PERSISTENT);
943-
}
980+
}

source/opencv2/opencv_imgproc.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,13 @@ ZEND_BEGIN_ARG_INFO_EX(opencv_erode_arginfo, 0, 0, 7)
111111
ZEND_END_ARG_INFO()
112112
PHP_FUNCTION(opencv_erode);
113113

114+
ZEND_BEGIN_ARG_INFO_EX(opencv_get_structuring_element_arginfo, 0, 0, 3)
115+
ZEND_ARG_INFO(0, shape)
116+
ZEND_ARG_INFO(0, ksize)
117+
ZEND_ARG_INFO(0, anchor)
118+
ZEND_END_ARG_INFO()
119+
PHP_FUNCTION(opencv_get_structuring_element);
120+
114121
ZEND_BEGIN_ARG_INFO_EX(opencv_threshold_arginfo, 0, 0, 5)
115122
ZEND_ARG_INFO(0, src)
116123
ZEND_ARG_INFO(1, dst)

tests/get_structuring_element.phpt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
--TEST--
2+
get_structuring_element function test
3+
--SKIPIF--
4+
<?php if (!extension_loaded("opencv")) print "skip"; ?>
5+
--FILE--
6+
<?php
7+
namespace CV;
8+
9+
$morph_elem = 2;
10+
$morph_size = 3;
11+
$element = getStructuringElement($morph_elem, new Size(2*$morph_size + 1, 2*$morph_size+1), new Point($morph_size, $morph_size));
12+
13+
var_dump($element);
14+
$element->print();
15+
16+
--EXPECT--
17+
object(CV\Mat)#3 (4) {
18+
["type":"CV\Mat":private]=>
19+
int(0)
20+
["rows"]=>
21+
int(7)
22+
["cols"]=>
23+
int(7)
24+
["dims"]=>
25+
int(2)
26+
}
27+
[ 0, 0, 0, 1, 0, 0, 0;
28+
0, 1, 1, 1, 1, 1, 0;
29+
1, 1, 1, 1, 1, 1, 1;
30+
1, 1, 1, 1, 1, 1, 1;
31+
1, 1, 1, 1, 1, 1, 1;
32+
0, 1, 1, 1, 1, 1, 0;
33+
0, 0, 0, 1, 0, 0, 0]

tests/mat.phpt

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ $mat->print(Formatter::FMT_PYTHON);
1515
$zeros = Mat::zeros(10,10,CV_8UC1);
1616
var_dump($zeros);
1717
$zeros->print(Formatter::FMT_PYTHON);
18+
$ones = Mat::ones(10,10,CV_8UC1);
19+
var_dump($ones);
20+
$ones->print(Formatter::FMT_PYTHON);
1821
?>
1922
--EXPECT--
2023
object(CV\Mat)#2 (4) {
@@ -51,4 +54,24 @@ object(CV\Mat)#3 (4) {
5154
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
5255
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
5356
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
54-
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
57+
[ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
58+
object(CV\Mat)#4 (4) {
59+
["type":"CV\Mat":private]=>
60+
int(0)
61+
["rows"]=>
62+
int(10)
63+
["cols"]=>
64+
int(10)
65+
["dims"]=>
66+
int(2)
67+
}
68+
[[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
69+
[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
70+
[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
71+
[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
72+
[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
73+
[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
74+
[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
75+
[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
76+
[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
77+
[ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]

0 commit comments

Comments
 (0)