From e81f22879603b4af45bb97ea4cbd67d7bd90eb01 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Wed, 14 Sep 2016 20:54:53 +0200 Subject: [PATCH] Add simple edge function. --- src/omv/img/edge.c | 15 +++++++++++++++ src/omv/img/imlib.h | 2 ++ src/omv/py/py_image.c | 7 ++++++- src/omv/py/qstrdefsomv.h | 1 + usr/examples/09-Feature-Detection/edges.py | 5 ++++- 5 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/omv/img/edge.c b/src/omv/img/edge.c index a83afd156..098e8c294 100644 --- a/src/omv/img/edge.c +++ b/src/omv/img/edge.c @@ -17,6 +17,12 @@ typedef struct gvec { uint16_t g; } gvec_t; +static const int8_t kernel_high_pass_33[] = { + -1, -1, -1, + -1, +8, -1, + -1, -1, -1 +}; + static const int8_t kernel_gauss_55[] = { 2, 4, 5, 4, 2, 4, 9, 12, 9, 4, @@ -26,6 +32,15 @@ static const int8_t kernel_gauss_55[] = { }; +void imlib_edge_simple(image_t *src, rectangle_t *roi, int low_thresh, int high_thresh) +{ + imlib_morph(src, 1, kernel_high_pass_33, 1.0f, 0.0f); + simple_color_t lt = {.G=low_thresh}; + simple_color_t ht = {.G=high_thresh}; + imlib_binary(src, 1, <, &ht, false); + imlib_erode(src, 1, 2); +} + void imlib_edge_canny(image_t *src, rectangle_t *roi, int low_thresh, int high_thresh) { int w = src->w; diff --git a/src/omv/img/imlib.h b/src/omv/img/imlib.h index ff3453223..7ce3867ad 100644 --- a/src/omv/img/imlib.h +++ b/src/omv/img/imlib.h @@ -355,6 +355,7 @@ typedef enum descriptor_type { typedef enum edge_detector_type { EDGE_CANNY, + EDGE_SIMPLE, } edge_detector_t; typedef enum template_match { @@ -530,5 +531,6 @@ void im_filter_skin(uint8_t *src, uint8_t *dst, int size, int bpp, void *args); array_t *imlib_find_lines(image_t *src, rectangle_t *roi, int threshold); // Edge detection +void imlib_edge_simple(image_t *src, rectangle_t *roi, int low_thresh, int high_thresh); void imlib_edge_canny(image_t *src, rectangle_t *roi, int low_thresh, int high_thresh); #endif //__IMLIB_H__ diff --git a/src/omv/py/py_image.c b/src/omv/py/py_image.c index 46bab8866..a8bae7f2e 100644 --- a/src/omv/py/py_image.c +++ b/src/omv/py/py_image.c @@ -1384,11 +1384,15 @@ static mp_obj_t py_image_find_edges(uint n_args, const mp_obj_t *args, mp_map_t py_helper_lookup_int_array(kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_threshold), thresh, 2); switch (edge_type) { + case EDGE_SIMPLE: { + imlib_edge_simple(img, &roi, thresh[0], thresh[1]); + break; + } case EDGE_CANNY: { - // Sanity checks imlib_edge_canny(img, &roi, thresh[0], thresh[1]); break; } + } return mp_const_true; @@ -1830,6 +1834,7 @@ static const mp_map_elem_t globals_dict_table[] = { {MP_OBJ_NEW_QSTR(MP_QSTR_SEARCH_EX), MP_OBJ_NEW_SMALL_INT(SEARCH_EX)}, {MP_OBJ_NEW_QSTR(MP_QSTR_SEARCH_DS), MP_OBJ_NEW_SMALL_INT(SEARCH_DS)}, {MP_OBJ_NEW_QSTR(MP_QSTR_EDGE_CANNY), MP_OBJ_NEW_SMALL_INT(EDGE_CANNY)}, + {MP_OBJ_NEW_QSTR(MP_QSTR_EDGE_SIMPLE), MP_OBJ_NEW_SMALL_INT(EDGE_SIMPLE)}, /* Color space functions */ {MP_OBJ_NEW_QSTR(MP_QSTR_rgb_to_lab), (mp_obj_t)&py_image_rgb_to_lab_obj}, {MP_OBJ_NEW_QSTR(MP_QSTR_lab_to_rgb), (mp_obj_t)&py_image_lab_to_rgb_obj}, diff --git a/src/omv/py/qstrdefsomv.h b/src/omv/py/qstrdefsomv.h index e227cf608..d4ba28f4c 100644 --- a/src/omv/py/qstrdefsomv.h +++ b/src/omv/py/qstrdefsomv.h @@ -21,6 +21,7 @@ Q(search) Q(SEARCH_EX) Q(SEARCH_DS) Q(EDGE_CANNY) +Q(EDGE_SIMPLE) Q(load_descriptor) Q(save_descriptor) Q(match_descriptor) diff --git a/usr/examples/09-Feature-Detection/edges.py b/usr/examples/09-Feature-Detection/edges.py index bad3602a7..7b2da2158 100644 --- a/usr/examples/09-Feature-Detection/edges.py +++ b/usr/examples/09-Feature-Detection/edges.py @@ -13,5 +13,8 @@ clock = time.clock() # Tracks FPS. while(True): clock.tick() # Track elapsed milliseconds between snapshots(). img = sensor.snapshot() # Take a picture and return the image. - img.find_edges(image.EDGE_CANNY, threshold=(50, 100)) + # Use Canny edge detector + img.find_edges(image.EDGE_CANNY, threshold=(50, 80)) + # Faster simpler edge detection + #img.find_edges(image.EDGE_SIMPLE, threshold=(100, 255)) print(clock.fps()) # Note: Your OpenMV Cam runs about half as fast while