From 8d63f6d84b1f83cba43549483cae80134f3b0bd1 Mon Sep 17 00:00:00 2001 From: "Kwabena W. Agyeman" Date: Sat, 28 Oct 2017 00:29:01 -0400 Subject: [PATCH] Fix up lens_correction and add example script. --- src/omv/img/imlib.c | 3 +++ src/omv/py/py_image.c | 6 +++--- .../04-Image-Filters/lens_correction.py | 21 +++++++++++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 usr/examples/04-Image-Filters/lens_correction.py diff --git a/src/omv/img/imlib.c b/src/omv/img/imlib.c index 630b25fc3..c4d72061e 100644 --- a/src/omv/img/imlib.c +++ b/src/omv/img/imlib.c @@ -1072,6 +1072,7 @@ void imlib_lens_corr(image_t *img, float strength, float zoom) // Create a temp copy of the image to pull pixels from. uint32_t *tmp = fb_alloc(((img->w + UINT32_T_MASK) >> UINT32_T_SHIFT) * img->h); memcpy(tmp, img->data, ((img->w + UINT32_T_MASK) >> UINT32_T_SHIFT) * img->h); + memset(img->data, 0, ((img->w + UINT32_T_MASK) >> UINT32_T_SHIFT) * img->h); for (int y = 0, yy = img->h; y < yy; y++) { uint32_t *row_ptr = IMAGE_COMPUTE_BINARY_PIXEL_ROW_PTR(img, y); @@ -1104,6 +1105,7 @@ void imlib_lens_corr(image_t *img, float strength, float zoom) // Create a temp copy of the image to pull pixels from. uint8_t *tmp = fb_alloc(img->w * img->h * sizeof(uint8_t)); memcpy(tmp, img->data, img->w * img->h * sizeof(uint8_t)); + memset(img->data, 0, img->w * img->h * sizeof(uint8_t)); for (int y = 0, yy = img->h; y < yy; y++) { uint8_t *row_ptr = IMAGE_COMPUTE_GRAYSCALE_PIXEL_ROW_PTR(img, y); @@ -1136,6 +1138,7 @@ void imlib_lens_corr(image_t *img, float strength, float zoom) // Create a temp copy of the image to pull pixels from. uint16_t *tmp = fb_alloc(img->w * img->h * sizeof(uint16_t)); memcpy(tmp, img->data, img->w * img->h * sizeof(uint16_t)); + memset(img->data, 0, img->w * img->h * sizeof(uint16_t)); for (int y = 0, yy = img->h; y < yy; y++) { uint16_t *row_ptr = IMAGE_COMPUTE_RGB565_PIXEL_ROW_PTR(img, y); diff --git a/src/omv/py/py_image.c b/src/omv/py/py_image.c index 4aefb29db..74f19dd98 100644 --- a/src/omv/py/py_image.c +++ b/src/omv/py/py_image.c @@ -1245,11 +1245,11 @@ static mp_obj_t py_image_lens_corr(uint n_args, const mp_obj_t *args, mp_map_t * float strength = py_helper_lookup_float(kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_strength), (n_args > 1) ? mp_obj_get_float(args[1]) : 1.8); - PY_ASSERT_TRUE_MSG(strength >= 0.0, "strength must be > 0"); + PY_ASSERT_TRUE_MSG(strength > 0.0, "strength must be > 0"); float zoom = py_helper_lookup_float(kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_zoom), (n_args > 2) ? mp_obj_get_float(args[2]) : 1.0); - PY_ASSERT_TRUE_MSG(zoom >= 1.0, "zoom must be > 1"); + PY_ASSERT_TRUE_MSG(zoom > 0.0, "zoom must be > 0"); fb_alloc_mark(); imlib_lens_corr(arg_img, strength, zoom); @@ -1283,7 +1283,7 @@ static mp_obj_t py_image_rotation_corr(uint n_args, const mp_obj_t *args, mp_map float zoom = py_helper_lookup_float(kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_zoom), (n_args > 6) ? mp_obj_get_float(args[6]) : 1.0); - PY_ASSERT_TRUE_MSG(zoom >= 0.0, "zoom must be > 0"); + PY_ASSERT_TRUE_MSG(zoom > 0.0, "zoom must be > 0"); fb_alloc_mark(); imlib_rotation_corr(arg_img, x_rotation, y_rotation, z_rotation, x_translation, y_translation, zoom); diff --git a/usr/examples/04-Image-Filters/lens_correction.py b/usr/examples/04-Image-Filters/lens_correction.py new file mode 100644 index 000000000..56066f583 --- /dev/null +++ b/usr/examples/04-Image-Filters/lens_correction.py @@ -0,0 +1,21 @@ +# Lens Correction +# +# This example shows off how to use the lens correction method to fix lens +# distortion in an image. You need to do this for qrcode / barcode / data matrix +# detection. Increase the strength below until lines are straight in the view. +# Zoom in (higher) or out (lower) until you see enough of the image. + +import sensor, image, time + +sensor.reset() +sensor.set_pixformat(sensor.RGB565) +sensor.set_framesize(sensor.QVGA) +sensor.skip_frames(time = 2000) +clock = time.clock() + +while(True): + clock.tick() + + img = sensor.snapshot().lens_corr(strength = 1.8, zoom = 1.0) + + print(clock.fps())