From 9e5d379c1893cd6bdd58e10cfc95dc9c42847197 Mon Sep 17 00:00:00 2001 From: "Kwabena W. Agyeman" Date: Sat, 9 Apr 2016 12:36:44 -0400 Subject: [PATCH] Remove old code. Pixels, centroid, and orientation are calculated in the blob code now. As for threshold, it is no longer needed (plus, it required storing a secondary image in RAM which isn't really something we can handle). --- src/omv/img/imlib.c | 191 ------------------ src/omv/img/imlib.h | 5 - src/omv/py/py_image.c | 111 ---------- src/omv/py/qstrdefsomv.h | 6 +- .../05-Snapshot/snapshot_on_movement.py | 8 +- .../06-Video-Recording/gif_on_movement.py | 8 +- .../06-Video-Recording/mjpeg_on_movement.py | 8 +- usr/examples/15-Tests/test_binary_1.py | 8 - usr/examples/15-Tests/test_binary_2.py | 12 -- 9 files changed, 13 insertions(+), 344 deletions(-) diff --git a/src/omv/img/imlib.c b/src/omv/img/imlib.c index 00134ffe4..dd7a5a218 100644 --- a/src/omv/img/imlib.c +++ b/src/omv/img/imlib.c @@ -568,111 +568,6 @@ void imlib_xnor(image_t *img, const char *path, image_t *other) imlib_image_operation(img, path, other, imlib_xnor_line_op); } -int imlib_pixels(image_t *img, rectangle_t *r) -{ - rectangle_t rect; - if (!rectangle_subimg(img, r, &rect)) { - return 0; - } - - int sum = 0; - if (IM_IS_GS(img)) { - for (int i = 0; i < rect.h; i++) { - for (int j = 0; j < rect.w; j++) { - sum += !!IM_GET_GS_PIXEL(img, (rect.x + j), (rect.y + i)); - } - } - } else { - for (int i = 0; i < rect.h; i++) { - for (int j = 0; j < rect.w; j++) { - sum += !!IM_GET_RGB565_PIXEL(img, (rect.x + j), (rect.y + i)); - } - } - } - return sum; -} - -int imlib_centroid(image_t *img, int *x_center, int *y_center, rectangle_t *r) -{ - rectangle_t rect; - if (!rectangle_subimg(img, r, &rect)) { - return 0; - } - - int sum = 0, x_sum = 0, y_sum = 0; - if (IM_IS_GS(img)) { - for (int i = 0; i < rect.h; i++) { - for (int j = 0; j < rect.w; j++) { - int x = (rect.x + j); - int y = (rect.y + i); - if(IM_GET_GS_PIXEL(img, x, y)) { - sum += 1; - x_sum += x; - y_sum += y; - } - } - } - } else { - for (int i = 0; i < rect.h; i++) { - for (int j = 0; j < rect.w; j++) { - int x = (rect.x + j); - int y = (rect.y + i); - if(IM_GET_RGB565_PIXEL(img, x, y)) { - sum += 1; - x_sum += x; - y_sum += y; - } - } - } - } - *x_center = sum ? (x_sum / sum) : 0; - *y_center = sum ? (y_sum / sum) : 0; - return sum; -} - -float imlib_orientation_radians(image_t *img, int *sum, int *x_center, int *y_center, rectangle_t *r) -{ - rectangle_t rect; - if (!rectangle_subimg(img, r, &rect)) { - return 0; - } - - *sum = imlib_centroid(img, x_center, y_center, r); - int a = 0, b = 0, c = 0; - if (IM_IS_GS(img)) { - for (int i = 0; i < rect.h; i++) { - for (int j = 0; j < rect.w; j++) { - int x = (rect.x + j); - int y = (rect.y + i); - if(IM_GET_GS_PIXEL(img, x, y)) { - a += (x - *x_center) * (x - *x_center); - b += (x - *x_center) * (y - *y_center); - c += (y - *y_center) * (y - *y_center); - } - } - } - } else { - for (int i = 0; i < rect.h; i++) { - for (int j = 0; j < rect.w; j++) { - int x = (rect.x + j); - int y = (rect.y + i); - if(IM_GET_RGB565_PIXEL(img, x, y)) { - a += (x - *x_center) * (x - *x_center); - b += (x - *x_center) * (y - *y_center); - c += (y - *y_center) * (y - *y_center); - } - } - } - } - b *= 2; - return ((a!=c) ? fast_atan2f(b, a-c) : 0.0) / 2.0; -} - -float imlib_orientation_degrees(image_t *img, int *sum, int *x_center, int *y_center, rectangle_t *r) -{ - return (imlib_orientation_radians(img, sum, x_center, y_center, r) * 180.0) / M_PI; -} - static void imlib_erode_dilate(image_t *img, int ksize, int threshold, int e_or_d) { int brows = ksize + 1; @@ -818,92 +713,6 @@ void imlib_difference(image_t *img, const char *path, image_t *other) //////////////////////////////////////////////////////////////////////////////// -#ifdef OPENMV1 -void imlib_threshold(image_t *src, image_t *dst, color_t *color, int color_size, int threshold) -{ - struct color rgb, lab; - // Square the threshold to avoid sqrt - threshold *= threshold; - - // Convert the RGB888 color list to LAB - for (int c=0; cpixels; - - for (int y=0; yh; y++) { - int i=y*src->w; - for (int x=0; xw; x++) { - uint32_t p = pixels[i+x]; - rgb.r = ((p>>3)&0x1F)*255/31; - rgb.g = ((p&0x07)<<3)|(p>>13)*255/63; - rgb.b = ((p>>8)&0x1F)*255/31; - imlib_rgb_to_lab(&rgb, &lab); - - dst->pixels[i+x] = 0; - for (int c=0; cpixels[i+x] = c+1; - break; - } - } - } - } -} -#else -void imlib_threshold(image_t *src, image_t *dst, color_t *color, int color_size, int threshold) -{ - // Square the threshold to avoid sqrt - threshold *= threshold; - - // Convert the RGB888 color list to LAB - for (int c=0; cpixels; - - for (int y=0; yh; y++) { - int i=y*src->w; - for (int x=0; xw; x++) { - // multiply by 3 to get the LAB table index - uint32_t rgb = pixels[i+x]*3; - dst->pixels[i+x] = 0; - for (int c=0; cpixels[i+x] = c+1; - break; - } - } - } - } -} -#endif - void imlib_rainbow(image_t *src, image_t *dst) { uint8_t *srcp = src->pixels; diff --git a/src/omv/img/imlib.h b/src/omv/img/imlib.h index 3cc192177..85b1c21c0 100644 --- a/src/omv/img/imlib.h +++ b/src/omv/img/imlib.h @@ -434,10 +434,6 @@ void imlib_or(image_t *img, const char *path, image_t *other); void imlib_nor(image_t *img, const char *path, image_t *other); void imlib_xor(image_t *img, const char *path, image_t *other); void imlib_xnor(image_t *img, const char *path, image_t *other); -int imlib_pixels(image_t *img, rectangle_t *r); -int imlib_centroid(image_t *img, int *x_center, int *y_center, rectangle_t *r); -float imlib_orientation_radians(image_t *img, int *sum, int *x_center, int *y_center, rectangle_t *r); -float imlib_orientation_degrees(image_t *img, int *sum, int *x_center, int *y_center, rectangle_t *r); void imlib_erode(image_t *img, int ksize, int threshold); void imlib_dilate(image_t *img, int ksize, int threshold); @@ -468,7 +464,6 @@ array_t *cluster_kmeans(array_t *points, int k); /* Image filtering functions */ int imlib_image_mean(struct image *src); void imlib_histeq(struct image *src); -void imlib_threshold(image_t *src, image_t *dst, color_t *color, int color_size, int threshold); void imlib_rainbow(image_t *src, struct image *dst); /* Integral image functions */ diff --git a/src/omv/py/py_image.c b/src/omv/py/py_image.c index a20dbbffd..67417f3e1 100644 --- a/src/omv/py/py_image.c +++ b/src/omv/py/py_image.c @@ -518,62 +518,6 @@ static mp_obj_t py_image_xnor(mp_obj_t img_obj, mp_obj_t other_obj) return mp_const_none; } -static mp_obj_t py_image_pixels(uint n_args, const mp_obj_t *args, mp_map_t *kw_args) -{ - image_t *arg_img = py_image_cobj(args[0]); - PY_ASSERT_FALSE_MSG(IM_IS_JPEG(arg_img), - "Operation not supported on JPEG"); - - rectangle_t arg_r; - py_helper_lookup_rectangle(kw_args, arg_img, &arg_r); - return mp_obj_new_int(imlib_pixels(arg_img, &arg_r)); -} - -static mp_obj_t py_image_centroid(uint n_args, const mp_obj_t *args, mp_map_t *kw_args) -{ - image_t *arg_img = py_image_cobj(args[0]); - PY_ASSERT_FALSE_MSG(IM_IS_JPEG(arg_img), - "Operation not supported on JPEG"); - - rectangle_t arg_r; - py_helper_lookup_rectangle(kw_args, arg_img, &arg_r); - int x = 0, y = 0; - int sum = imlib_centroid(arg_img, &x, &y, &arg_r); - - return mp_obj_new_tuple(3, (mp_obj_t[3]) - {mp_obj_new_int(sum), mp_obj_new_int(x), mp_obj_new_int(y)}); -} - -static mp_obj_t py_image_orientation_radians(uint n_args, const mp_obj_t *args, mp_map_t *kw_args) -{ - image_t *arg_img = py_image_cobj(args[0]); - PY_ASSERT_FALSE_MSG(IM_IS_JPEG(arg_img), - "Operation not supported on JPEG"); - - rectangle_t arg_r; - py_helper_lookup_rectangle(kw_args, arg_img, &arg_r); - int sum = 0, x = 0, y = 0; - float o = imlib_orientation_radians(arg_img, &sum, &x, &y, &arg_r); - - return mp_obj_new_tuple(4, (mp_obj_t[4]) - {mp_obj_new_int(sum), mp_obj_new_int(x), mp_obj_new_int(y), mp_obj_new_float(o)}); -} - -static mp_obj_t py_image_orientation_degrees(uint n_args, const mp_obj_t *args, mp_map_t *kw_args) -{ - image_t *arg_img = py_image_cobj(args[0]); - PY_ASSERT_FALSE_MSG(IM_IS_JPEG(arg_img), - "Operation not supported on JPEG"); - - rectangle_t arg_r; - py_helper_lookup_rectangle(kw_args, arg_img, &arg_r); - int sum = 0, x = 0, y = 0; - float o = imlib_orientation_degrees(arg_img, &sum, &x, &y, &arg_r); - - return mp_obj_new_tuple(4, (mp_obj_t[4]) - {mp_obj_new_int(sum), mp_obj_new_int(x), mp_obj_new_int(y), mp_obj_new_float(o)}); -} - static mp_obj_t py_image_erode(uint n_args, const mp_obj_t *args, mp_map_t *kw_args) { image_t *arg_img = py_image_cobj(args[0]); @@ -968,51 +912,6 @@ static mp_obj_t py_image_histeq(mp_obj_t image_obj) return mp_const_none; } -static mp_obj_t py_image_threshold(mp_obj_t image_obj, mp_obj_t color_list_obj, mp_obj_t threshold) -{ - color_t *color; - image_t *image; - - /* sanity checks */ - PY_ASSERT_TRUE_MSG(sensor.pixformat == PIXFORMAT_RGB565, - "This function is only supported on RGB565 images"); - - PY_ASSERT_TRUE_MSG(sensor.framesize <= OMV_MAX_BLOB_FRAME, - "This function is only supported on "OMV_MAX_BLOB_FRAME_STR" and smaller frames"); - - /* read arguments */ - image = py_image_cobj(image_obj); - int thresh = mp_obj_get_int(threshold); - - /* returned image */ - image_t bimage = { - .w=image->w, - .h=image->h, - .bpp=1, - .pixels=image->data+(image->w*image->h*image->bpp) - }; - - /* copy color list */ - uint len; - mp_obj_t *color_arr; - mp_obj_get_array(color_list_obj, &len, &color_arr); - - color = xalloc(len*sizeof*color); - - for (int i=0; i 100: # Over 100 pixels need to change to detect motion. - diff -= 1 + for blob_l in img.find_blobs([(20, 100, -128, 127, -128, 127)]): + for blob in blob_l: + # Over 100 pixels need to change to detect motion. + if (diff and (blob[4] > 100)): diff -= 1 pyb.LED(BLUE_LED_PIN).off() print("Movement detected! Saving image...") diff --git a/usr/examples/06-Video-Recording/gif_on_movement.py b/usr/examples/06-Video-Recording/gif_on_movement.py index 6cf0c0d1b..125b69205 100644 --- a/usr/examples/06-Video-Recording/gif_on_movement.py +++ b/usr/examples/06-Video-Recording/gif_on_movement.py @@ -37,10 +37,10 @@ while(True): while(diff): img = sensor.snapshot() img.difference("temp/bg.bmp") - img.binary([(20, 100, -128, 127, -128, 127)]) - sum = img.pixels() - if sum > 100: # Over 100 pixels need to change to detect motion. - diff -= 1 + for blob_l in img.find_blobs([(20, 100, -128, 127, -128, 127)]): + for blob in blob_l: + # Over 100 pixels need to change to detect motion. + if (diff and (blob[4] > 100)): diff -= 1 g = gif.Gif("example-%d.gif" % pyb.rng(), loop=True) diff --git a/usr/examples/06-Video-Recording/mjpeg_on_movement.py b/usr/examples/06-Video-Recording/mjpeg_on_movement.py index 2bce47814..f0019d55c 100644 --- a/usr/examples/06-Video-Recording/mjpeg_on_movement.py +++ b/usr/examples/06-Video-Recording/mjpeg_on_movement.py @@ -38,10 +38,10 @@ while(True): while(diff): img = sensor.snapshot() img.difference("temp/bg.bmp") - img.binary([(20, 100, -128, 127, -128, 127)]) - sum = img.pixels() - if sum > 100: # Over 100 pixels need to change to detect motion. - diff -= 1 + for blob_l in img.find_blobs([(20, 100, -128, 127, -128, 127)]): + for blob in blob_l: + # Over 100 pixels need to change to detect motion. + if (diff and (blob[4] > 100)): diff -= 1 m = mjpeg.Mjpeg("example-%d.mjpeg" % pyb.rng()) diff --git a/usr/examples/15-Tests/test_binary_1.py b/usr/examples/15-Tests/test_binary_1.py index 5cbae36cf..cac1fc7bb 100644 --- a/usr/examples/15-Tests/test_binary_1.py +++ b/usr/examples/15-Tests/test_binary_1.py @@ -9,23 +9,15 @@ while(True): 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/examples/15-Tests/test_binary_2.py b/usr/examples/15-Tests/test_binary_2.py index 3bf1a4281..1e3c0266f 100644 --- a/usr/examples/15-Tests/test_binary_2.py +++ b/usr/examples/15-Tests/test_binary_2.py @@ -10,35 +10,23 @@ while(True): 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)