Fix blob detection

This commit is contained in:
iabdalkader 2014-05-03 00:03:39 +02:00
parent 3c31c48d6b
commit 72c13e47d7
4 changed files with 23 additions and 21 deletions

View File

@ -216,10 +216,10 @@ void imlib_erosion_filter(struct image *src, uint8_t *kernel, int k_size)
({ uint16_t _x = (x); \ ({ uint16_t _x = (x); \
(((_x & 0xff)<<8 |(_x & 0xff00) >> 8));}) (((_x & 0xff)<<8 |(_x & 0xff00) >> 8));})
void imlib_threshold(image_t *image, struct color *color, int threshold) void imlib_threshold(image_t *src, image_t *dst, struct color *color, int threshold)
{ {
color_t lab1,lab2; color_t lab1,lab2;
uint16_t *pixels = (uint16_t*) image->pixels; uint16_t *pixels = (uint16_t*) src->pixels;
/* Convert reference RGB to LAB */ /* Convert reference RGB to LAB */
uint16_t r = color->r*31/255; uint16_t r = color->r*31/255;
@ -231,21 +231,20 @@ void imlib_threshold(image_t *image, struct color *color, int threshold)
lab1.A = lab_table[r*3+1]; lab1.A = lab_table[r*3+1];
lab1.B = lab_table[r*3+2]; lab1.B = lab_table[r*3+2];
for (int y=0; y<image->h; y++) { for (int y=0; y<src->h; y++) {
for (int x=0; x<image->w; x++) { for (int x=0; x<src->w; x++) {
int i=y*image->w+x; int i=y*src->w+x;
lab2.L = lab_table[pixels[i]*3]; lab2.L = lab_table[pixels[i]*3];
lab2.A = lab_table[pixels[i]*3+1]; lab2.A = lab_table[pixels[i]*3+1];
lab2.B = lab_table[pixels[i]*3+2]; lab2.B = lab_table[pixels[i]*3+2];
/* add pixel if within threshold */ /* add pixel if within threshold */
if (imlib_lab_distance(&lab1, &lab2)<threshold) { if (imlib_lab_distance(&lab1, &lab2)<threshold) {
image->pixels[i] = 0x01; dst->pixels[i] = 1;
} else { } else {
image->pixels[i] = 0x00; dst->pixels[i] = 0;
} }
} }
} }
image->bpp = 1;
} }
int imlib_image_mean(struct image *src) int imlib_image_mean(struct image *src)

View File

@ -182,7 +182,7 @@ void imlib_rgb_to_hsv(struct color *rgb, struct color *hsv);
void imlib_histeq(struct image *src); void imlib_histeq(struct image *src);
void imlib_median_filter(image_t *src, int r); void imlib_median_filter(image_t *src, int r);
void imlib_erosion_filter(struct image *src, uint8_t *kernel, int k_size); void imlib_erosion_filter(struct image *src, uint8_t *kernel, int k_size);
void imlib_threshold(struct image *image, struct color *color, int threshold); void imlib_threshold(struct image *src, struct image *dst, struct color *color, int threshold);
array_t *imlib_count_blobs(struct image *image); array_t *imlib_count_blobs(struct image *image);
/* Integral image functions */ /* Integral image functions */

View File

@ -170,9 +170,11 @@ static mp_obj_t py_image_threshold(mp_obj_t image_obj, mp_obj_t color_obj, mp_ob
/* C stuff */ /* C stuff */
struct color color; struct color color;
struct image *image; struct image *image;
mp_obj_t bimage_obj;
/* sanity checks */ /* sanity checks */
PY_ASSERT_TRUE(sensor.pixformat == PIXFORMAT_RGB565); PY_ASSERT_TRUE(sensor.pixformat == PIXFORMAT_RGB565);
PY_ASSERT_TRUE(sensor.framesize <= FRAMESIZE_QQVGA);
mp_obj_t *col_obj; mp_obj_t *col_obj;
mp_obj_get_array_fixed_n(color_obj, 3, &col_obj); mp_obj_get_array_fixed_n(color_obj, 3, &col_obj);
@ -183,10 +185,13 @@ static mp_obj_t py_image_threshold(mp_obj_t image_obj, mp_obj_t color_obj, mp_ob
/* get image pointer */ /* get image pointer */
image = py_image_cobj(image_obj); image = py_image_cobj(image_obj);
/* Threshold image using reference color */ /* returned image */
imlib_threshold(image, &color, mp_obj_get_int(threshold)); bimage_obj = py_image(image->w, image->h, 1, image->data+(image->w*image->h*image->bpp));
return mp_const_none; /* Threshold image using reference color */
imlib_threshold(image, py_image_cobj(bimage_obj), &color, mp_obj_get_int(threshold));
return bimage_obj;
} }
static mp_obj_t py_image_draw_circle(mp_obj_t image_obj, mp_obj_t c_obj, mp_obj_t r_obj) static mp_obj_t py_image_draw_circle(mp_obj_t image_obj, mp_obj_t c_obj, mp_obj_t r_obj)

View File

@ -5,15 +5,13 @@ while (True):
clock.tick() clock.tick()
# take snapshot # take snapshot
image = sensor.snapshot() image = sensor.snapshot()
#get a binary image
# detect blobs binary = image.threshold((255, 127, 127), 25)
image.threshold((255, 127, 127), 40) # run median filter
image.median(3) binary.median(3)
blobs = image.find_blobs() # detect blobs in image
blobs = binary.find_blobs()
# draw rectangles around detected blobs # draw rectangles around detected blobs
image = sensor.snapshot()
for r in blobs: for r in blobs:
image.draw_rectangle(r) image.draw_rectangle(r)
print(clock.fps()) print(clock.fps())