Fix up lens_correction and add example script.

This commit is contained in:
Kwabena W. Agyeman 2017-10-28 00:29:01 -04:00
parent 7ef27db280
commit 8d63f6d84b
3 changed files with 27 additions and 3 deletions

View File

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

View File

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

View File

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