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;
		}
	}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章