将矩形转换为圆形
Convert Rectangle to circle
我正在尝试使用圆检测对象,我使用基于 yolo 深度学习的框架 darknet。
我必须更改此功能,我尝试将此功能从 draw_rectangle
更改为 draw_circle
,
怎么做?
void draw_box(image a, int x1, int y1, int x2, int y2, float r, float g, float b) {
//normalize_image(a);
int i;
if (x1 < 0) x1 = 0;
if (x1 >= a.w) x1 = a.w - 1;
if (x2 < 0) x2 = 0;
if (x2 >= a.w) x2 = a.w - 1;
if (y1 < 0) y1 = 0;
if (y1 >= a.h) y1 = a.h - 1;
if (y2 < 0) y2 = 0;
if (y2 >= a.h) y2 = a.h - 1;
for (i = x1; i <= x2; ++i) {
a.data[i + y1*a.w + 0*a.w*a.h] = r;
a.data[i + y2*a.w + 0*a.w*a.h] = r;
a.data[i + y1*a.w + 1*a.w*a.h] = g;
a.data[i + y2*a.w + 1*a.w*a.h] = g;
a.data[i + y1*a.w + 2*a.w*a.h] = b;
a.data[i + y2*a.w + 2*a.w*a.h] = b;
}
for(i = y1; i <= y2; ++i) {
a.data[x1 + i*a.w + 0*a.w*a.h] = r;
a.data[x2 + i*a.w + 0*a.w*a.h] = r;
a.data[x1 + i*a.w + 1*a.w*a.h] = g;
a.data[x2 + i*a.w + 1*a.w*a.h] = g;
a.data[x1 + i*a.w + 2*a.w*a.h] = b;
a.data[x2 + i*a.w + 2*a.w*a.h] = b;
}
}
void draw_box_width(image a, int x1, int y1, int x2, int y2, int w, float r, float g, float b) {
int i;
for (i = 0; i < w; ++i) {
draw_box(a, x1+i, y1+i, x2-i, y2-i, r, g, b);
}
}
所以,结果是这样的:
https://i.imgur.com/b7Bl9Iv.png
我想把它改成圆形
这是 Bresenham 算法的简单实现,用于在 image
上绘制省略号(改编自 https://gist.github.com/bert/1085538):
void set_pixel(image a, int x, int y, float r, float g, float b) {
if (x >= 0 && x < a.w && y >= 0 && y < a.h) {
a.data[x + y * a.w + 0 * a.w * a.h] = r;
a.data[x + y * a.w + 1 * a.w * a.h] = g;
a.data[x + y * a.w + 2 * a.w * a.h] = b;
}
}
void plot_ellipsis_rect(image a, int x0, int y0, int x1, int y1, float r, float g, float b) {
int a = abs(x1 - x0), b = abs(y1 - y0), b1 = b & 1; /* values of diameter */
long dx = 4 * (1 - a) * b * b, dy = 4 * (b1 + 1) * a * a; /* error increment */
long err = dx + dy + b1 * a * a, e2; /* error of 1.step */
y0 += (b + 1) / 2;
y1 = y0 - b1; /* starting pixel */
a = 8 * a * a;
b1 = 8 * b * b;
do {
set_pixel(a, x1, y0, r, g, b); /* I. Quadrant */
set_pixel(a, x0, y0, r, g, b); /* II. Quadrant */
set_pixel(a, x0, y1, r, g, b); /* III. Quadrant */
set_pixel(a, x1, y1, r, g, b); /* IV. Quadrant */
e2 = 2 * err;
if (e2 >= dx) {
x0++;
x1--;
err += dx += b1;
}
if (e2 <= dy) {
y0++;
y1--;
err += dy += a;
}
} while (x0 <= x1);
while (y0 - y1 < b) { /* too early stop of flat ellipses a=1 */
set_pixel(a, x0 - 1, y0, r, g, b); /* -> finish tip of ellipse */
set_pixel(a, x1 + 1, y0++, r, g, b);
set_pixel(a, x0 - 1, y1, r, g, b);
set_pixel(a, x1 + 1, y1--, r, g, b);
}
}
我正在尝试使用圆检测对象,我使用基于 yolo 深度学习的框架 darknet。
我必须更改此功能,我尝试将此功能从 draw_rectangle
更改为 draw_circle
,
怎么做?
void draw_box(image a, int x1, int y1, int x2, int y2, float r, float g, float b) {
//normalize_image(a);
int i;
if (x1 < 0) x1 = 0;
if (x1 >= a.w) x1 = a.w - 1;
if (x2 < 0) x2 = 0;
if (x2 >= a.w) x2 = a.w - 1;
if (y1 < 0) y1 = 0;
if (y1 >= a.h) y1 = a.h - 1;
if (y2 < 0) y2 = 0;
if (y2 >= a.h) y2 = a.h - 1;
for (i = x1; i <= x2; ++i) {
a.data[i + y1*a.w + 0*a.w*a.h] = r;
a.data[i + y2*a.w + 0*a.w*a.h] = r;
a.data[i + y1*a.w + 1*a.w*a.h] = g;
a.data[i + y2*a.w + 1*a.w*a.h] = g;
a.data[i + y1*a.w + 2*a.w*a.h] = b;
a.data[i + y2*a.w + 2*a.w*a.h] = b;
}
for(i = y1; i <= y2; ++i) {
a.data[x1 + i*a.w + 0*a.w*a.h] = r;
a.data[x2 + i*a.w + 0*a.w*a.h] = r;
a.data[x1 + i*a.w + 1*a.w*a.h] = g;
a.data[x2 + i*a.w + 1*a.w*a.h] = g;
a.data[x1 + i*a.w + 2*a.w*a.h] = b;
a.data[x2 + i*a.w + 2*a.w*a.h] = b;
}
}
void draw_box_width(image a, int x1, int y1, int x2, int y2, int w, float r, float g, float b) {
int i;
for (i = 0; i < w; ++i) {
draw_box(a, x1+i, y1+i, x2-i, y2-i, r, g, b);
}
}
所以,结果是这样的:
https://i.imgur.com/b7Bl9Iv.png
我想把它改成圆形
这是 Bresenham 算法的简单实现,用于在 image
上绘制省略号(改编自 https://gist.github.com/bert/1085538):
void set_pixel(image a, int x, int y, float r, float g, float b) {
if (x >= 0 && x < a.w && y >= 0 && y < a.h) {
a.data[x + y * a.w + 0 * a.w * a.h] = r;
a.data[x + y * a.w + 1 * a.w * a.h] = g;
a.data[x + y * a.w + 2 * a.w * a.h] = b;
}
}
void plot_ellipsis_rect(image a, int x0, int y0, int x1, int y1, float r, float g, float b) {
int a = abs(x1 - x0), b = abs(y1 - y0), b1 = b & 1; /* values of diameter */
long dx = 4 * (1 - a) * b * b, dy = 4 * (b1 + 1) * a * a; /* error increment */
long err = dx + dy + b1 * a * a, e2; /* error of 1.step */
y0 += (b + 1) / 2;
y1 = y0 - b1; /* starting pixel */
a = 8 * a * a;
b1 = 8 * b * b;
do {
set_pixel(a, x1, y0, r, g, b); /* I. Quadrant */
set_pixel(a, x0, y0, r, g, b); /* II. Quadrant */
set_pixel(a, x0, y1, r, g, b); /* III. Quadrant */
set_pixel(a, x1, y1, r, g, b); /* IV. Quadrant */
e2 = 2 * err;
if (e2 >= dx) {
x0++;
x1--;
err += dx += b1;
}
if (e2 <= dy) {
y0++;
y1--;
err += dy += a;
}
} while (x0 <= x1);
while (y0 - y1 < b) { /* too early stop of flat ellipses a=1 */
set_pixel(a, x0 - 1, y0, r, g, b); /* -> finish tip of ellipse */
set_pixel(a, x1 + 1, y0++, r, g, b);
set_pixel(a, x0 - 1, y1, r, g, b);
set_pixel(a, x1 + 1, y1--, r, g, b);
}
}