From 055a468b8311300974260aa96897fccaf6df4c01 Mon Sep 17 00:00:00 2001 From: "Kwabena W. Agyeman" Date: Mon, 12 Feb 2024 20:27:41 -0800 Subject: [PATCH] imlib: Remove cartoon() method. --- .../01-Image-Filters/cartoon_filter.py | 34 ---- src/omv/boards/ARDUINO_GIGA/imlib_config.h | 3 - .../ARDUINO_NANO_33_BLE_SENSE/imlib_config.h | 3 - .../imlib_config.h | 3 - .../ARDUINO_NICLA_VISION/imlib_config.h | 3 - .../boards/ARDUINO_PORTENTA_H7/imlib_config.h | 3 - src/omv/boards/OPENMV2/imlib_config.h | 3 - src/omv/boards/OPENMV3/imlib_config.h | 3 - src/omv/boards/OPENMV4/imlib_config.h | 3 - src/omv/boards/OPENMV4P/imlib_config.h | 3 - src/omv/boards/OPENMV4_PRO/imlib_config.h | 3 - src/omv/boards/OPENMVPT/imlib_config.h | 3 - src/omv/boards/OPENMV_RT1060/imlib_config.h | 3 - src/omv/boards/PICO/imlib_config.h | 3 - src/omv/imlib/filter.c | 173 ------------------ src/omv/imlib/imlib.h | 1 - src/omv/modules/py_image.c | 28 --- 17 files changed, 275 deletions(-) delete mode 100644 scripts/examples/02-Image-Processing/01-Image-Filters/cartoon_filter.py diff --git a/scripts/examples/02-Image-Processing/01-Image-Filters/cartoon_filter.py b/scripts/examples/02-Image-Processing/01-Image-Filters/cartoon_filter.py deleted file mode 100644 index 6899ca996..000000000 --- a/scripts/examples/02-Image-Processing/01-Image-Filters/cartoon_filter.py +++ /dev/null @@ -1,34 +0,0 @@ -# This work is licensed under the MIT license. -# Copyright (c) 2013-2023 OpenMV LLC. All rights reserved. -# https://github.com/openmv/openmv/blob/master/LICENSE -# -# Cartoon Filter -# -# This example shows off a simple cartoon filter on images. The cartoon -# filter works by joining similar pixel areas of an image and replacing -# the pixels in those areas with the area mean. - -import sensor -import time - -sensor.reset() -sensor.set_pixformat(sensor.RGB565) # or GRAYSCALE... -sensor.set_framesize(sensor.QVGA) # or QQVGA... -sensor.skip_frames(time=2000) -clock = time.clock() - -while True: - clock.tick() - - # seed_threshold controls the maximum area growth of a colored - # region. Making this larger will merge more pixels. - - # floating_threshold controls the maximum pixel-to-pixel difference - # when growing a region. Settings this very high will quickly combine - # all pixels in the image. You should keep this small. - - # cartoon() will grow regions while both thresholds are satisfied... - - img = sensor.snapshot().cartoon(seed_threshold=0.05, floating_thresholds=0.05) - - print(clock.fps()) diff --git a/src/omv/boards/ARDUINO_GIGA/imlib_config.h b/src/omv/boards/ARDUINO_GIGA/imlib_config.h index dcbebd64e..864bcd0e5 100644 --- a/src/omv/boards/ARDUINO_GIGA/imlib_config.h +++ b/src/omv/boards/ARDUINO_GIGA/imlib_config.h @@ -65,9 +65,6 @@ // Enable bilateral() //#define IMLIB_ENABLE_BILATERAL -// Enable cartoon() -// #define IMLIB_ENABLE_CARTOON - // Enable linpolar() #define IMLIB_ENABLE_LINPOLAR diff --git a/src/omv/boards/ARDUINO_NANO_33_BLE_SENSE/imlib_config.h b/src/omv/boards/ARDUINO_NANO_33_BLE_SENSE/imlib_config.h index 6260080e2..aa5125b0a 100644 --- a/src/omv/boards/ARDUINO_NANO_33_BLE_SENSE/imlib_config.h +++ b/src/omv/boards/ARDUINO_NANO_33_BLE_SENSE/imlib_config.h @@ -66,9 +66,6 @@ // Enable bilateral() //#define IMLIB_ENABLE_BILATERAL -// Enable cartoon() -// #define IMLIB_ENABLE_CARTOON - // Enable linpolar() //#define IMLIB_ENABLE_LINPOLAR diff --git a/src/omv/boards/ARDUINO_NANO_RP2040_CONNECT/imlib_config.h b/src/omv/boards/ARDUINO_NANO_RP2040_CONNECT/imlib_config.h index 86cbcee0e..31fb76237 100644 --- a/src/omv/boards/ARDUINO_NANO_RP2040_CONNECT/imlib_config.h +++ b/src/omv/boards/ARDUINO_NANO_RP2040_CONNECT/imlib_config.h @@ -65,9 +65,6 @@ // Enable bilateral() //#define IMLIB_ENABLE_BILATERAL -// Enable cartoon() -// #define IMLIB_ENABLE_CARTOON - // Enable linpolar() //#define IMLIB_ENABLE_LINPOLAR diff --git a/src/omv/boards/ARDUINO_NICLA_VISION/imlib_config.h b/src/omv/boards/ARDUINO_NICLA_VISION/imlib_config.h index 86594895b..416de40f0 100644 --- a/src/omv/boards/ARDUINO_NICLA_VISION/imlib_config.h +++ b/src/omv/boards/ARDUINO_NICLA_VISION/imlib_config.h @@ -65,9 +65,6 @@ // Enable bilateral() #define IMLIB_ENABLE_BILATERAL -// Enable cartoon() -// #define IMLIB_ENABLE_CARTOON - // Enable linpolar() #define IMLIB_ENABLE_LINPOLAR diff --git a/src/omv/boards/ARDUINO_PORTENTA_H7/imlib_config.h b/src/omv/boards/ARDUINO_PORTENTA_H7/imlib_config.h index 4e8d34d00..d6af847e2 100644 --- a/src/omv/boards/ARDUINO_PORTENTA_H7/imlib_config.h +++ b/src/omv/boards/ARDUINO_PORTENTA_H7/imlib_config.h @@ -65,9 +65,6 @@ // Enable bilateral() //#define IMLIB_ENABLE_BILATERAL -// Enable cartoon() -// #define IMLIB_ENABLE_CARTOON - // Enable linpolar() #define IMLIB_ENABLE_LINPOLAR diff --git a/src/omv/boards/OPENMV2/imlib_config.h b/src/omv/boards/OPENMV2/imlib_config.h index ed4fafd9d..269319c81 100644 --- a/src/omv/boards/OPENMV2/imlib_config.h +++ b/src/omv/boards/OPENMV2/imlib_config.h @@ -62,9 +62,6 @@ // Enable bilateral() //#define IMLIB_ENABLE_BILATERAL -// Enable cartoon() -//#define IMLIB_ENABLE_CARTOON - // Enable linpolar() //#define IMLIB_ENABLE_LINPOLAR diff --git a/src/omv/boards/OPENMV3/imlib_config.h b/src/omv/boards/OPENMV3/imlib_config.h index 271c13697..2bfe9d876 100644 --- a/src/omv/boards/OPENMV3/imlib_config.h +++ b/src/omv/boards/OPENMV3/imlib_config.h @@ -65,9 +65,6 @@ // Enable bilateral() #define IMLIB_ENABLE_BILATERAL -// Enable cartoon() -// #define IMLIB_ENABLE_CARTOON - // Enable linpolar() #define IMLIB_ENABLE_LINPOLAR diff --git a/src/omv/boards/OPENMV4/imlib_config.h b/src/omv/boards/OPENMV4/imlib_config.h index 31eeaf43b..192e93b9d 100644 --- a/src/omv/boards/OPENMV4/imlib_config.h +++ b/src/omv/boards/OPENMV4/imlib_config.h @@ -65,9 +65,6 @@ // Enable bilateral() #define IMLIB_ENABLE_BILATERAL -// Enable cartoon() -// #define IMLIB_ENABLE_CARTOON - // Enable linpolar() #define IMLIB_ENABLE_LINPOLAR diff --git a/src/omv/boards/OPENMV4P/imlib_config.h b/src/omv/boards/OPENMV4P/imlib_config.h index 88d153100..06f981c0c 100644 --- a/src/omv/boards/OPENMV4P/imlib_config.h +++ b/src/omv/boards/OPENMV4P/imlib_config.h @@ -65,9 +65,6 @@ // Enable bilateral() #define IMLIB_ENABLE_BILATERAL -// Enable cartoon() -// #define IMLIB_ENABLE_CARTOON - // Enable linpolar() #define IMLIB_ENABLE_LINPOLAR diff --git a/src/omv/boards/OPENMV4_PRO/imlib_config.h b/src/omv/boards/OPENMV4_PRO/imlib_config.h index 88d153100..06f981c0c 100644 --- a/src/omv/boards/OPENMV4_PRO/imlib_config.h +++ b/src/omv/boards/OPENMV4_PRO/imlib_config.h @@ -65,9 +65,6 @@ // Enable bilateral() #define IMLIB_ENABLE_BILATERAL -// Enable cartoon() -// #define IMLIB_ENABLE_CARTOON - // Enable linpolar() #define IMLIB_ENABLE_LINPOLAR diff --git a/src/omv/boards/OPENMVPT/imlib_config.h b/src/omv/boards/OPENMVPT/imlib_config.h index 88d153100..06f981c0c 100644 --- a/src/omv/boards/OPENMVPT/imlib_config.h +++ b/src/omv/boards/OPENMVPT/imlib_config.h @@ -65,9 +65,6 @@ // Enable bilateral() #define IMLIB_ENABLE_BILATERAL -// Enable cartoon() -// #define IMLIB_ENABLE_CARTOON - // Enable linpolar() #define IMLIB_ENABLE_LINPOLAR diff --git a/src/omv/boards/OPENMV_RT1060/imlib_config.h b/src/omv/boards/OPENMV_RT1060/imlib_config.h index adbc6f4b3..358c2d5c3 100644 --- a/src/omv/boards/OPENMV_RT1060/imlib_config.h +++ b/src/omv/boards/OPENMV_RT1060/imlib_config.h @@ -64,9 +64,6 @@ // Enable bilateral() #define IMLIB_ENABLE_BILATERAL -// Enable cartoon() -// #define IMLIB_ENABLE_CARTOON - // Enable linpolar() #define IMLIB_ENABLE_LINPOLAR diff --git a/src/omv/boards/PICO/imlib_config.h b/src/omv/boards/PICO/imlib_config.h index 867020f4c..75551d25e 100644 --- a/src/omv/boards/PICO/imlib_config.h +++ b/src/omv/boards/PICO/imlib_config.h @@ -66,9 +66,6 @@ // Enable bilateral() //#define IMLIB_ENABLE_BILATERAL -// Enable cartoon() -// #define IMLIB_ENABLE_CARTOON - // Enable linpolar() //#define IMLIB_ENABLE_LINPOLAR diff --git a/src/omv/imlib/filter.c b/src/omv/imlib/filter.c index 999f2b650..48d0d3473 100644 --- a/src/omv/imlib/filter.c +++ b/src/omv/imlib/filter.c @@ -2078,176 +2078,3 @@ void imlib_bilateral_filter(image_t *img, } } #endif // IMLIB_ENABLE_BILATERAL - -#ifdef IMLIB_ENABLE_CARTOON -typedef struct imlib_cartoon_filter_mean_state { - int r_acc, g_acc, b_acc, pixels; -} imlib_cartoon_filter_mean_state_t; - -static void imlib_cartoon_filter_mean(image_t *img, int line, int l, int r, void *data) { - imlib_cartoon_filter_mean_state_t *state = (imlib_cartoon_filter_mean_state_t *) data; - - switch (img->pixfmt) { - case PIXFORMAT_BINARY: { - uint32_t *row_ptr = IMAGE_COMPUTE_BINARY_PIXEL_ROW_PTR(img, line); - for (int i = l; i <= r; i++) { - state->g_acc += IMAGE_GET_BINARY_PIXEL_FAST(row_ptr, i); - state->pixels += 1; - } - break; - } - case PIXFORMAT_GRAYSCALE: { - uint8_t *row_ptr = IMAGE_COMPUTE_GRAYSCALE_PIXEL_ROW_PTR(img, line); - for (int i = l; i <= r; i++) { - state->g_acc += IMAGE_GET_GRAYSCALE_PIXEL_FAST(row_ptr, i); - state->pixels += 1; - } - break; - } - case PIXFORMAT_RGB565: { - uint16_t *row_ptr = IMAGE_COMPUTE_RGB565_PIXEL_ROW_PTR(img, line); - for (int i = l; i <= r; i++) { - int pixel = IMAGE_GET_RGB565_PIXEL_FAST(row_ptr, i); - state->r_acc += COLOR_RGB565_TO_R5(pixel); - state->g_acc += COLOR_RGB565_TO_G6(pixel); - state->b_acc += COLOR_RGB565_TO_B5(pixel); - state->pixels += 1; - } - break; - } - default: { - break; - } - } -} - -typedef struct imlib_cartoon_filter_fill_state { - int mean; -} imlib_cartoon_filter_fill_state_t; - -static void imlib_cartoon_filter_fill(image_t *img, int line, int l, int r, void *data) { - imlib_cartoon_filter_fill_state_t *state = (imlib_cartoon_filter_fill_state_t *) data; - - switch (img->pixfmt) { - case PIXFORMAT_BINARY: { - uint32_t *row_ptr = IMAGE_COMPUTE_BINARY_PIXEL_ROW_PTR(img, line); - for (int i = l; i <= r; i++) { - IMAGE_PUT_BINARY_PIXEL_FAST(row_ptr, i, state->mean); - } - break; - } - case PIXFORMAT_GRAYSCALE: { - uint8_t *row_ptr = IMAGE_COMPUTE_GRAYSCALE_PIXEL_ROW_PTR(img, line); - for (int i = l; i <= r; i++) { - IMAGE_PUT_GRAYSCALE_PIXEL_FAST(row_ptr, i, state->mean); - } - break; - } - case PIXFORMAT_RGB565: { - uint16_t *row_ptr = IMAGE_COMPUTE_RGB565_PIXEL_ROW_PTR(img, line); - for (int i = l; i <= r; i++) { - IMAGE_PUT_RGB565_PIXEL_FAST(row_ptr, i, state->mean); - } - break; - } - default: { - break; - } - } -} - -void imlib_cartoon_filter(image_t *img, float seed_threshold, float floating_threshold, image_t *mask) { - image_t mean_image, fill_image; - - mean_image.w = img->w; - mean_image.h = img->h; - mean_image.pixfmt = PIXFORMAT_BINARY; - mean_image.pixels = fb_alloc0(image_size(&mean_image), FB_ALLOC_NO_HINT); - - fill_image.w = img->w; - fill_image.h = img->h; - fill_image.pixfmt = PIXFORMAT_BINARY; - fill_image.pixels = fb_alloc0(image_size(&fill_image), FB_ALLOC_NO_HINT); - - if (mask) { - for (int y = 0, yy = fill_image.h; y < yy; y++) { - uint32_t *row_ptr = IMAGE_COMPUTE_BINARY_PIXEL_ROW_PTR(&fill_image, y); - for (int x = 0, xx = fill_image.w; x < xx; x++) { - if (image_get_mask_pixel(mask, x, y)) { - IMAGE_SET_BINARY_PIXEL_FAST(row_ptr, x); - } - } - } - } - - int color_seed_threshold = 0; - int color_floating_threshold = 0; - - switch (img->pixfmt) { - case PIXFORMAT_BINARY: { - color_seed_threshold = fast_floorf(seed_threshold * COLOR_BINARY_MAX); - color_floating_threshold = fast_floorf(floating_threshold * COLOR_BINARY_MAX); - break; - } - case PIXFORMAT_GRAYSCALE: { - color_seed_threshold = fast_floorf(seed_threshold * COLOR_GRAYSCALE_MAX); - color_floating_threshold = fast_floorf(floating_threshold * COLOR_GRAYSCALE_MAX); - break; - } - case PIXFORMAT_RGB565: { - color_seed_threshold = COLOR_R5_G6_B5_TO_RGB565(fast_floorf(seed_threshold * COLOR_R5_MAX), - fast_floorf(seed_threshold * COLOR_G6_MAX), - fast_floorf(seed_threshold * COLOR_B5_MAX)); - color_floating_threshold = COLOR_R5_G6_B5_TO_RGB565(fast_floorf(floating_threshold * COLOR_R5_MAX), - fast_floorf(floating_threshold * COLOR_G6_MAX), - fast_floorf(floating_threshold * COLOR_B5_MAX)); - break; - } - default: { - break; - } - } - - for (int y = 0, yy = img->h; y < yy; y++) { - uint32_t *row_ptr = IMAGE_COMPUTE_BINARY_PIXEL_ROW_PTR(&mean_image, y); - for (int x = 0, xx = img->w; x < xx; x++) { - if (!IMAGE_GET_BINARY_PIXEL_FAST(row_ptr, x)) { - - imlib_cartoon_filter_mean_state_t mean_state; - memset(&mean_state, 0, sizeof(imlib_cartoon_filter_mean_state_t)); - imlib_flood_fill_int(&mean_image, img, x, y, color_seed_threshold, color_floating_threshold, - imlib_cartoon_filter_mean, &mean_state); - - imlib_cartoon_filter_fill_state_t fill_state; - memset(&fill_state, 0, sizeof(imlib_cartoon_filter_fill_state_t)); - - switch (img->pixfmt) { - case PIXFORMAT_BINARY: { - fill_state.mean = mean_state.g_acc / mean_state.pixels; - break; - } - case PIXFORMAT_GRAYSCALE: { - fill_state.mean = mean_state.g_acc / mean_state.pixels; - break; - } - case PIXFORMAT_RGB565: { - fill_state.mean = COLOR_R5_G6_B5_TO_RGB565(mean_state.r_acc / mean_state.pixels, - mean_state.g_acc / mean_state.pixels, - mean_state.b_acc / mean_state.pixels); - break; - } - default: { - break; - } - } - - imlib_flood_fill_int(&fill_image, img, x, y, color_seed_threshold, color_floating_threshold, - imlib_cartoon_filter_fill, &fill_state); - } - } - } - - fb_free(); - fb_free(); -} -#endif // IMLIB_ENABLE_CARTOON diff --git a/src/omv/imlib/imlib.h b/src/omv/imlib/imlib.h index 01a93bbe0..03d32e8b4 100644 --- a/src/omv/imlib/imlib.h +++ b/src/omv/imlib/imlib.h @@ -1422,7 +1422,6 @@ void imlib_bilateral_filter(image_t *img, int offset, bool invert, image_t *mask); -void imlib_cartoon_filter(image_t *img, float seed_threshold, float floating_threshold, image_t *mask); // Image Correction void imlib_logpolar_int(image_t *dst, image_t *src, rectangle_t *roi, bool linear, bool reverse); // helper/internal void imlib_logpolar(image_t *img, bool linear, bool reverse); diff --git a/src/omv/modules/py_image.c b/src/omv/modules/py_image.c index 78ef5b88e..d791f6c57 100644 --- a/src/omv/modules/py_image.c +++ b/src/omv/modules/py_image.c @@ -2788,29 +2788,6 @@ STATIC mp_obj_t py_image_bilateral(uint n_args, const mp_obj_t *args, mp_map_t * STATIC MP_DEFINE_CONST_FUN_OBJ_KW(py_image_bilateral_obj, 2, py_image_bilateral); #endif // IMLIB_ENABLE_BILATERAL -#ifdef IMLIB_ENABLE_CARTOON -STATIC mp_obj_t py_image_cartoon(uint n_args, const mp_obj_t *args, mp_map_t *kw_args) { - image_t *arg_img = - py_helper_arg_to_image(args[0], ARG_IMAGE_MUTABLE); - float arg_seed_threshold = - py_helper_keyword_float(n_args, args, 1, kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_seed_threshold), 0.05); - PY_ASSERT_TRUE_MSG((0.0f <= arg_seed_threshold) && (arg_seed_threshold <= 1.0f), - "Error: 0.0 <= seed_threshold <= 1.0!"); - float arg_floating_threshold = - py_helper_keyword_float(n_args, args, 2, kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_floating_threshold), 0.05); - PY_ASSERT_TRUE_MSG((0.0f <= arg_floating_threshold) && (arg_floating_threshold <= 1.0f), - "Error: 0.0 <= floating_threshold <= 1.0!"); - image_t *arg_msk = - py_helper_keyword_to_image(n_args, args, 3, kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_mask), NULL); - - fb_alloc_mark(); - imlib_cartoon_filter(arg_img, arg_seed_threshold, arg_floating_threshold, arg_msk); - fb_alloc_free_till_mark(); - return args[0]; -} -STATIC MP_DEFINE_CONST_FUN_OBJ_KW(py_image_cartoon_obj, 1, py_image_cartoon); -#endif // IMLIB_ENABLE_CARTOON - //////////////////// // Geometric Methods //////////////////// @@ -6637,11 +6614,6 @@ static const mp_rom_map_elem_t locals_dict_table[] = { #else {MP_ROM_QSTR(MP_QSTR_bilateral), MP_ROM_PTR(&py_func_unavailable_obj)}, #endif - #ifdef IMLIB_ENABLE_CARTOON - {MP_ROM_QSTR(MP_QSTR_cartoon), MP_ROM_PTR(&py_image_cartoon_obj)}, - #else - {MP_ROM_QSTR(MP_QSTR_cartoon), MP_ROM_PTR(&py_func_unavailable_obj)}, - #endif /* Geometric Methods */ #ifdef IMLIB_ENABLE_LINPOLAR {MP_ROM_QSTR(MP_QSTR_linpolar), MP_ROM_PTR(&py_image_linpolar_obj)},