gpt4 book ai didi

将矩形转换为圆形

转载 作者:行者123 更新时间:2023-11-30 16:19:50 25 4
gpt4 key购买 nike

我正在尝试使用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);
}
}

所以,结果显示如下: enter image description here

https://i.imgur.com/b7Bl9Iv.png

我想把它变成一个圆圈

最佳答案

这是 Bresenham 算法的简单实现,用于在图像上绘制省略号(改编自 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);
}
}

关于将矩形转换为圆形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55475650/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com