学习OpenCV3 在图像容器MAT中作图(1)

学习OpenCV3 在图像容器MAT中作图(1)

首先是定义一个窗口并规定窗口的长宽

#define WINDOW_NAME1 "【绘制图1】"        //为窗口标题定义的宏 
#define WINDOW_WIDTH 500				//定义窗口大小的宏  长宽各500

如何画圆呢
我们通过一个园函数来自动画圆

void DrawFilledCircle( Mat img, Point center )
{
	int thickness =-1; //thickness 线宽为-1内部填满
	int lineType = 8; //lineType 线性

	circle( img,
		center,//根据圆心 
		WINDOW_WIDTH/28,//半径为      窗口长度/28
		Scalar( 0, 0, 255 ),//颜色为红色  三个参数分别代表	B G R
		thickness,
		lineType );
}

thickness =-1
学习OpenCV3 在图像容器MAT中作图(1)
thickness =1
学习OpenCV3 在图像容器MAT中作图(1)
WINDOW_WIDTH/2
学习OpenCV3 在图像容器MAT中作图(1)
int lineType = 8; //线性为8联通线型

什么是8联通线型 参考以下博客 四连通与八连通 - diligentkong的博客 - ****博客 https://blog.****.net/diligentkong/article/details/77991671

如何画椭圆呢
我们通过一个椭圆函数来自动画圆

void DrawEllipse( Mat img, double angle )
{
	int thickness =2;//线宽为2
	int lineType =8;//线型为8

	ellipse( img,
		Point( WINDOW_WIDTH/2, WINDOW_WIDTH/2 ),//中心点位置 即窗口中心点
		Size( WINDOW_WIDTH/4, WINDOW_WIDTH/16 ),//长半轴为:窗口宽/4    短半轴为:窗口宽/16      
		angle,
		0,			//距离x轴顺时针偏离0°
		360,
		Scalar( 255, 125, 0),//代表蓝色 数值可以随便改 颜色会变
		thickness,//线宽为2
		lineType );//线型为8
}

学习OpenCV3 在图像容器MAT中作图(1)

接着是主函数

int main(  )
{

	// 创建空白的Mat图像
	Mat atomImage = Mat::zeros( WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3 );
	
	//先绘制出椭圆
	DrawEllipse( atomImage, 0 );//距离x轴顺时针偏离0°
	//再绘制圆心
	DrawFilledCircle( atomImage, Point( WINDOW_WIDTH/2, WINDOW_WIDTH/2) );
	
	imshow( WINDOW_NAME1, atomImage );
	moveWindow( WINDOW_NAME1, 0, 200 );//以显示盘左上角为坐标原点向下为y+ 向右为x+   距离原点(200,200)
	waitKey( 0 );
	return(0);
}

在作图时默认以显示屏左上角为原点往下为y轴正方向,往右为x轴正方向
学习OpenCV3 在图像容器MAT中作图(1)
当然你也可以多画几个椭圆比如

	//先绘制出椭圆
	DrawEllipse( atomImage, 0 );//距离x轴顺时针偏离0°
	DrawEllipse( atomImage, 90 );//距离x轴顺时针偏离90°

学习OpenCV3 在图像容器MAT中作图(1)
又或者

	DrawEllipse( atomImage,   0 );//距离x轴顺时针偏离0°
	DrawEllipse( atomImage,  30 );//距离x轴顺时针偏离30°
	DrawEllipse( atomImage,  60 );//距离x轴顺时针偏离60°
	DrawEllipse( atomImage,  90 );//距离x轴顺时针偏离90°
	DrawEllipse( atomImage, 120 );//距离x轴顺时针偏离120°
	DrawEllipse( atomImage, 150 );//距离x轴顺时针偏离150°

学习OpenCV3 在图像容器MAT中作图(1)
恩 还不过瘾?
学习OpenCV3 在图像容器MAT中作图(1)
这下满意了吧 o( ̄︶ ̄)o
当然你也可以改变颜色 线宽 等等 作出意想不到的图像
学习OpenCV3 在图像容器MAT中作图(1)

完整版代码

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
using namespace cv;

#define WINDOW_NAME1 "【绘制图1】"        //为窗口标题定义的宏 
#define WINDOW_WIDTH 500				//定义窗口大小的宏  长宽各500

void DrawEllipse( Mat img, double angle );//绘制椭圆
void DrawFilledCircle( Mat img, Point center );//绘制圆

int main(  )
{

	// 创建空白的Mat图像
	Mat atomImage = Mat::zeros( WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3 );

	//先绘制出椭圆
	DrawEllipse( atomImage,    0 );//距离x轴顺时针偏离0°
	DrawEllipse( atomImage,  10 );//距离x轴顺时针偏离10°
	DrawEllipse( atomImage,  20 );//距离x轴顺时针偏离20°
	DrawEllipse( atomImage,  30 );//距离x轴顺时针偏离30°
	DrawEllipse( atomImage,  40 );//距离x轴顺时针偏离40°
	DrawEllipse( atomImage,  50 );//距离x轴顺时针偏离50°
	DrawEllipse( atomImage,  60 );//距离x轴顺时针偏离60°
	DrawEllipse( atomImage,  70 );//距离x轴顺时针偏离70°
	DrawEllipse( atomImage,  80 );//距离x轴顺时针偏离80°
	DrawEllipse( atomImage,  90 );//距离x轴顺时针偏离90°
	DrawEllipse( atomImage, 100 );//距离x轴顺时针偏离100°
	DrawEllipse( atomImage, 110 );//距离x轴顺时针偏离110°
	DrawEllipse( atomImage, 120 );//距离x轴顺时针偏离120°
	DrawEllipse( atomImage, 130 );//距离x轴顺时针偏离130°
	DrawEllipse( atomImage, 140 );//距离x轴顺时针偏离140°
	DrawEllipse( atomImage, 150 );//距离x轴顺时针偏离150°
	DrawEllipse( atomImage, 160 );//距离x轴顺时针偏离160°
	DrawEllipse( atomImage, 170 );//距离x轴顺时针偏离170°
	
	//再绘制圆心
	DrawFilledCircle( atomImage, Point( WINDOW_WIDTH/2, WINDOW_WIDTH/2) );


	imshow( WINDOW_NAME1, atomImage );
	moveWindow( WINDOW_NAME1, 0, 200 );//以显示盘左上角为坐标原点向下为y+ 向右为x+   距离原点(200,200)
	waitKey( 0 );
	return(0);
}

void DrawEllipse( Mat img, double angle )
{
	int thickness =2;//线宽为2
	int lineType =8;//线型为8

	ellipse( img,
		Point( WINDOW_WIDTH/2, WINDOW_WIDTH/2 ),//中心点位置 即窗口中心点
		Size( WINDOW_WIDTH/4, WINDOW_WIDTH/16 ),//长半轴为:窗口宽/4    短半轴为:窗口宽/16      
		angle,
		0,			//距离x轴顺时针偏离0°
		360,
		Scalar( 255, 125, 0),//代表蓝色 数值可以随便改 颜色会变
		thickness,//线宽为2
		lineType );//线型为8
}


void DrawFilledCircle( Mat img, Point center )
{
	int thickness =-1; //thickness 线宽为-1内部填满
	int lineType = 8; //lineType 线性

	circle( img,
		center,//根据圆心 
		WINDOW_WIDTH/28,//半径为      窗口长度/28
		Scalar( 0, 0, 255 ),//颜色为红色  三个参数分别代表	B G R
		thickness,
		lineType );
}





拜拜捏

2119816