Skip to content

Commit da3452c

Browse files
committed
Add thresholding functions
1 parent 1948925 commit da3452c

File tree

3 files changed

+90
-0
lines changed

3 files changed

+90
-0
lines changed

src/imgproc.cpp

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,43 @@ extern "C" {
1212

1313
using namespace cv;
1414

15+
mp_obj_t cv2_imgproc_adaptiveThreshold(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
16+
// Define the arguments
17+
enum { ARG_src, ARG_maxValue, ARG_adaptiveMethod, ARG_thresholdType, ARG_blockSize, ARG_C, ARG_dst };
18+
static const mp_arg_t allowed_args[] = {
19+
{ MP_QSTR_src, MP_ARG_REQUIRED | MP_ARG_OBJ, { .u_obj = MP_OBJ_NULL } },
20+
{ MP_QSTR_maxValue, MP_ARG_REQUIRED | MP_ARG_OBJ, { .u_obj = MP_OBJ_NULL } },
21+
{ MP_QSTR_adaptiveMethod, MP_ARG_REQUIRED | MP_ARG_INT, { .u_int = 0 } },
22+
{ MP_QSTR_thresholdType, MP_ARG_REQUIRED | MP_ARG_INT, { .u_int = 0 } },
23+
{ MP_QSTR_blockSize, MP_ARG_REQUIRED | MP_ARG_INT, { .u_int = 0 } },
24+
{ MP_QSTR_C, MP_ARG_REQUIRED | MP_ARG_OBJ, { .u_obj = MP_OBJ_NULL } },
25+
{ MP_QSTR_dst, MP_ARG_OBJ, { .u_obj = mp_const_none } },
26+
};
27+
28+
// Parse the arguments
29+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
30+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
31+
32+
// Convert arguments to required types
33+
Mat src = mp_obj_to_mat(args[ARG_src].u_obj);
34+
mp_float_t maxValue = mp_obj_get_float(args[ARG_maxValue].u_obj);
35+
int adaptiveMethod = args[ARG_adaptiveMethod].u_int;
36+
int thresholdType = args[ARG_thresholdType].u_int;
37+
int blockSize = args[ARG_blockSize].u_int;
38+
mp_float_t C = mp_obj_get_float(args[ARG_C].u_obj);
39+
Mat dst = mp_obj_to_mat(args[ARG_dst].u_obj);
40+
41+
// Call the corresponding OpenCV function
42+
try {
43+
adaptiveThreshold(src, dst, maxValue, adaptiveMethod, thresholdType, blockSize, C);
44+
} catch(Exception& e) {
45+
mp_raise_msg(&mp_type_Exception, MP_ERROR_TEXT(e.what()));
46+
}
47+
48+
// Return the result
49+
return mat_to_mp_obj(dst);
50+
}
51+
1552
mp_obj_t cv2_imgproc_arrowedLine(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
1653
// Define the arguments
1754
enum { ARG_img, ARG_pt1, ARG_pt2, ARG_color, ARG_thickness, ARG_line_type, ARG_shift, ARG_tipLength };
@@ -1259,3 +1296,36 @@ mp_obj_t cv2_imgproc_spatialGradient(size_t n_args, const mp_obj_t *pos_args, mp
12591296
result[1] = mat_to_mp_obj(dy);
12601297
return mp_obj_new_tuple(2, result);
12611298
}
1299+
1300+
mp_obj_t cv2_imgproc_threshold(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
1301+
// Define the arguments
1302+
enum { ARG_src, ARG_thresh, ARG_maxval, ARG_type, ARG_dst };
1303+
static const mp_arg_t allowed_args[] = {
1304+
{ MP_QSTR_src, MP_ARG_REQUIRED | MP_ARG_OBJ, { .u_obj = MP_OBJ_NULL } },
1305+
{ MP_QSTR_thresh, MP_ARG_REQUIRED | MP_ARG_OBJ, { .u_obj = MP_OBJ_NULL } },
1306+
{ MP_QSTR_maxval, MP_ARG_REQUIRED | MP_ARG_OBJ, { .u_obj = MP_OBJ_NULL } },
1307+
{ MP_QSTR_type, MP_ARG_REQUIRED | MP_ARG_INT, { .u_int = THRESH_BINARY } },
1308+
{ MP_QSTR_dst, MP_ARG_OBJ, { .u_obj = mp_const_none } },
1309+
};
1310+
1311+
// Parse the arguments
1312+
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
1313+
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
1314+
1315+
// Convert arguments to required types
1316+
Mat src = mp_obj_to_mat(args[ARG_src].u_obj);
1317+
mp_float_t thresh = mp_obj_get_float(args[ARG_thresh].u_obj);
1318+
mp_float_t maxval = mp_obj_get_float(args[ARG_maxval].u_obj);
1319+
int type = args[ARG_type].u_int;
1320+
Mat dst = mp_obj_to_mat(args[ARG_dst].u_obj);
1321+
1322+
// Call the corresponding OpenCV function
1323+
try {
1324+
threshold(src, dst, thresh, maxval, type);
1325+
} catch(Exception& e) {
1326+
mp_raise_msg(&mp_type_Exception, MP_ERROR_TEXT(e.what()));
1327+
}
1328+
1329+
// Return the result
1330+
return mat_to_mp_obj(dst);
1331+
}

src/imgproc.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// C headers
22
#include "py/runtime.h"
33

4+
extern mp_obj_t cv2_imgproc_adaptiveThreshold(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
45
extern mp_obj_t cv2_imgproc_arrowedLine(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
56
extern mp_obj_t cv2_imgproc_bilateralFilter(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
67
extern mp_obj_t cv2_imgproc_blur(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
@@ -31,3 +32,4 @@ extern mp_obj_t cv2_imgproc_rectangle(size_t n_args, const mp_obj_t *pos_args, m
3132
extern mp_obj_t cv2_imgproc_Scharr(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
3233
extern mp_obj_t cv2_imgproc_Sobel(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
3334
extern mp_obj_t cv2_imgproc_spatialGradient(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);
35+
extern mp_obj_t cv2_imgproc_threshold(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args);

src/opencv_upy.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
static MP_DEFINE_CONST_FUN_OBJ_KW(cv2_core_inRange_obj, 3, cv2_core_inRange);
1010

1111
// OpenCV imgproc module
12+
static MP_DEFINE_CONST_FUN_OBJ_KW(cv2_imgproc_adaptiveThreshold_obj, 6, cv2_imgproc_adaptiveThreshold);
1213
static MP_DEFINE_CONST_FUN_OBJ_KW(cv2_imgproc_arrowedLine_obj, 4, cv2_imgproc_arrowedLine);
1314
static MP_DEFINE_CONST_FUN_OBJ_KW(cv2_imgproc_bilateralFilter_obj, 4, cv2_imgproc_bilateralFilter);
1415
static MP_DEFINE_CONST_FUN_OBJ_KW(cv2_imgproc_blur_obj, 2, cv2_imgproc_blur);
@@ -39,6 +40,7 @@ static MP_DEFINE_CONST_FUN_OBJ_KW(cv2_imgproc_rectangle_obj, 4, cv2_imgproc_rect
3940
static MP_DEFINE_CONST_FUN_OBJ_KW(cv2_imgproc_Scharr_obj, 4, cv2_imgproc_Scharr);
4041
static MP_DEFINE_CONST_FUN_OBJ_KW(cv2_imgproc_Sobel_obj, 4, cv2_imgproc_Sobel);
4142
static MP_DEFINE_CONST_FUN_OBJ_KW(cv2_imgproc_spatialGradient_obj, 1, cv2_imgproc_spatialGradient);
43+
static MP_DEFINE_CONST_FUN_OBJ_KW(cv2_imgproc_threshold_obj, 4, cv2_imgproc_threshold);
4244

4345
////////////////////////////////////////////////////////////////////////////////
4446
// Module attributes
@@ -84,6 +86,20 @@ static const mp_rom_map_elem_t cv2_module_globals_table[] = {
8486
{ MP_ROM_QSTR(MP_QSTR_MORPH_CROSS), MP_ROM_INT(1) },
8587
{ MP_ROM_QSTR(MP_QSTR_MORPH_ELLIPSE), MP_ROM_INT(2) },
8688

89+
// Threshold types, from opencv2/imgproc.hpp
90+
{ MP_ROM_QSTR(MP_QSTR_THRESH_BINARY), MP_ROM_INT(0) },
91+
{ MP_ROM_QSTR(MP_QSTR_THRESH_BINARY_INV), MP_ROM_INT(1) },
92+
{ MP_ROM_QSTR(MP_QSTR_THRESH_TRUNC), MP_ROM_INT(2) },
93+
{ MP_ROM_QSTR(MP_QSTR_THRESH_TOZERO), MP_ROM_INT(3) },
94+
{ MP_ROM_QSTR(MP_QSTR_THRESH_TOZERO_INV), MP_ROM_INT(4) },
95+
{ MP_ROM_QSTR(MP_QSTR_THRESH_MASK), MP_ROM_INT(7) },
96+
{ MP_ROM_QSTR(MP_QSTR_THRESH_OTSU), MP_ROM_INT(8) },
97+
{ MP_ROM_QSTR(MP_QSTR_THRESH_TRIANGLE), MP_ROM_INT(16) },
98+
99+
// Adaptive threshold methods, from opencv2/imgproc.hpp
100+
{ MP_ROM_QSTR(MP_QSTR_ADAPTIVE_THRESH_MEAN_C), MP_ROM_INT(0) },
101+
{ MP_ROM_QSTR(MP_QSTR_ADAPTIVE_THRESH_GAUSSIAN_C), MP_ROM_INT(1) },
102+
87103
// Hough modes, from opencv2/imgproc.hpp
88104
{ MP_ROM_QSTR(MP_QSTR_HOUGH_STANDARD), MP_ROM_INT(0) },
89105
{ MP_ROM_QSTR(MP_QSTR_HOUGH_PROBABILISTIC), MP_ROM_INT(1) },
@@ -179,6 +195,7 @@ static const mp_rom_map_elem_t cv2_module_globals_table[] = {
179195
// OpenCV imgproc functions
180196
////////////////////////////////////////////////////////////////////////////
181197

198+
{ MP_ROM_QSTR(MP_QSTR_adaptiveThreshold), MP_ROM_PTR(&cv2_imgproc_adaptiveThreshold_obj) },
182199
{ MP_ROM_QSTR(MP_QSTR_arrowedLine), MP_ROM_PTR(&cv2_imgproc_arrowedLine_obj) },
183200
{ MP_ROM_QSTR(MP_QSTR_bilateralFilter), MP_ROM_PTR(&cv2_imgproc_bilateralFilter_obj) },
184201
{ MP_ROM_QSTR(MP_QSTR_blur), MP_ROM_PTR(&cv2_imgproc_blur_obj) },
@@ -209,6 +226,7 @@ static const mp_rom_map_elem_t cv2_module_globals_table[] = {
209226
{ MP_ROM_QSTR(MP_QSTR_Scharr), MP_ROM_PTR(&cv2_imgproc_Scharr_obj) },
210227
{ MP_ROM_QSTR(MP_QSTR_Sobel), MP_ROM_PTR(&cv2_imgproc_Sobel_obj) },
211228
{ MP_ROM_QSTR(MP_QSTR_spatialGradient), MP_ROM_PTR(&cv2_imgproc_spatialGradient_obj) },
229+
{ MP_ROM_QSTR(MP_QSTR_threshold), MP_ROM_PTR(&cv2_imgproc_threshold_obj) },
212230
};
213231
static MP_DEFINE_CONST_DICT(cv2_module_globals, cv2_module_globals_table);
214232

0 commit comments

Comments
 (0)