Merge pull request #75 from kwagyeman/master

Fix draw_keypoints and add drawing examples.
This commit is contained in:
Ibrahim Abd Elkader 2016-02-21 02:16:44 +02:00
commit ba62fddd00
5 changed files with 124 additions and 14 deletions

View File

@ -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;
}
}
}

View File

@ -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; i<arg_vec_len; i++) {
mp_obj_t *arg_keypoint;
mp_obj_get_array_fixed_n(arg_vec[i], 3, &arg_keypoint);
int x = mp_obj_get_int(arg_keypoint[0]);
int y = mp_obj_get_int(arg_keypoint[1]);
float angle = mp_obj_get_float(arg_keypoint[2]);
float co = arm_cos_f32(angle);
float si = arm_sin_f32(angle);
imlib_draw_line(arg_img, x, y, x+(co*arg_s), y+(si*arg_s), arg_c);
imlib_draw_circle(arg_img, x, y, (arg_s-2)/2, arg_c);
}
} else {
mp_obj_t kpts_obj = args[1];
PY_ASSERT_TYPE(kpts_obj, &py_kp_type);
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);
}
}
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)) {

View File

@ -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)

View File

@ -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)

View File

@ -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)])