diff --git a/src/omv/img/imlib.c b/src/omv/img/imlib.c index 7be158353..9e7315b2a 100644 --- a/src/omv/img/imlib.c +++ b/src/omv/img/imlib.c @@ -366,8 +366,8 @@ int imlib_centroid(image_t *img, int *x_center, int *y_center, rectangle_t *r) int y = (rect.y + i); if(IM_GET_GS_PIXEL(img, x, y)) { sum += 1; - x_center += x; - y_center += y; + x_sum += x; + y_sum += y; } } } @@ -378,8 +378,8 @@ int imlib_centroid(image_t *img, int *x_center, int *y_center, rectangle_t *r) int y = (rect.y + i); if(IM_GET_RGB565_PIXEL(img, x, y)) { sum += 1; - x_center += x; - y_center += y; + x_sum += x; + y_sum += y; } } } diff --git a/src/omv/py/py_image.c b/src/omv/py/py_image.c index 158890b3d..7e8f55ef3 100644 --- a/src/omv/py/py_image.c +++ b/src/omv/py/py_image.c @@ -377,17 +377,35 @@ static mp_obj_t py_image_draw_keypoints(uint n_args, const mp_obj_t *args, mp_ma PY_ASSERT_FALSE_MSG(IM_IS_JPEG(arg_img), "Operation not supported on JPEG"); - mp_obj_t kpts_obj = args[1]; - PY_ASSERT_TYPE(kpts_obj, &py_kp_type); int arg_c = get_color_kw(kw_args, -1); // white int arg_s = get_int_kw(kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_size), 10); - for (int i=0; i<((py_kp_obj_t*)kpts_obj)->size; i++) { - kp_t *kp = &((py_kp_obj_t*)kpts_obj)->kpts[i]; - float co = arm_cos_f32(kp->angle); - float si = arm_sin_f32(kp->angle); - imlib_draw_line(arg_img, kp->x, kp->y, kp->x+(co*arg_s), kp->y+(si*arg_s), arg_c); - imlib_draw_circle(arg_img, kp->x, kp->y, (arg_s-2)/2, arg_c); + if (MP_OBJ_IS_TYPE(args[1],&mp_type_tuple)||(MP_OBJ_IS_TYPE(args[1],&mp_type_list))) { + mp_uint_t arg_vec_len; + mp_obj_t *arg_vec; + mp_obj_get_array(args[1], &arg_vec_len, &arg_vec); + if (!arg_vec_len) return mp_const_none; + for (int i=0; isize; i++) { + kp_t *kp = &((py_kp_obj_t*)kpts_obj)->kpts[i]; + float co = arm_cos_f32(kp->angle); + float si = arm_sin_f32(kp->angle); + imlib_draw_line(arg_img, kp->x, kp->y, kp->x+(co*arg_s), kp->y+(si*arg_s), arg_c); + imlib_draw_circle(arg_img, kp->x, kp->y, (arg_s-2)/2, arg_c); + } } return mp_const_none; } @@ -401,7 +419,7 @@ static mp_obj_t py_image_binary(uint n_args, const mp_obj_t *args, mp_map_t *kw_ mp_uint_t arg_t_len; mp_obj_t *arg_t; mp_obj_get_array(args[1], &arg_t_len, &arg_t); - PY_ASSERT_TRUE_MSG(arg_t_len, "Invalid Argument: threshold_list_len == 0"); + if (!arg_t_len) return mp_const_none; simple_color_t l_t[arg_t_len], u_t[arg_t_len]; if (IM_IS_GS(arg_img)) { diff --git a/usr/tests/test_binary_1.py b/usr/tests/test_binary_1.py new file mode 100644 index 000000000..5cbae36cf --- /dev/null +++ b/usr/tests/test_binary_1.py @@ -0,0 +1,31 @@ +import pyb, sensor, image, math +sensor.reset() +sensor.set_framesize(sensor.QVGA) +sensor.set_pixformat(sensor.GRAYSCALE) +low_threshold = (0, 50) +high_threshold = (205, 255) +while(True): + # Test low threshold + for i in range(100): + img = sensor.snapshot() + img.binary([low_threshold]) + sum, x, y, rads = img.orientation_radians() + img.draw_keypoints([(x, y, rads)], color = 127, size = 20) + # Test high threshold + for i in range(100): + img = sensor.snapshot() + img.binary([high_threshold]) + sum, x, y, rads = img.orientation_radians() + img.draw_keypoints([(x, y, rads)], color = 127, size = 20) + # Test not low threshold + for i in range(100): + img = sensor.snapshot() + img.binary([low_threshold], invert = 1) + sum, x, y, rads = img.orientation_radians() + img.draw_keypoints([(x, y, rads)], color = 127, size = 20) + # Test not high threshold + for i in range(100): + img = sensor.snapshot() + img.binary([high_threshold], invert = 1) + sum, x, y, rads = img.orientation_radians() + img.draw_keypoints([(x, y, rads)], color = 127, size = 20) diff --git a/usr/tests/test_binary_2.py b/usr/tests/test_binary_2.py new file mode 100644 index 000000000..3bf1a4281 --- /dev/null +++ b/usr/tests/test_binary_2.py @@ -0,0 +1,44 @@ +import pyb, sensor, image, math +sensor.reset() +sensor.set_framesize(sensor.QVGA) +sensor.set_pixformat(sensor.RGB565) +red_threshold = (0,100, 0,127, 0,127) # L A B +green_threshold = (0,100, -128,0, 0,127) # L A B +blue_threshold = (0,100, -128,127, -128,0) # L A B +while(True): + # Test red threshold + for i in range(100): + img = sensor.snapshot() + img.binary([red_threshold]) + sum, x, y, rads = img.orientation_radians() + img.draw_keypoints([(x, y, rads)], color = (255, 0, 0), size = 20) + # Test green threshold + for i in range(100): + img = sensor.snapshot() + img.binary([green_threshold]) + sum, x, y, rads = img.orientation_radians() + img.draw_keypoints([(x, y, rads)], color = (0, 255, 0), size = 20) + # Test blue threshold + for i in range(100): + img = sensor.snapshot() + img.binary([blue_threshold]) + sum, x, y, rads = img.orientation_radians() + img.draw_keypoints([(x, y, rads)], color = (0, 0, 255), size = 20) + # Test not red threshold + for i in range(100): + img = sensor.snapshot() + img.binary([red_threshold], invert = 1) + sum, x, y, rads = img.orientation_radians() + img.draw_keypoints([(x, y, rads)], color = (255, 0, 0), size = 20) + # Test not green threshold + for i in range(100): + img = sensor.snapshot() + img.binary([green_threshold], invert = 1) + sum, x, y, rads = img.orientation_radians() + img.draw_keypoints([(x, y, rads)], color = (0, 255, 0), size = 20) + # Test not blue threshold + for i in range(100): + img = sensor.snapshot() + img.binary([blue_threshold], invert = 1) + sum, x, y, rads = img.orientation_radians() + img.draw_keypoints([(x, y, rads)], color = (0, 0, 255), size = 20) diff --git a/usr/tests/test_drawing_1.py b/usr/tests/test_drawing_1.py index d9b1ee5fa..b9bf7b662 100644 --- a/usr/tests/test_drawing_1.py +++ b/usr/tests/test_drawing_1.py @@ -1,4 +1,4 @@ -import pyb, sensor, image +import pyb, sensor, image, math sensor.reset() sensor.set_framesize(sensor.QVGA) while(True): @@ -102,3 +102,20 @@ while(True): x = (pyb.rng() % (2*img.width())) - (img.width()//2) y = (pyb.rng() % (2*img.height())) - (img.height()//2) img.draw_cross(x, y) + # Test Draw Keypoints + sensor.set_pixformat(sensor.GRAYSCALE) + for i in range(10): + img = sensor.snapshot() + for j in range(100): + x = (pyb.rng() % (2*img.width())) - (img.width()//2) + y = (pyb.rng() % (2*img.height())) - (img.height()//2) + a = (pyb.rng() % (2*math.pi)) + img.draw_keypoints([(x, y, a)]) + sensor.set_pixformat(sensor.RGB565) + for i in range(10): + img = sensor.snapshot() + for j in range(100): + x = (pyb.rng() % (2*img.width())) - (img.width()//2) + y = (pyb.rng() % (2*img.height())) - (img.height()//2) + a = (pyb.rng() % (2*math.pi)) + img.draw_keypoints([(x, y, a)])