Skip to content

Commit 527a8a5

Browse files
committed
Add more contour functions
1 parent 83376cf commit 527a8a5

File tree

6 files changed

+753
-0
lines changed

6 files changed

+753
-0
lines changed

src/convert.cpp

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,61 @@ Size mp_obj_to_size(mp_obj_t obj)
201201
return size;
202202
}
203203

204+
Size2f mp_obj_to_size2f(mp_obj_t obj)
205+
{
206+
// Check for None object
207+
if(obj == mp_const_none)
208+
{
209+
// Create an empty Size2f object
210+
return Size2f();
211+
}
212+
213+
// Assume the object is a ndarray, or can be converted to one. Will raise an
214+
// exception if not
215+
ndarray_obj_t *ndarray = ndarray_from_mp_obj(obj, 0);
216+
217+
// Validate the length of the ndarray
218+
if(ndarray->len != 2)
219+
{
220+
mp_raise_TypeError(MP_ERROR_TEXT("Size2f must be length 2"));
221+
}
222+
223+
// Compute the size, checking the type of the ndarray
224+
Size2f size;
225+
switch(ndarray->dtype)
226+
{
227+
case NDARRAY_UINT8:
228+
size.width = ((uint8_t*) ndarray->array)[0];
229+
size.height = ((uint8_t*) ndarray->array)[1];
230+
break;
231+
case NDARRAY_INT8:
232+
size.width = ((int8_t*) ndarray->array)[0];
233+
size.height = ((int8_t*) ndarray->array)[1];
234+
break;
235+
case NDARRAY_UINT16:
236+
size.width = ((uint16_t*) ndarray->array)[0];
237+
size.height = ((uint16_t*) ndarray->array)[1];
238+
break;
239+
case NDARRAY_INT16:
240+
size.width = ((int16_t*) ndarray->array)[0];
241+
size.height = ((int16_t*) ndarray->array)[1];
242+
break;
243+
case NDARRAY_FLOAT:
244+
size.width = ((float*) ndarray->array)[0];
245+
size.height = ((float*) ndarray->array)[1];
246+
break;
247+
case NDARRAY_BOOL:
248+
size.width = ((bool*) ndarray->array)[0];
249+
size.height = ((bool*) ndarray->array)[1];
250+
break;
251+
default:
252+
mp_raise_TypeError(MP_ERROR_TEXT("Unsupported ndarray type"));
253+
break;
254+
}
255+
256+
return size;
257+
}
258+
204259
Point mp_obj_to_point(mp_obj_t obj)
205260
{
206261
// Check for None object
@@ -256,6 +311,61 @@ Point mp_obj_to_point(mp_obj_t obj)
256311
return point;
257312
}
258313

