|
| 1 | +// C++ headers |
| 2 | +#include "opencv2/core.hpp" |
| 3 | +#include "convert.h" |
| 4 | +#include "numpy.h" |
| 5 | + |
| 6 | +// C headers |
| 7 | +extern "C" { |
| 8 | +#include "core.h" |
| 9 | +#include "ulab/code/ndarray.h" |
| 10 | +} // extern "C" |
| 11 | + |
| 12 | +using namespace cv; |
| 13 | + |
| 14 | +mp_obj_t cv2_core_inRange(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 15 | + // Define the arguments |
| 16 | + enum { ARG_src, ARG_lower, ARG_upper, ARG_dst }; |
| 17 | + static const mp_arg_t allowed_args[] = { |
| 18 | + { MP_QSTR_src, MP_ARG_REQUIRED | MP_ARG_OBJ, { .u_obj = MP_OBJ_NULL } }, |
| 19 | + { MP_QSTR_lower, MP_ARG_REQUIRED | MP_ARG_OBJ, { .u_obj = MP_OBJ_NULL } }, |
| 20 | + { MP_QSTR_upper, MP_ARG_REQUIRED | MP_ARG_OBJ, { .u_obj = MP_OBJ_NULL } }, |
| 21 | + { MP_QSTR_dst, MP_ARG_OBJ, { .u_obj = mp_const_none } }, |
| 22 | + }; |
| 23 | + |
| 24 | + // Parse the arguments |
| 25 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 26 | + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 27 | + |
| 28 | + // Convert arguments to required types |
| 29 | + Mat src = mp_obj_to_mat(args[ARG_src].u_obj); |
| 30 | + Mat lower = mp_obj_to_mat(args[ARG_lower].u_obj); |
| 31 | + Mat upper = mp_obj_to_mat(args[ARG_upper].u_obj); |
| 32 | + Mat dst = mp_obj_to_mat(args[ARG_dst].u_obj); |
| 33 | + |
| 34 | + // Call the corresponding OpenCV function |
| 35 | + try { |
| 36 | + inRange(src, lower, upper, dst); |
| 37 | + } catch(Exception& e) { |
| 38 | + mp_raise_msg(&mp_type_Exception, MP_ERROR_TEXT(e.what())); |
| 39 | + } |
| 40 | + |
| 41 | + // Return the result |
| 42 | + return mat_to_mp_obj(dst); |
| 43 | +} |
| 44 | + |
| 45 | +mp_obj_t cv2_core_max(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 46 | + // Define the arguments |
| 47 | + enum { ARG_src1, ARG_src2, ARG_dst }; |
| 48 | + static const mp_arg_t allowed_args[] = { |
| 49 | + { MP_QSTR_src1, MP_ARG_REQUIRED | MP_ARG_OBJ, { .u_obj = MP_OBJ_NULL } }, |
| 50 | + { MP_QSTR_src2, MP_ARG_REQUIRED | MP_ARG_OBJ, { .u_obj = MP_OBJ_NULL } }, |
| 51 | + { MP_QSTR_dst, MP_ARG_OBJ, { .u_obj = mp_const_none } }, |
| 52 | + }; |
| 53 | + |
| 54 | + // Parse the arguments |
| 55 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 56 | + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 57 | + |
| 58 | + // Convert arguments to required types |
| 59 | + Mat src1 = mp_obj_to_mat(args[ARG_src1].u_obj); |
| 60 | + Mat src2 = mp_obj_to_mat(args[ARG_src2].u_obj); |
| 61 | + Mat dst = mp_obj_to_mat(args[ARG_dst].u_obj); |
| 62 | + |
| 63 | + // Call the corresponding OpenCV function |
| 64 | + try { |
| 65 | + max(src1, src2, dst); |
| 66 | + } catch(Exception& e) { |
| 67 | + mp_raise_msg(&mp_type_Exception, MP_ERROR_TEXT(e.what())); |
| 68 | + } |
| 69 | + |
| 70 | + // Return the result |
| 71 | + return mat_to_mp_obj(dst); |
| 72 | +} |
| 73 | + |
| 74 | +mp_obj_t cv2_core_min(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { |
| 75 | + // Define the arguments |
| 76 | + enum { ARG_src1, ARG_src2, ARG_dst }; |
| 77 | + static const mp_arg_t allowed_args[] = { |
| 78 | + { MP_QSTR_src1, MP_ARG_REQUIRED | MP_ARG_OBJ, { .u_obj = MP_OBJ_NULL } }, |
| 79 | + { MP_QSTR_src2, MP_ARG_REQUIRED | MP_ARG_OBJ, { .u_obj = MP_OBJ_NULL } }, |
| 80 | + { MP_QSTR_dst, MP_ARG_OBJ, { .u_obj = mp_const_none } }, |
| 81 | + }; |
| 82 | + |
| 83 | + // Parse the arguments |
| 84 | + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; |
| 85 | + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); |
| 86 | + |
| 87 | + // Convert arguments to required types |
| 88 | + Mat src1 = mp_obj_to_mat(args[ARG_src1].u_obj); |
| 89 | + Mat src2 = mp_obj_to_mat(args[ARG_src2].u_obj); |
| 90 | + Mat dst = mp_obj_to_mat(args[ARG_dst].u_obj); |
| 91 | + |
| 92 | + // Call the corresponding OpenCV function |
| 93 | + try { |
| 94 | + min(src1, src2, dst); |
| 95 | + } catch(Exception& e) { |
| 96 | + mp_raise_msg(&mp_type_Exception, MP_ERROR_TEXT(e.what())); |
| 97 | + } |
| 98 | + |
| 99 | + // Return the result |
| 100 | + return mat_to_mp_obj(dst); |
| 101 | +} |
0 commit comments