Add simple edge function.

This commit is contained in:
iabdalkader 2016-09-14 20:54:53 +02:00
parent 51f1605aee
commit e81f228796
5 changed files with 28 additions and 2 deletions

View File

@ -17,6 +17,12 @@ typedef struct gvec {
uint16_t g; uint16_t g;
} gvec_t; } 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[] = { static const int8_t kernel_gauss_55[] = {
2, 4, 5, 4, 2, 2, 4, 5, 4, 2,
4, 9, 12, 9, 4, 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, &lt, &ht, false);
imlib_erode(src, 1, 2);
}
void imlib_edge_canny(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)
{ {
int w = src->w; int w = src->w;

View File

@ -355,6 +355,7 @@ typedef enum descriptor_type {
typedef enum edge_detector_type { typedef enum edge_detector_type {
EDGE_CANNY, EDGE_CANNY,
EDGE_SIMPLE,
} edge_detector_t; } edge_detector_t;
typedef enum template_match { 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); array_t *imlib_find_lines(image_t *src, rectangle_t *roi, int threshold);
// Edge detection // 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); void imlib_edge_canny(image_t *src, rectangle_t *roi, int low_thresh, int high_thresh);
#endif //__IMLIB_H__ #endif //__IMLIB_H__

View File

@ -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); py_helper_lookup_int_array(kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_threshold), thresh, 2);
switch (edge_type) { switch (edge_type) {
case EDGE_SIMPLE: {
imlib_edge_simple(img, &roi, thresh[0], thresh[1]);
break;
}
case EDGE_CANNY: { case EDGE_CANNY: {
// Sanity checks
imlib_edge_canny(img, &roi, thresh[0], thresh[1]); imlib_edge_canny(img, &roi, thresh[0], thresh[1]);
break; break;
} }
} }
return mp_const_true; 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_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_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_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 */ /* 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_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}, {MP_OBJ_NEW_QSTR(MP_QSTR_lab_to_rgb), (mp_obj_t)&py_image_lab_to_rgb_obj},

View File

@ -21,6 +21,7 @@ Q(search)
Q(SEARCH_EX) Q(SEARCH_EX)
Q(SEARCH_DS) Q(SEARCH_DS)
Q(EDGE_CANNY) Q(EDGE_CANNY)
Q(EDGE_SIMPLE)
Q(load_descriptor) Q(load_descriptor)
Q(save_descriptor) Q(save_descriptor)
Q(match_descriptor) Q(match_descriptor)

View File

@ -13,5 +13,8 @@ clock = time.clock() # Tracks FPS.
while(True): while(True):
clock.tick() # Track elapsed milliseconds between snapshots(). clock.tick() # Track elapsed milliseconds between snapshots().
img = sensor.snapshot() # Take a picture and return the image. 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 print(clock.fps()) # Note: Your OpenMV Cam runs about half as fast while