diff --git a/src/omv/img/imlib.h b/src/omv/img/imlib.h index 285e65bc7..0247a3b0d 100644 --- a/src/omv/img/imlib.h +++ b/src/omv/img/imlib.h @@ -211,7 +211,8 @@ int freak_save_descriptor(kp_t *kpts, int kpts_size, const char *path); int freak_load_descriptor(kp_t **kpts_out, int *kpts_size_out, const char *path); /* LBP Operator */ -uint8_t *imlib_lbp_desc(image_t *image); +void imlib_lbp_desc(image_t *image, int div, uint8_t *desc, rectangle_t *roi); +uint8_t *imlib_lbp_cascade(image_t *image, rectangle_t *roi); int imlib_lbp_desc_distance(uint8_t *d0, uint8_t *d1); int imlib_lbp_desc_load(const char *path, uint8_t **desc); diff --git a/src/omv/img/lbp.c b/src/omv/img/lbp.c index 81fdf2845..13c06e6af 100644 --- a/src/omv/img/lbp.c +++ b/src/omv/img/lbp.c @@ -9,6 +9,7 @@ #include "imlib.h" #include "xalloc.h" #include "ff.h" +#include "std.h" #define min(a,b) \ ({ __typeof__ (a) _a = (a); \ @@ -21,8 +22,7 @@ _a > _b ? _a : _b; }) #define LBP_HIST_SIZE (59) //58 uniform hist + 1 -#define LBP_NUM_REGIONS (7) //7x7 regions -#define LBP_DESC_SIZE (LBP_NUM_REGIONS*LBP_NUM_REGIONS*LBP_HIST_SIZE) +#define LBP_DESC_SIZE (4956) //const static float lbp_weights [49]= { // 2.0f, 1.0f, 1.0f, 1.0f, 1.0f, 1.0f, 2.0f, @@ -53,45 +53,95 @@ const static uint8_t uniform_tbl[256] = { 47, 48, 58, 49, 58, 58, 58, 50, 51, 52, 58, 53, 54, 55, 56, 57 }; -uint8_t *imlib_lbp_desc(image_t *image) +//(56, 94, 24, 24) +void imlib_lbp_desc(image_t *image, int div, uint8_t *desc, rectangle_t *roi) { - int w = image->w; - int h = image->h; - int RY = h/LBP_NUM_REGIONS; - int RX = w/LBP_NUM_REGIONS; + int RY = roi->h/div; + int RX = roi->w/div; + int s = image->w; //stride + int h = (roi->y+RY*div)-3; + int w = (roi->x+RX*div)-3; uint8_t *data = image->data; - uint8_t *desc = xalloc0(LBP_DESC_SIZE); - for (int y=0; yy; yy)/RY*div; + for (int x=roi->x; xx)/RX; + uint8_t p = data[(y+1)*s+x+1]; - lbp |= (data[(y+0)*w+x+0] >= p) << 0; - lbp |= (data[(y+0)*w+x+1] >= p) << 1; - lbp |= (data[(y+0)*w+x+2] >= p) << 2; - lbp |= (data[(y+1)*w+x+2] >= p) << 3; - lbp |= (data[(y+2)*w+x+2] >= p) << 4; - lbp |= (data[(y+2)*w+x+1] >= p) << 5; - lbp |= (data[(y+2)*w+x+0] >= p) << 6; - lbp |= (data[(y+1)*w+x+0] >= p) << 7; + lbp |= (data[(y+0)*s+x+0] >= p) << 0; + lbp |= (data[(y+0)*s+x+1] >= p) << 1; + lbp |= (data[(y+0)*s+x+2] >= p) << 2; + lbp |= (data[(y+1)*s+x+2] >= p) << 3; + lbp |= (data[(y+2)*s+x+2] >= p) << 4; + lbp |= (data[(y+2)*s+x+1] >= p) << 5; + lbp |= (data[(y+2)*s+x+0] >= p) << 6; + lbp |= (data[(y+1)*s+x+0] >= p) << 7; desc[hist_idx*LBP_HIST_SIZE+uniform_tbl[lbp]]++; } } +} + +uint8_t *imlib_lbp_cascade(image_t *image, rectangle_t *roi) +{ + uint8_t *desc = xalloc0(LBP_DESC_SIZE); + + int offset =0; + for (int i=1, c=0; i<=7; i+=2, c++) { + imlib_lbp_desc(image, i, desc+offset, roi); + offset += i*i*LBP_HIST_SIZE; + } + return desc; } +#if 0 int imlib_lbp_desc_distance(uint8_t *d0, uint8_t *d1) { uint32_t sum = 0; - for (int i=0; ibpp == 1); + mp_obj_t *array; + mp_obj_get_array_fixed_n(roi_obj, 4, &array); + + rectangle_t roi = { + mp_obj_get_int(array[0]), + mp_obj_get_int(array[1]), + mp_obj_get_int(array[2]), + mp_obj_get_int(array[3]), + }; + lbp_obj = m_new_obj(py_lbp_obj_t); lbp_obj->base.type = &py_lbp_type; - lbp_obj->hist = imlib_lbp_desc(image); + lbp_obj->hist = imlib_lbp_cascade(image, &roi); return lbp_obj; } @@ -930,7 +938,7 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_1(py_image_find_blobs_obj, py_image_find_blobs); STATIC MP_DEFINE_CONST_FUN_OBJ_3(py_image_find_template_obj, py_image_find_template); STATIC MP_DEFINE_CONST_FUN_OBJ_KW(py_image_find_features_obj, 2, py_image_find_features); STATIC MP_DEFINE_CONST_FUN_OBJ_KW(py_image_find_keypoints_obj, 1, py_image_find_keypoints); -STATIC MP_DEFINE_CONST_FUN_OBJ_1(py_image_find_lbp_obj, py_image_find_lbp); +STATIC MP_DEFINE_CONST_FUN_OBJ_2(py_image_find_lbp_obj, py_image_find_lbp); STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(py_image_match_keypoints_obj, 4, 4, py_image_match_keypoints); STATIC MP_DEFINE_CONST_FUN_OBJ_3(py_image_match_lbp_obj, py_image_match_lbp);