在学习过程中,我们可以在图像中绘制一些几何图形,比如矩形,椭圆,线段,填充多边形等,这些函数都挺容易理解,下面简单看一下。

1.矩形,rectangle() 通过对角线上的两个顶点绘制矩形

void rectangle(InputOutputArray img, Rect rec,
		const Scalar& color, int thickness = 1,
		int lineType = LINE_8, int shift = 0);

img 名称
rec pt1矩形的顶点 pt2与pt1相对的矩形顶点
color 颜色  也可以用像素存放类Scalar
thickness 宽度 如果是-1,就代表对改矩形进行填充
lineType  类型
shift 移位点坐标中的小数位数。

代码:

int main()
{
	Mat img = Mat::ones(240, 240, CV_8UC3);
	rectangle(img, Rect(20, 20, 100, 100), Scalar(0, 0, 255),7);
	imshow("www", img);
	waitKey(0);

}

效果图:

2. 圆 circle()

void circle(InputOutputArray img, Point center, int radius,
		const Scalar& color, int thickness = 1,
		int lineType = LINE_8, int shift = 0);

img 名称
center 圆心坐标
radius 圆的半径
color 圆环颜色
thickness 正数,则表示圆轮廓的厚度 负数 对该圆填充颜色
lineType  类型
shift  移位中心坐标和半径值的小数位数。

代码:

int main()
{
	Mat img1=Mat::zeros(100, 100, CV_8UC3);
	circle(img1, Point(40, 40), 20, Scalar(0, 0, 255),-1);//-1 填充
	imshow("www", img1);
	waitKey(0);

}

效果图:

3.椭圆 elliple()

void ellipse(InputOutputArray img, Point center, Size axes,
		double angle, double startAngle, double endAngle,
		const Scalar& color, int thickness = 1,
		int lineType = LINE_8, int shift = 0);

img 名称
center 椭圆的中心。
axes 轴 椭圆主轴大小的一半。
angle	椭圆旋转角度。
startAngle	椭圆弧的起始角,以度表示。
endAngle	椭圆弧的结束角,以度数表示。
color	椭圆颜色。
thickness 正数 椭圆圆弧轮廓的厚度  负数 对椭圆进行填充。
linetype 椭圆边界类型。 
shift 中心坐标和坐标轴值的小数位数。

代码:

int main()
{
	Mat img1 = Mat::zeros(300, 300, CV_8UC3);
	ellipse(img1, Point(100, 100), Size(40, 25), 0, 0, 360, Scalar(0, 0, 255),5);
	imshow("111", img1);
	waitKey(0);

}

效果图: