opencv bresenham画圆 并保存大圆坐标,再以大圆上的点为圆心画小圆并填充,并保存为视频

自己把图片地址改一下

#include<opencv2/opencv.hpp>
#include <opencv2/highgui/highgui.hpp>
#include<math.h>
#include<string.h>
using namespace cv;
void readlistanddraw(Mat image);
void savepointtolist(int x, int y);
void bresenham(Mat *image, int x0, int y0, int R, char *color, int flag1);
struct node
{
	int x1, y1;
}point[2000];

int R2;
char color[10];
int count = 0;
int f = 1;
Mat image = imread("C:\\Users\\chenh\\Desktop\\earth.jpg");

int main()
{
	//namedWindow("move");
	int x0, y0, R;
	printf("请输入大圆圆心坐标and半径:");
	scanf("%d%d%d", &x0, &y0,&R);
	printf("请输入颜色:");
	scanf("%s",color);
	printf("请输入小圆的半径:");
	scanf("%d", &R2);
	bresenham(&image, x0, y0, R, color, 0);
	readlistanddraw(image);
}
void savepointtolist(int x,int y)
{
	point[count].x1 = x;
	point[count].y1 = y;
	printf("%d	%d\n", point[count].x1, point[count].y1);
	count++;

}
void readlistanddraw(Mat image)
{
	int i, j;
	for (i = 0; i < 8; i++)
	{
		for (j = i; j < count; j = j + 8)
		{
			bresenham(&image, point[j].x1, point[j].y1, R2, color, 1);
			imshow("move", image);
			waitKey(5);
			destroyAllWindows();
		}
	}
}
void bresenham(Mat *image,int x0,int y0,int R,char *color,int flag1)//flag1为是否填充的标志,默认为不填充,填充为1
{
	int t1, t2;
	int r, g, b, temp1 = x0,temp2=y0;
	y0 = y0 - R;
	if (strcmp("red", color) == 0)
	{
		r = 255, g = 255, b = 255;
	}
	else if (strcmp("blue", color) == 0)
	{
		r = 255, g = 255, b = 255;
	}
	else if (strcmp("green", color) == 0)
	{
		r = 255, g = 255, b = 255;
	}
	else
	{
		r = 255, g = 255, b = 255;
	}
	if(!(x0-R>=0&&image->cols-2*R>=0&&y0>=R&&image->cols-y0>=R))
	{
		printf("您输入不合法,请重新输入\n");
		exit(1);
	}
	while (x0<temp1+(1.414/2)*R+1)
	{
		float t1 = sqrt((x0 - temp1)*(x0 - temp1) + (y0 - temp2)*(y0 - temp2));//向右移动
		float t2 = sqrt((x0 - temp1)*(x0 - temp1) + (y0 -temp2-1)*(y0 - temp2-1));//右下移动
		if (t1-R>0)
		{
			y0++;
		}
		//printf("%d	%d\n", x0, y0);
		image->at<Vec3b>(x0, y0)[0] = b;
		image->at<Vec3b>(x0, y0)[1] = g;
		image->at<Vec3b>(x0, y0)[2] = r;
		
		image->at<Vec3b>(y0 - temp2 + temp1, x0 - temp1 + temp2)[0] = b;
		image->at<Vec3b>(y0 - temp2 + temp1, x0 - temp1 + temp2)[1] = g;
		image->at<Vec3b>(y0 - temp2 + temp1, x0 - temp1 + temp2)[2] = r;

		image->at<Vec3b>(-(x0 - temp1) + temp1, y0)[0] = b;
		image->at<Vec3b>(-(x0 - temp1) + temp1, y0)[1] = g;
		image->at<Vec3b>(-(x0 - temp1) + temp1, y0)[2] = r;

		image->at<Vec3b>(x0, -(y0 - temp2) + temp2)[0] = b;
		image->at<Vec3b>(x0, -(y0 - temp2) + temp2)[1] = g;
		image->at<Vec3b>(x0, -(y0 - temp2) + temp2)[2] = r;

		image->at<Vec3b>(-(x0 - temp1) + temp1, -(y0 - temp2) + temp2)[0] = b;
		image->at<Vec3b>(-(x0 - temp1) + temp1, -(y0 - temp2) + temp2)[1] = g;
		image->at<Vec3b>(-(x0 - temp1) + temp1, -(y0 - temp2) + temp2)[2] = r;

		image->at<Vec3b>(-(y0 - temp2) + temp1, x0 - temp1 + temp2)[0] = b;
		image->at<Vec3b>(-(y0 - temp2) + temp1, x0 - temp1 + temp2)[1] = g;
		image->at<Vec3b>(-(y0 - temp2) + temp1, x0 - temp1 + temp2)[2] = r;

		image->at<Vec3b>(-(y0 - temp2) + temp1, -(x0 - temp1) + temp2)[0] = b;
		image->at<Vec3b>(-(y0 - temp2) + temp1, -(x0 - temp1) + temp2)[1] = g;
		image->at<Vec3b>(-(y0 - temp2) + temp1, -(x0 - temp1) + temp2)[2] = r;

		image->at<Vec3b>(y0 - temp2 + temp1, -(x0 - temp1) + temp2)[0] = b;
		image->at<Vec3b>(y0 - temp2 + temp1, -(x0 - temp1) + temp2)[1] = g;
		image->at<Vec3b>(y0 - temp2 + temp1, -(x0 - temp1) + temp2)[2] = r;

		if (!flag1)
		{
			savepointtolist(x0, y0);
			savepointtolist(y0 - temp2 + temp1, x0 - temp1 + temp2);
			savepointtolist(-(x0 - temp1) + temp1, y0);
			savepointtolist(x0, -(y0 - temp2) + temp2);
			savepointtolist(-(x0 - temp1) + temp1, -(y0 - temp2) + temp2);
			savepointtolist(-(y0 - temp2) + temp1, x0 - temp1 + temp2);
			savepointtolist(-(y0 - temp2) + temp1, -(x0 - temp1) + temp2);
			savepointtolist(y0 - temp2 + temp1, -(x0 - temp1) + temp2);
		}
		x0++;
	}
	if (flag1 == 12)
	{
		while (sqrt((x0 - temp1)*(x0 - temp1) + (y0 + R - temp2)*(y0 + R - temp2)) <= R)
		{
			image->at<Vec3b>(x0, y0)[0] = b;
			image->at<Vec3b>(x0, y0)[1] = g;
			image->at<Vec3b>(x0, y0)[2] = r;
		}
	}
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值