diff --git a/src/img/imlib.c b/src/img/imlib.c index 32615f1b8..7b58784c8 100644 --- a/src/img/imlib.c +++ b/src/img/imlib.c @@ -216,10 +216,10 @@ void imlib_erosion_filter(struct image *src, uint8_t *kernel, int k_size) ({ uint16_t _x = (x); \ (((_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; - uint16_t *pixels = (uint16_t*) image->pixels; + uint16_t *pixels = (uint16_t*) src->pixels; /* Convert reference RGB to LAB */ 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.B = lab_table[r*3+2]; - for (int y=0; yh; y++) { - for (int x=0; xw; x++) { - int i=y*image->w+x; + for (int y=0; yh; y++) { + for (int x=0; xw; x++) { + int i=y*src->w+x; lab2.L = lab_table[pixels[i]*3]; lab2.A = lab_table[pixels[i]*3+1]; lab2.B = lab_table[pixels[i]*3+2]; /* add pixel if within threshold */ if (imlib_lab_distance(&lab1, &lab2)pixels[i] = 0x01; + dst->pixels[i] = 1; } else { - image->pixels[i] = 0x00; + dst->pixels[i] = 0; } } } - image->bpp = 1; } int imlib_image_mean(struct image *src) diff --git a/src/img/imlib.h b/src/img/imlib.h index 0fd6c28fd..b60998e65 100644 --- a/src/img/imlib.h +++ b/src/img/imlib.h @@ -182,7 +182,7 @@ void imlib_rgb_to_hsv(struct color *rgb, struct color *hsv); void imlib_histeq(struct image *src); void imlib_median_filter(image_t *src, int r); 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); /* Integral image functions */ diff --git a/src/py/py_image.c b/src/py/py_image.c index b1b0514a8..801109c28 100644 --- a/src/py/py_image.c +++ b/src/py/py_image.c @@ -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 */ struct color color; struct image *image; + mp_obj_t bimage_obj; /* sanity checks */ PY_ASSERT_TRUE(sensor.pixformat == PIXFORMAT_RGB565); + PY_ASSERT_TRUE(sensor.framesize <= FRAMESIZE_QQVGA); mp_obj_t *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 */ image = py_image_cobj(image_obj); - /* Threshold image using reference color */ - imlib_threshold(image, &color, mp_obj_get_int(threshold)); + /* returned image */ + 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) diff --git a/usr/examples/blob_detection.py b/usr/examples/blob_detection.py index 211a12729..78c28f39d 100644 --- a/usr/examples/blob_detection.py +++ b/usr/examples/blob_detection.py @@ -5,15 +5,13 @@ while (True): clock.tick() # take snapshot image = sensor.snapshot() - - # detect blobs - image.threshold((255, 127, 127), 40) - image.median(3) - blobs = image.find_blobs() - + #get a binary image + binary = image.threshold((255, 127, 127), 25) + # run median filter + binary.median(3) + # detect blobs in image + blobs = binary.find_blobs() # draw rectangles around detected blobs - image = sensor.snapshot() for r in blobs: - image.draw_rectangle(r) - + image.draw_rectangle(r) print(clock.fps())