@@ -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
472481mp_obj_t cv2_imgproc_contourArea (size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
473482 // Define the arguments
0 commit comments