Skip to content

Commit 2486762

Browse files
committed
Add connectedComponentsWithStats()
Fixes #6 Just convert output matrices to floats
1 parent 3acc441 commit 2486762

File tree

2 files changed

+54
-45
lines changed

2 files changed

+54
-45
lines changed

src/imgproc.cpp

Lines changed: 51 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -426,48 +426,57 @@ mp_obj_t cv2_imgproc_connectedComponents(size_t n_args, const mp_obj_t *pos_args
426426
return mp_obj_new_tuple(2, result);
427427
}
428428

429-
// mp_obj_t cv2_imgproc_connectedComponentsWithStats(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
430-
// // Define the arguments
431-
// enum { ARG_image, ARG_labels, ARG_stats, ARG_centroids, ARG_connectivity, ARG_ltype };
432-
// static const mp_arg_t allowed_args[] = {
433-
// { MP_QSTR_image, MP_ARG_REQUIRED | MP_ARG_OBJ, { .u_obj = MP_OBJ_NULL } },
434-
// { MP_QSTR_labels, MP_ARG_OBJ, { .u_obj = mp_const_none } },
435-
// { MP_QSTR_stats, MP_ARG_OBJ, { .u_obj = mp_const_none } },
436-
// { MP_QSTR_centroids, MP_ARG_OBJ, { .u_obj = mp_const_none } },
437-
// { MP_QSTR_connectivity, MP_ARG_INT, { .u_int = 8 } },
438-
// { MP_QSTR_ltype, MP_ARG_INT, { .u_int = CV_16U } }, // Normally CV_32S, but ulab doesn't support 32-bit integers
439-
// };
440-
441-
// // Parse the arguments
442-
// mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
443-
// mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
444-
445-
// // Convert arguments to required types
446-
// Mat image = mp_obj_to_mat(args[ARG_image].u_obj);
447-
// Mat labels = mp_obj_to_mat(args[ARG_labels].u_obj);
448-
// Mat stats = mp_obj_to_mat(args[ARG_stats].u_obj);
449-
// Mat centroids = mp_obj_to_mat(args[ARG_centroids].u_obj);
450-
// int connectivity = args[ARG_connectivity].u_int;
451-
// int ltype = args[ARG_ltype].u_int;
452-
453-
// // Return value
454-
// int retval = 0;
455-
456-
// // Call the corresponding OpenCV function
457-
// try {
458-
// retval = connectedComponentsWithStats(image, labels, stats, centroids, connectivity, ltype);
459-
// } catch(Exception& e) {
460-
// mp_raise_msg(&mp_type_Exception, MP_ERROR_TEXT(e.what()));
461-
// }
462-
463-
// // Return the result
464-
// mp_obj_t result[4];
465-
// result[0] = mp_obj_new_int(retval);
466-
// result[1] = mat_to_mp_obj(labels);
467-
// result[2] = mat_to_mp_obj(stats);
468-
// result[3] = mat_to_mp_obj(centroids);
469-
// return mp_obj_new_tuple(4, result);
470-
// }
429+
mp_obj_t cv2_imgproc_connectedComponentsWithStats(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
430+
// Define the arguments
431+
enum { ARG_image, ARG_labels, ARG_stats, ARG_centroids, ARG_connectivity, ARG_ltype };
432+
static const mp_arg_t allowed_args[] = {
433+
{ MP_QSTR_image, MP_ARG_REQUIRED | MP_ARG_OBJ, { .u_obj = MP_OBJ_NULL } },
434+
{ MP_QSTR_labels, MP_ARG_OBJ, { .u_obj = mp_const_none } },
435+
{ MP_QSTR_stats, MP_ARG_OBJ, { .u_obj = mp_const_none } },
436+
{ MP_QSTR_centroids, MP_ARG_OBJ, { .u_obj = mp_const_none } },
437+
{ MP_QSTR_connectivity, MP_ARG_INT, { .u_int = 8 } },
438+
{ MP_QSTR_ltype, MP_ARG_INT, { .u_int = CV_16U } }, // Normally CV_32S, but ulab doesn't support 32-bit integers
439+
};
440+
441+
// Parse the arguments
442+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
443+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
444+
445+
// Convert arguments to required types
446+
Mat image = mp_obj_to_mat(args[ARG_image].u_obj);
447+
Mat labels32S; // TODO: Allow user input
448+
Mat stats32S; // TODO: Allow user input
449+
Mat centroids64F; // TODO: Allow user input
450+
int connectivity = args[ARG_connectivity].u_int;
451+
int ltype = args[ARG_ltype].u_int;
452+
453+
// Return value
454+
int retval = 0;
455+
456+
// Call the corresponding OpenCV function
457+
try {
458+
retval = connectedComponentsWithStats(image, labels32S, stats32S, centroids64F, connectivity, ltype);
459+
} catch(Exception& e) {
460+
mp_raise_msg(&mp_type_Exception, MP_ERROR_TEXT(e.what()));
461+
}
462+
463+
// Convert output matrices to float
464+
Mat labels, stats, centroids;
465+
labels.allocator = &GetNumpyAllocator();
466+
stats.allocator = &GetNumpyAllocator();
467+
centroids.allocator = &GetNumpyAllocator();
468+
labels32S.convertTo(labels, CV_32F);
469+
stats32S.convertTo(stats, CV_32F);
470+
centroids64F.convertTo(centroids, CV_32F);
471+
472+
// Return the result
473+
mp_obj_t result[4];
474+
result[0] = mp_obj_new_int(retval);
475+
result[1] = mat_to_mp_obj(labels);
476+
result[2] = mat_to_mp_obj(stats);
477+
result[3] = mat_to_mp_obj(centroids);
478+
return mp_obj_new_tuple(4, result);
479+
}
471480