314+
Point2f mp_obj_to_point2f(mp_obj_t obj)
315+
{
316+
// Check for None object
317+
if(obj == mp_const_none)
318+
{
319+
// Create an empty Point2f object
320+
return Point2f();
321+
}
322+
323+
// Assume the object is a ndarray, or can be converted to one. Will raise an
324+
// exception if not
325+
ndarray_obj_t *ndarray = ndarray_from_mp_obj(obj, 0);
326+
327+
// Validate the length of the ndarray
328+
if(ndarray->len != 2)
329+
{
330+
mp_raise_TypeError(MP_ERROR_TEXT("Point2f must be length 2"));
331+
}
332+
333+
// Compute the point, checking the type of the ndarray
334+
Point2f point;
335+
switch(ndarray->dtype)
336+
{
337+
case NDARRAY_UINT8:
338+
point.x = ((uint8_t*) ndarray->array)[0];
339+
point.y = ((uint8_t*) ndarray->array)[1];
340+
break;
341+
case NDARRAY_INT8:
342+
point.x = ((int8_t*) ndarray->array)[0];
343+
point.y = ((int8_t*) ndarray->array)[1];
344+
break;
345+
case NDARRAY_UINT16:
346+
point.x = ((uint16_t*) ndarray->array)[0];
347+
point.y = ((uint16_t*) ndarray->array)[1];
348+
break;
349+
case NDARRAY_INT16:
350+
point.x = ((int16_t*) ndarray->array)[0];
351+
point.y = ((int16_t*) ndarray->array)[1];
352+
break;
353+
case NDARRAY_FLOAT:
354+
point.x = ((float*) ndarray->array)[0];
355+
point.y = ((float*) ndarray->array)[1];
356+
break;
357+
case NDARRAY_BOOL:
358+
point.x = ((bool*) ndarray->array)[0];
359+
point.y = ((bool*) ndarray->array)[1];
360+
break;
361+
default:
362+
mp_raise_TypeError(MP_ERROR_TEXT("Unsupported ndarray type"));
363+
break;
364+
}
365+
366+
return point;
367+
}
368+
259369
Scalar mp_obj_to_scalar(mp_obj_t obj)
260370
{
261371
// Check for None object

src/convert.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,11 @@ Mat mp_obj_to_mat(mp_obj_t obj);
2424

2525
// Conversion functions between Size and mp_obj_t
2626
Size mp_obj_to_size(mp_obj_t obj);
27+
Size2f mp_obj_to_size2f(mp_obj_t obj);
2728

2829
// Conversion functions between Point and mp_obj_t
2930
Point mp_obj_to_point(mp_obj_t obj);
31+
Point2f mp_obj_to_point2f(mp_obj_t obj);
3032

3133
// Conversion functions between Scalar and mp_obj_t
3234
Scalar mp_obj_to_scalar(mp_obj_t obj);

src/core.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,3 +122,47 @@ mp_obj_t cv2_core_inRange(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_
122122
// Return the result
123123
return mat_to_mp_obj(dst);
124124
}
125+
126+
mp_obj_t cv2_core_minMaxLoc(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
127+
// Define the arguments
128+
enum { ARG_src, ARG_mask };
129+
static const mp_arg_t allowed_args[] = {
130+
{ MP_QSTR_src, MP_ARG_REQUIRED | MP_ARG_OBJ, { .u_obj = MP_OBJ_NULL } },
131+
{ MP_QSTR_mask, MP_ARG_OBJ, { .u_obj = mp_const_none } },
132+
};
133+
134+
// Parse the arguments
135+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
136+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
137+
138+
// Convert arguments to required types
139+
Mat src = mp_obj_to_mat(args[ARG_src].u_obj);
140+
Mat mask = mp_obj_to_mat(args[ARG_mask].u_obj);
141+
142+
double minVal, maxVal;
143+
Point minLoc, maxLoc;
144+
145+
// Call the corresponding OpenCV function
146+
try {
147+
minMaxLoc(src, &minVal, &maxVal, &minLoc, &maxLoc, mask);
148+
} catch(Exception& e) {
149+
mp_raise_msg(&mp_type_Exception, MP_ERROR_TEXT(e.what()));
150+
}
151+
152+
// Return the result
153+
mp_obj_t min_loc_tuple[2] = {
154+
mp_obj_new_float(minLoc.x),
155+
mp_obj_new_float(minLoc.y)
156+
};
157+
mp_obj_t max_loc_tuple[2] = {
158+
mp_obj_new_float(maxLoc.x),
159+
mp_obj_new_float(maxLoc.y)
160+
};
161+
mp_obj_t result_tuple[4] = {
162+
mp_obj_new_float(minVal),
163+
mp_obj_new_float(maxVal),
164+
mp_obj_new_tuple(2, min_loc_tuple),
165+
mp_obj_new_tuple(2, max_loc_tuple)
166+
};
167+
return mp_obj_new_tuple(4, result_tuple);
168+
}

src/core.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,19 @@
44
// Function declarations
55
extern mp_obj_t cv2_core_convertScaleAbs(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
66
extern mp_obj_t cv2_core_inRange(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
7+
extern mp_obj_t cv2_core_minMaxLoc(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
78

89
// Python references to the functions
910
static MP_DEFINE_CONST_FUN_OBJ_KW(cv2_core_convertScaleAbs_obj, 1, cv2_core_convertScaleAbs);
1011
static MP_DEFINE_CONST_FUN_OBJ_KW(cv2_core_inRange_obj, 3, cv2_core_inRange);
12+
static MP_DEFINE_CONST_FUN_OBJ_KW(cv2_core_minMaxLoc_obj, 1, cv2_core_minMaxLoc);
1113

1214
// Global definitions for functions and constants
1315
#define OPENCV_CORE_GLOBALS \
1416
/* Functions */ \
1517
{ MP_ROM_QSTR(MP_QSTR_convertScaleAbs), MP_ROM_PTR(&cv2_core_convertScaleAbs_obj) }, \
1618
{ MP_ROM_QSTR(MP_QSTR_inRange), MP_ROM_PTR(&cv2_core_inRange_obj) }, \
19+
{ MP_ROM_QSTR(MP_QSTR_minMaxLoc), MP_ROM_PTR(&cv2_core_minMaxLoc_obj) }, \
1720
\
1821
/* OpenCV data types, from opencv2/core/hal/interface.h */ \
1922
/* Other types are currently not supported by ulab */ \

0 commit comments

Comments
 (0)