mirror of
https://github.com/openmv/openmv.git
synced 2025-11-04 14:49:50 +08:00
commit
42db630564
@ -16,15 +16,6 @@
|
||||
#include "xalloc.h"
|
||||
#include "mdefs.h"
|
||||
#include "font.h"
|
||||
#define MIN(a,b) \
|
||||
({ __typeof__ (a) _a = (a); \
|
||||
__typeof__ (b) _b = (b); \
|
||||
_a < _b ? _a : _b; })
|
||||
|
||||
#define MAX(a,b) \
|
||||
({ __typeof__ (a) _a = (a); \
|
||||
__typeof__ (b) _b = (b); \
|
||||
_a > _b ? _a : _b; })
|
||||
|
||||
#define PIXEL_AT(src, x, y) \
|
||||
({ __typeof__ (x) _x = (x); \
|
||||
@ -123,8 +114,8 @@ void imlib_rgb_to_hsv(struct color *rgb, struct color *hsv)
|
||||
g = rgb->g*100/255;
|
||||
b = rgb->b*100/255;
|
||||
|
||||
min = MIN(r, MIN(g, b));
|
||||
max = MAX(r, MAX(g, b));
|
||||
min = IM_MIN(r, IM_MIN(g, b));
|
||||
max = IM_MAX(r, IM_MAX(g, b));
|
||||
|
||||
if (min == max) {
|
||||
/* Black/gray/white */
|
||||
@ -250,7 +241,7 @@ void imlib_binary(image_t *src, int threshold)
|
||||
}
|
||||
}
|
||||
|
||||
#ifdef OPENMV1
|
||||
#ifdef OPENMV1
|
||||
void imlib_threshold(image_t *src, image_t *dst, color_t *color, int color_size, int threshold)
|
||||
{
|
||||
struct color rgb, lab;
|
||||
@ -276,7 +267,7 @@ void imlib_threshold(image_t *src, image_t *dst, color_t *color, int color_size,
|
||||
rgb.g = ((p&0x07)<<3)|(p>>13)*255/63;
|
||||
rgb.b = ((p>>8)&0x1F)*255/31;
|
||||
imlib_rgb_to_lab(&rgb, &lab);
|
||||
|
||||
|
||||
dst->pixels[i+x] = 0;
|
||||
for (int c=0; c<color_size; c++) {
|
||||
uint32_t sum =(color[c].L-lab.L) * (color[c].L-lab.L) +
|
||||
@ -599,8 +590,8 @@ void imlib_draw_rectangle(struct image *image, struct rectangle *r)
|
||||
{
|
||||
int i;
|
||||
uint8_t c=0xFF;
|
||||
int x = MIN(MAX(r->x, 0), image->w-1);
|
||||
int y = MIN(MAX(r->y, 0), image->h-1);
|
||||
int x = IM_MIN(IM_MAX(r->x, 0), image->w-1);
|
||||
int y = IM_MIN(IM_MAX(r->y, 0), image->h-1);
|
||||
int w = (x+r->w) >= image->w ? (image->w-x-1):r->w;
|
||||
int h = (y+r->h) >= image->h ? (image->h-y-1):r->h;
|
||||
|
||||
|
||||
@ -12,21 +12,32 @@
|
||||
#include <stdbool.h>
|
||||
#include "array.h"
|
||||
#include "fmath.h"
|
||||
typedef struct point {
|
||||
uint16_t x;
|
||||
uint16_t y;
|
||||
} point_t;
|
||||
|
||||
#define IM_MIN(a,b) \
|
||||
({ __typeof__ (a) _a = (a); \
|
||||
__typeof__ (b) _b = (b); \
|
||||
_a < _b ? _a : _b; })
|
||||
|
||||
#define IM_MAX(a,b) \
|
||||
({ __typeof__ (a) _a = (a); \
|
||||
__typeof__ (b) _b = (b); \
|
||||
_a > _b ? _a : _b; })
|
||||
|
||||
typedef struct size {
|
||||
int w;
|
||||
int h;
|
||||
} wsize_t;
|
||||
|
||||
typedef struct point {
|
||||
int16_t x;
|
||||
int16_t y;
|
||||
} point_t;
|
||||
|
||||
typedef struct rectangle {
|
||||
int x;
|
||||
int y;
|
||||
int w;
|
||||
int h;
|
||||
int16_t x;
|
||||
int16_t y;
|
||||
int16_t w;
|
||||
int16_t h;
|
||||
} rectangle_t;
|
||||
|
||||
typedef struct blob {
|
||||
@ -140,17 +151,16 @@ typedef enum interp {
|
||||
} interp_t;
|
||||
|
||||
/* Point functions */
|
||||
point_t *point_alloc(int x, int y);
|
||||
int point_equal(point_t *p1, point_t *p2);
|
||||
point_t *point_alloc(int16_t x, int16_t y);
|
||||
bool point_equal(point_t *p1, point_t *p2);
|
||||
float point_distance(point_t *p1, point_t *p2);
|
||||
|
||||
/* Rectangle functions */
|
||||
rectangle_t *rectangle_alloc();
|
||||
rectangle_t *rectangle_clone(rectangle_t *r);
|
||||
void rectangle_add(rectangle_t *r0, rectangle_t *r1);
|
||||
void rectangle_div(rectangle_t *r0, int c);
|
||||
int rectangle_intersects(rectangle_t *r0, rectangle_t *r1);
|
||||
array_t *rectangle_merge(array_t *r);
|
||||
rectangle_t *rectangle_alloc(int16_t x, int16_t y, int16_t w, int16_t h);
|
||||
bool rectangle_equal(rectangle_t *r1, rectangle_t *r2);
|
||||
bool rectangle_intersects(rectangle_t *r1, rectangle_t *r2);
|
||||
bool rectangle_subimg(image_t *img, rectangle_t *r, rectangle_t *r_out);
|
||||
array_t *rectangle_merge(array_t *rectangles);
|
||||
|
||||
/* Clustering functions */
|
||||
array_t *cluster_kmeans(array_t *points, int k);
|
||||
|
||||
@ -3,27 +3,21 @@
|
||||
* Copyright (c) 2013/2014 Ibrahim Abdelkader <i.abdalkader@gmail.com>
|
||||
* This work is licensed under the MIT license, see the file LICENSE for details.
|
||||
*
|
||||
* X,Y Point.
|
||||
* Point functions.
|
||||
*
|
||||
*/
|
||||
#include <float.h>
|
||||
#include <limits.h>
|
||||
#include <arm_math.h>
|
||||
#include "imlib.h"
|
||||
#include "array.h"
|
||||
#include "xalloc.h"
|
||||
|
||||
point_t *point_alloc(int x, int y)
|
||||
point_t *point_alloc(int16_t x, int16_t y)
|
||||
{
|
||||
point_t *p = xalloc(sizeof(*p));
|
||||
if (p != NULL) {
|
||||
p->x = x;
|
||||
p->y = y;
|
||||
}
|
||||
point_t *p = xalloc(sizeof(point_t));
|
||||
p->x = x;
|
||||
p->y = y;
|
||||
return p;
|
||||
}
|
||||
|
||||
int point_equal(point_t *p1, point_t *p2)
|
||||
bool point_equal(point_t *p1, point_t *p2)
|
||||
{
|
||||
return ((p1->x==p2->x)&&(p1->y==p2->y));
|
||||
}
|
||||
|
||||
@ -6,95 +6,100 @@
|
||||
* Rectangle functions.
|
||||
*
|
||||
*/
|
||||
#include <float.h>
|
||||
#include <limits.h>
|
||||
#include <arm_math.h>
|
||||
#include "imlib.h"
|
||||
#include "array.h"
|
||||
#include "xalloc.h"
|
||||
|
||||
rectangle_t *rectangle_alloc(int x, int y, int w, int h)
|
||||
rectangle_t *rectangle_alloc(int16_t x, int16_t y, int16_t w, int16_t h)
|
||||
{
|
||||
rectangle_t *rectangle;
|
||||
rectangle = xalloc(sizeof(*rectangle));
|
||||
rectangle->x = x;
|
||||
rectangle->y = y;
|
||||
rectangle->w = w;
|
||||
rectangle->h = h;
|
||||
return rectangle;
|
||||
rectangle_t *r = xalloc(sizeof(rectangle_t));
|
||||
r->x = x;
|
||||
r->y = y;
|
||||
r->w = w;
|
||||
r->h = h;
|
||||
return r;
|
||||
}
|
||||
|
||||
rectangle_t *rectangle_clone(rectangle_t *rect)
|
||||
bool rectangle_equal(rectangle_t *r1, rectangle_t *r2)
|
||||
{
|
||||
rectangle_t *rectangle;
|
||||
rectangle = xalloc(sizeof(rectangle_t));
|
||||
memcpy(rectangle, rect, sizeof(rectangle_t));
|
||||
return rectangle;
|
||||
return ((r1->x==r2->x)&&(r1->y==r2->y)&&(r1->w==r2->w)&&(r1->h==r2->h));
|
||||
}
|
||||
|
||||
void rectangle_add(rectangle_t *rect0, rectangle_t *rect1)
|
||||
bool rectangle_intersects(rectangle_t *r1, rectangle_t *r2)
|
||||
{
|
||||
rect0->x += rect1->x;
|
||||
rect0->y += rect1->y;
|
||||
rect0->w += rect1->w;
|
||||
rect0->h += rect1->h;
|
||||
return ((r1->x < (r2->x+r2->w)) &&
|
||||
(r1->y < (r2->y+r2->h)) &&
|
||||
((r1->x+r1->w) > r2->x) &&
|
||||
((r1->y+r1->h) > r2->y));
|
||||
}
|
||||
|
||||
void rectangle_div(rectangle_t *rect0, int c)
|
||||
// Determine subimg even if it is going off the edge of the main image.
|
||||
bool rectangle_subimg(image_t *img, rectangle_t *r, rectangle_t *r_out)
|
||||
{
|
||||
rect0->x /= c;
|
||||
rect0->y /= c;
|
||||
rect0->w /= c;
|
||||
rect0->h /= c;
|
||||
rectangle_t r_img;
|
||||
r_img.x = 0;
|
||||
r_img.y = 0;
|
||||
r_img.w = img->w;
|
||||
r_img.h = img->h;
|
||||
bool result = rectangle_intersects(&r_img, r);
|
||||
if (result) {
|
||||
int r_img_x2 = r_img.x + r_img.w;
|
||||
int r_img_y2 = r_img.y + r_img.h;
|
||||
int r_x2 = r->x + r->w;
|
||||
int r_y2 = r->y + r->h;
|
||||
r_out->x = IM_MAX(r_img.x, r->x);
|
||||
r_out->y = IM_MAX(r_img.y, r->y);
|
||||
r_out->w = IM_MIN(r_img_x2, r_x2) - r_out->x;
|
||||
r_out->h = IM_MIN(r_img_y2, r_y2) - r_out->y;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
int rectangle_intersects(rectangle_t *rect0, rectangle_t *rect1)
|
||||
// This isn't for actually combining the rects standardly, but, to instead
|
||||
// find the average rectangle between a bunch of overlapping rectangles.
|
||||
static void rectangle_add(rectangle_t *r1, rectangle_t *r2)
|
||||
{
|
||||
return ((rect0->x < (rect1->x+rect1->w)) &&
|
||||
(rect0->y < (rect1->y+rect1->h)) &&
|
||||
((rect0->x+rect0->w) > rect1->x) &&
|
||||
((rect0->y+rect0->h) > rect1->y));
|
||||
r1->x += r2->x;
|
||||
r1->y += r2->y;
|
||||
r1->w += r2->w;
|
||||
r1->h += r2->h;
|
||||
}
|
||||
|
||||
// This isn't for actually combining the rects standardly, but, to instead
|
||||
// find the average rectangle between a bunch of overlapping rectangles.
|
||||
static void rectangle_div(rectangle_t *r, int c)
|
||||
{
|
||||
r->x /= c;
|
||||
r->y /= c;
|
||||
r->w /= c;
|
||||
r->h /= c;
|
||||
}
|
||||
|
||||
array_t *rectangle_merge(array_t *rectangles)
|
||||
{
|
||||
int j;
|
||||
array_t *objects;
|
||||
array_t *overlap;
|
||||
rectangle_t *rect1, *rect2;
|
||||
|
||||
array_alloc(&objects, xfree);
|
||||
array_alloc(&overlap, xfree);
|
||||
|
||||
array_t *objects; array_alloc(&objects, xfree);
|
||||
array_t *overlap; array_alloc(&overlap, xfree);
|
||||
/* merge overlaping detections */
|
||||
while (array_length(rectangles)) {
|
||||
/* check for overlaping detections */
|
||||
rect1 = (rectangle_t *) array_at(rectangles, 0);
|
||||
for (j=1; j<array_length(rectangles); j++) {
|
||||
rect2 = (rectangle_t *) array_at(rectangles, j);
|
||||
if (rectangle_intersects(rect1, rect2)) {
|
||||
array_push_back(overlap, rectangle_clone(rect2));
|
||||
array_erase(rectangles, j--);
|
||||
rectangle_t *rect = (rectangle_t *) array_take(rectangles, 0);
|
||||
for (int j=0; j<array_length(rectangles); j++) { // do not cache bound
|
||||
if (rectangle_intersects(rect, (rectangle_t *) array_at(rectangles, j))) {
|
||||
array_push_back(overlap, array_take(rectangles, j--));
|
||||
}
|
||||
}
|
||||
|
||||
/* add the overlaping detections */
|
||||
int count = array_length(overlap)+1;
|
||||
while (array_length(overlap)) {
|
||||
rect2 = (rectangle_t *) array_at(overlap, 0);
|
||||
rectangle_add(rect1, rect2);
|
||||
array_erase(overlap, 0);
|
||||
int count = array_length(overlap);
|
||||
for (int i=0; i<count; i++) {
|
||||
rectangle_t *overlap_rect = (rectangle_t *) array_pop_back(overlap);
|
||||
rectangle_add(rect, overlap_rect);
|
||||
xfree(overlap_rect);
|
||||
}
|
||||
|
||||
/* average the overlaping detections */
|
||||
rectangle_div(rect1, count);
|
||||
array_push_back(objects, rectangle_clone(rect1));
|
||||
array_erase(rectangles, 0);
|
||||
rectangle_div(rect, count + 1);
|
||||
array_push_back(objects, rect);
|
||||
}
|
||||
|
||||
array_free(overlap);
|
||||
array_free(rectangles);
|
||||
array_free(overlap);
|
||||
return objects;
|
||||
}
|
||||
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user