472481
mp_obj_t cv2_imgproc_contourArea(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
473482
// Define the arguments

src/imgproc.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ extern mp_obj_t cv2_imgproc_boxFilter(size_t n_args, const mp_obj_t *pos_args, m
1414
extern mp_obj_t cv2_imgproc_boxPoints(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
1515
extern mp_obj_t cv2_imgproc_Canny(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
1616
extern mp_obj_t cv2_imgproc_connectedComponents(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
17-
// extern mp_obj_t cv2_imgproc_connectedComponentsWithStats(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
17+
extern mp_obj_t cv2_imgproc_connectedComponentsWithStats(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
1818
extern mp_obj_t cv2_imgproc_contourArea(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
1919
extern mp_obj_t cv2_imgproc_convexHull(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
2020
extern mp_obj_t cv2_imgproc_convexityDefects(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
@@ -70,7 +70,7 @@ static MP_DEFINE_CONST_FUN_OBJ_KW(cv2_imgproc_boxFilter_obj, 3, cv2_imgproc_boxF
7070
static MP_DEFINE_CONST_FUN_OBJ_KW(cv2_imgproc_boxPoints_obj, 1, cv2_imgproc_boxPoints);
7171
static MP_DEFINE_CONST_FUN_OBJ_KW(cv2_imgproc_Canny_obj, 3, cv2_imgproc_Canny);
7272
static MP_DEFINE_CONST_FUN_OBJ_KW(cv2_imgproc_connectedComponents_obj, 1, cv2_imgproc_connectedComponents);
73-
// static MP_DEFINE_CONST_FUN_OBJ_KW(cv2_imgproc_connectedComponentsWithStats_obj, 1, cv2_imgproc_connectedComponentsWithStats);
73+
static MP_DEFINE_CONST_FUN_OBJ_KW(cv2_imgproc_connectedComponentsWithStats_obj, 1, cv2_imgproc_connectedComponentsWithStats);
7474
static MP_DEFINE_CONST_FUN_OBJ_KW(cv2_imgproc_contourArea_obj, 1, cv2_imgproc_contourArea);
7575
static MP_DEFINE_CONST_FUN_OBJ_KW(cv2_imgproc_convexHull_obj, 1, cv2_imgproc_convexHull);
7676
static MP_DEFINE_CONST_FUN_OBJ_KW(cv2_imgproc_convexityDefects_obj, 1, cv2_imgproc_convexityDefects);
@@ -128,7 +128,7 @@ static MP_DEFINE_CONST_FUN_OBJ_KW(cv2_imgproc_threshold_obj, 4, cv2_imgproc_thre
128128
{ MP_ROM_QSTR(MP_QSTR_boxPoints), MP_ROM_PTR(&cv2_imgproc_boxPoints_obj) }, \
129129
{ MP_ROM_QSTR(MP_QSTR_Canny), MP_ROM_PTR(&cv2_imgproc_Canny_obj) }, \
130130
{ MP_ROM_QSTR(MP_QSTR_connectedComponents), MP_ROM_PTR(&cv2_imgproc_connectedComponents_obj) }, \
131-
/* { MP_ROM_QSTR(MP_QSTR_connectedComponentsWithStats), MP_ROM_PTR(&cv2_imgproc_connectedComponentsWithStats_obj) }, */ \
131+
{ MP_ROM_QSTR(MP_QSTR_connectedComponentsWithStats), MP_ROM_PTR(&cv2_imgproc_connectedComponentsWithStats_obj) }, \
132132
{ MP_ROM_QSTR(MP_QSTR_contourArea), MP_ROM_PTR(&cv2_imgproc_contourArea_obj) }, \
133133
{ MP_ROM_QSTR(MP_QSTR_convexHull), MP_ROM_PTR(&cv2_imgproc_convexHull_obj) }, \
134134
{ MP_ROM_QSTR(MP_QSTR_convexityDefects), MP_ROM_PTR(&cv2_imgproc_convexityDefects_obj) }, \

0 commit comments

Comments
 (0)