Skip to content

Commit 164ef13

Browse files
committed
Ensure morphology functions have all optional arguments
1 parent 8560bcf commit 164ef13

File tree

1 file changed

+62
-8
lines changed

1 file changed

+62
-8
lines changed

src/imgproc.cpp

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,15 @@ mp_obj_t cv2_imgproc_cvtColor(size_t n_args, const mp_obj_t *pos_args, mp_map_t
4343

4444
mp_obj_t cv2_imgproc_dilate(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
4545
// Define the arguments
46-
enum { ARG_src, ARG_kernel, ARG_dst };
46+
enum { ARG_src, ARG_kernel, ARG_dst, ARG_anchor, ARG_iterations, ARG_borderType, ARG_borderValue };
4747
static const mp_arg_t allowed_args[] = {
4848
{ MP_QSTR_src, MP_ARG_REQUIRED | MP_ARG_OBJ, { .u_obj = MP_OBJ_NULL } },
4949
{ MP_QSTR_kernel, MP_ARG_REQUIRED | MP_ARG_OBJ, { .u_obj = MP_OBJ_NULL } },
5050
{ MP_QSTR_dst, MP_ARG_OBJ, { .u_obj = mp_const_none } },
51+
{ MP_QSTR_anchor, MP_ARG_OBJ, { .u_obj = mp_const_none } },
52+
{ MP_QSTR_iterations, MP_ARG_INT, { .u_int = 1 } },
53+
{ MP_QSTR_borderType, MP_ARG_INT, { .u_int = BORDER_CONSTANT } },
54+
{ MP_QSTR_borderValue, MP_ARG_OBJ, { .u_obj = mp_const_none } },
5155
};
5256

5357
// Parse the arguments
@@ -58,10 +62,22 @@ mp_obj_t cv2_imgproc_dilate(size_t n_args, const mp_obj_t *pos_args, mp_map_t *k
5862
Mat src = mp_obj_to_mat(args[ARG_src].u_obj);
5963
Mat kernel = mp_obj_to_mat(args[ARG_kernel].u_obj);
6064
Mat dst = mp_obj_to_mat(args[ARG_dst].u_obj);
65+
Point anchor;
66+
if(args[ARG_anchor].u_obj == mp_const_none)
67+
anchor = Point(-1, -1); // Default value
68+
else
69+
anchor = mp_obj_to_point(args[ARG_anchor].u_obj);
70+
int iterations = args[ARG_iterations].u_int;
71+
int borderType = args[ARG_borderType].u_int;
72+
Scalar borderValue;
73+
if(args[ARG_borderValue].u_obj == mp_const_none)
74+
borderValue = morphologyDefaultBorderValue(); // Default value
75+
else
76+
borderValue = mp_obj_to_scalar(args[ARG_borderValue].u_obj);
6177

6278
// Call the corresponding OpenCV function
6379
try {
64-
dilate(src, dst, kernel);
80+
dilate(src, dst, kernel, anchor, iterations, borderType, borderValue);
6581
} catch(Exception& e) {
6682
mp_raise_msg(&mp_type_Exception, MP_ERROR_TEXT(e.what()));
6783
}
@@ -72,11 +88,15 @@ mp_obj_t cv2_imgproc_dilate(size_t n_args, const mp_obj_t *pos_args, mp_map_t *k
7288

7389
mp_obj_t cv2_imgproc_erode(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
7490
// Define the arguments
75-
enum { ARG_src, ARG_kernel, ARG_dst };
91+
enum { ARG_src, ARG_kernel, ARG_dst, ARG_anchor, ARG_iterations, ARG_borderType, ARG_borderValue };
7692
static const mp_arg_t allowed_args[] = {
7793
{ MP_QSTR_src, MP_ARG_REQUIRED | MP_ARG_OBJ, { .u_obj = MP_OBJ_NULL } },
7894
{ MP_QSTR_kernel, MP_ARG_REQUIRED | MP_ARG_OBJ, { .u_obj = MP_OBJ_NULL } },
7995
{ MP_QSTR_dst, MP_ARG_OBJ, { .u_obj = mp_const_none } },
96+
{ MP_QSTR_anchor, MP_ARG_OBJ, { .u_obj = mp_const_none } },
97+
{ MP_QSTR_iterations, MP_ARG_INT, { .u_int = 1 } },
98+
{ MP_QSTR_borderType, MP_ARG_INT, { .u_int = BORDER_CONSTANT } },
99+
{ MP_QSTR_borderValue, MP_ARG_OBJ, { .u_obj = mp_const_none } },
80100
};
81101

82102
// Parse the arguments
@@ -87,10 +107,22 @@ mp_obj_t cv2_imgproc_erode(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw
87107
Mat src = mp_obj_to_mat(args[ARG_src].u_obj);
88108
Mat kernel = mp_obj_to_mat(args[ARG_kernel].u_obj);
89109
Mat dst = mp_obj_to_mat(args[ARG_dst].u_obj);
110+
Point anchor;
111+
if(args[ARG_anchor].u_obj == mp_const_none)
112+
anchor = Point(-1, -1); // Default value
113+
else
114+
anchor = mp_obj_to_point(args[ARG_anchor].u_obj);
115+
int iterations = args[ARG_iterations].u_int;
116+
int borderType = args[ARG_borderType].u_int;
117+
Scalar borderValue;
118+
if(args[ARG_borderValue].u_obj == mp_const_none)
119+
borderValue = morphologyDefaultBorderValue(); // Default value
120+
else
121+
borderValue = mp_obj_to_scalar(args[ARG_borderValue].u_obj);
90122

91123
// Call the corresponding OpenCV function
92124
try {
93-
erode(src, dst, kernel);
125+
erode(src, dst, kernel, anchor, iterations, borderType, borderValue);
94126
} catch(Exception& e) {
95127
mp_raise_msg(&mp_type_Exception, MP_ERROR_TEXT(e.what()));
96128
}
@@ -101,10 +133,11 @@ mp_obj_t cv2_imgproc_erode(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw
101133

102134
mp_obj_t cv2_imgproc_getStructuringElement(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
103135
// Define the arguments
104-
enum { ARG_shape, ARG_ksize };
136+
enum { ARG_shape, ARG_ksize, ARG_anchor };
105137
static const mp_arg_t allowed_args[] = {
106138
{ MP_QSTR_shape, MP_ARG_REQUIRED | MP_ARG_INT, { .u_int = 0 } },
107139
{ MP_QSTR_ksize, MP_ARG_REQUIRED | MP_ARG_OBJ, { .u_obj = MP_OBJ_NULL } },
140+
{ MP_QSTR_anchor, MP_ARG_OBJ, { .u_obj = mp_const_none } },
108141
};
109142

110143
// Parse the arguments
@@ -114,13 +147,18 @@ mp_obj_t cv2_imgproc_getStructuringElement(size_t n_args, const mp_obj_t *pos_ar
114147
// Convert arguments to required types
115148
int shape = args[ARG_shape].u_int;
116149
Size ksize = mp_obj_to_size(args[ARG_ksize].u_obj);
150+
Point anchor;
151+
if(args[ARG_anchor].u_obj == mp_const_none)
152+
anchor = Point(-1, -1); // Default value
153+
else
154+
anchor = mp_obj_to_point(args[ARG_anchor].u_obj);
117155

118156
// Instantiate result
119157
Mat kernel;
120158

121159
// Call the corresponding OpenCV function
122160
try {
123-
kernel = getStructuringElement(shape, ksize);
161+
kernel = getStructuringElement(shape, ksize, anchor);
124162
} catch(Exception& e) {
125163
mp_raise_msg(&mp_type_Exception, MP_ERROR_TEXT(e.what()));
126164
}
@@ -131,12 +169,16 @@ mp_obj_t cv2_imgproc_getStructuringElement(size_t n_args, const mp_obj_t *pos_ar
131169

132170
mp_obj_t cv2_imgproc_morphologyEx(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
133171
// Define the arguments
134-
enum { ARG_src, ARG_op, ARG_kernel, ARG_dst };
172+
enum { ARG_src, ARG_op, ARG_kernel, ARG_dst, ARG_anchor, ARG_iterations, ARG_borderType, ARG_borderValue };
135173
static const mp_arg_t allowed_args[] = {
136174
{ MP_QSTR_src, MP_ARG_REQUIRED | MP_ARG_OBJ, { .u_obj = MP_OBJ_NULL } },
137175
{ MP_QSTR_op, MP_ARG_REQUIRED | MP_ARG_INT, { .u_int = 0 } },
138176
{ MP_QSTR_kernel, MP_ARG_REQUIRED | MP_ARG_OBJ, { .u_obj = MP_OBJ_NULL } },
139177
{ MP_QSTR_dst, MP_ARG_OBJ, { .u_obj = mp_const_none } },
178+
{ MP_QSTR_anchor, MP_ARG_OBJ, { .u_obj = mp_const_none } },
179+
{ MP_QSTR_iterations, MP_ARG_INT, { .u_int = 1 } },
180+
{ MP_QSTR_borderType, MP_ARG_INT, { .u_int = BORDER_CONSTANT } },
181+
{ MP_QSTR_borderValue, MP_ARG_OBJ, { .u_obj = mp_const_none } },
140182
};
141183

142184
// Parse the arguments
@@ -148,10 +190,22 @@ mp_obj_t cv2_imgproc_morphologyEx(size_t n_args, const mp_obj_t *pos_args, mp_ma
148190
int op = args[ARG_op].u_int;
149191
Mat kernel = mp_obj_to_mat(args[ARG_kernel].u_obj);
150192
Mat dst = mp_obj_to_mat(args[ARG_dst].u_obj);
193+
Point anchor;
194+
if(args[ARG_anchor].u_obj == mp_const_none)
195+
anchor = Point(-1, -1); // Default value
196+
else
197+
anchor = mp_obj_to_point(args[ARG_anchor].u_obj);
198+
int iterations = args[ARG_iterations].u_int;
199+
int borderType = args[ARG_borderType].u_int;
200+
Scalar borderValue;
201+
if(args[ARG_borderValue].u_obj == mp_const_none)
202+
borderValue = morphologyDefaultBorderValue(); // Default value
203+
else
204+
borderValue = mp_obj_to_scalar(args[ARG_borderValue].u_obj);
151205

152206
// Call the corresponding OpenCV function
153207
try {
154-
morphologyEx(src, dst, op, kernel);
208+
morphologyEx(src, dst, op, kernel, anchor, iterations, borderType, borderValue);
155209
} catch(Exception& e) {
156210
mp_raise_msg(&mp_type_Exception, MP_ERROR_TEXT(e.what()));
157211
}

0 commit comments

Comments
 (0)