imlib: Remove cartoon() method.

This commit is contained in:
Kwabena W. Agyeman 2024-02-12 20:27:41 -08:00
parent 32b7075bc6
commit 055a468b83
17 changed files with 0 additions and 275 deletions

View File

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

View File

@ -65,9 +65,6 @@
// Enable bilateral() // Enable bilateral()
//#define IMLIB_ENABLE_BILATERAL //#define IMLIB_ENABLE_BILATERAL
// Enable cartoon()
// #define IMLIB_ENABLE_CARTOON
// Enable linpolar() // Enable linpolar()
#define IMLIB_ENABLE_LINPOLAR #define IMLIB_ENABLE_LINPOLAR

View File

@ -66,9 +66,6 @@
// Enable bilateral() // Enable bilateral()
//#define IMLIB_ENABLE_BILATERAL //#define IMLIB_ENABLE_BILATERAL
// Enable cartoon()
// #define IMLIB_ENABLE_CARTOON
// Enable linpolar() // Enable linpolar()
//#define IMLIB_ENABLE_LINPOLAR //#define IMLIB_ENABLE_LINPOLAR

View File

@ -65,9 +65,6 @@
// Enable bilateral() // Enable bilateral()
//#define IMLIB_ENABLE_BILATERAL //#define IMLIB_ENABLE_BILATERAL
// Enable cartoon()
// #define IMLIB_ENABLE_CARTOON
// Enable linpolar() // Enable linpolar()
//#define IMLIB_ENABLE_LINPOLAR //#define IMLIB_ENABLE_LINPOLAR

View File

@ -65,9 +65,6 @@
// Enable bilateral() // Enable bilateral()
#define IMLIB_ENABLE_BILATERAL #define IMLIB_ENABLE_BILATERAL
// Enable cartoon()
// #define IMLIB_ENABLE_CARTOON
// Enable linpolar() // Enable linpolar()
#define IMLIB_ENABLE_LINPOLAR #define IMLIB_ENABLE_LINPOLAR

View File

@ -65,9 +65,6 @@
// Enable bilateral() // Enable bilateral()
//#define IMLIB_ENABLE_BILATERAL //#define IMLIB_ENABLE_BILATERAL
// Enable cartoon()
// #define IMLIB_ENABLE_CARTOON
// Enable linpolar() // Enable linpolar()
#define IMLIB_ENABLE_LINPOLAR #define IMLIB_ENABLE_LINPOLAR

View File

@ -62,9 +62,6 @@
// Enable bilateral() // Enable bilateral()
//#define IMLIB_ENABLE_BILATERAL //#define IMLIB_ENABLE_BILATERAL
// Enable cartoon()
//#define IMLIB_ENABLE_CARTOON
// Enable linpolar() // Enable linpolar()
//#define IMLIB_ENABLE_LINPOLAR //#define IMLIB_ENABLE_LINPOLAR

View File

@ -65,9 +65,6 @@
// Enable bilateral() // Enable bilateral()
#define IMLIB_ENABLE_BILATERAL #define IMLIB_ENABLE_BILATERAL
// Enable cartoon()
// #define IMLIB_ENABLE_CARTOON
// Enable linpolar() // Enable linpolar()
#define IMLIB_ENABLE_LINPOLAR #define IMLIB_ENABLE_LINPOLAR

View File

@ -65,9 +65,6 @@
// Enable bilateral() // Enable bilateral()
#define IMLIB_ENABLE_BILATERAL #define IMLIB_ENABLE_BILATERAL
// Enable cartoon()
// #define IMLIB_ENABLE_CARTOON
// Enable linpolar() // Enable linpolar()
#define IMLIB_ENABLE_LINPOLAR #define IMLIB_ENABLE_LINPOLAR

View File

@ -65,9 +65,6 @@
// Enable bilateral() // Enable bilateral()
#define IMLIB_ENABLE_BILATERAL #define IMLIB_ENABLE_BILATERAL
// Enable cartoon()
// #define IMLIB_ENABLE_CARTOON
// Enable linpolar() // Enable linpolar()
#define IMLIB_ENABLE_LINPOLAR #define IMLIB_ENABLE_LINPOLAR

View File

@ -65,9 +65,6 @@
// Enable bilateral() // Enable bilateral()
#define IMLIB_ENABLE_BILATERAL #define IMLIB_ENABLE_BILATERAL
// Enable cartoon()
// #define IMLIB_ENABLE_CARTOON
// Enable linpolar() // Enable linpolar()
#define IMLIB_ENABLE_LINPOLAR #define IMLIB_ENABLE_LINPOLAR

View File

@ -65,9 +65,6 @@
// Enable bilateral() // Enable bilateral()
#define IMLIB_ENABLE_BILATERAL #define IMLIB_ENABLE_BILATERAL
// Enable cartoon()
// #define IMLIB_ENABLE_CARTOON
// Enable linpolar() // Enable linpolar()
#define IMLIB_ENABLE_LINPOLAR #define IMLIB_ENABLE_LINPOLAR

View File

@ -64,9 +64,6 @@
// Enable bilateral() // Enable bilateral()
#define IMLIB_ENABLE_BILATERAL #define IMLIB_ENABLE_BILATERAL
// Enable cartoon()
// #define IMLIB_ENABLE_CARTOON
// Enable linpolar() // Enable linpolar()
#define IMLIB_ENABLE_LINPOLAR #define IMLIB_ENABLE_LINPOLAR

View File

@ -66,9 +66,6 @@
// Enable bilateral() // Enable bilateral()
//#define IMLIB_ENABLE_BILATERAL //#define IMLIB_ENABLE_BILATERAL
// Enable cartoon()
// #define IMLIB_ENABLE_CARTOON
// Enable linpolar() // Enable linpolar()
//#define IMLIB_ENABLE_LINPOLAR //#define IMLIB_ENABLE_LINPOLAR

View File

@ -2078,176 +2078,3 @@ void imlib_bilateral_filter(image_t *img,
} }
} }
#endif // IMLIB_ENABLE_BILATERAL #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

View File

@ -1422,7 +1422,6 @@ void imlib_bilateral_filter(image_t *img,
int offset, int offset,
bool invert, bool invert,
image_t *mask); image_t *mask);
void imlib_cartoon_filter(image_t *img, float seed_threshold, float floating_threshold, image_t *mask);
// Image Correction // Image Correction
void imlib_logpolar_int(image_t *dst, image_t *src, rectangle_t *roi, bool linear, bool reverse); // helper/internal 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); void imlib_logpolar(image_t *img, bool linear, bool reverse);

View File

@ -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); STATIC MP_DEFINE_CONST_FUN_OBJ_KW(py_image_bilateral_obj, 2, py_image_bilateral);
#endif // IMLIB_ENABLE_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 // Geometric Methods
//////////////////// ////////////////////
@ -6637,11 +6614,6 @@ static const mp_rom_map_elem_t locals_dict_table[] = {
#else #else
{MP_ROM_QSTR(MP_QSTR_bilateral), MP_ROM_PTR(&py_func_unavailable_obj)}, {MP_ROM_QSTR(MP_QSTR_bilateral), MP_ROM_PTR(&py_func_unavailable_obj)},
#endif #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 */ /* Geometric Methods */
#ifdef IMLIB_ENABLE_LINPOLAR #ifdef IMLIB_ENABLE_LINPOLAR
{MP_ROM_QSTR(MP_QSTR_linpolar), MP_ROM_PTR(&py_image_linpolar_obj)}, {MP_ROM_QSTR(MP_QSTR_linpolar), MP_ROM_PTR(&py_image_linpolar_obj)},