opencv 在图中绘图(矩形框,线段,点,圆)

使用opencv处理图像时,经常会需要在图像上画点、线段、矩形框等,

而在opencv画图中Point和Scalar是两个非常重要的结构体。

1、Point表示由其图像坐标 x 和 y 指定的2D点。可定义为:

Point pt;
pt.x = 10;

pt.y = 8;

或者

Point pt =  Point(10, 8);

2、Scalar 表示了具有4个元素的数组。在OpenCV中被大量用于传递像素值。如果用不到第四个参数,则无需定义。

Scalar( a, b, c )
那么定义的RGB颜色值为: Red = c, Green = b and Blue = a


=============================================

opencv中常用的画图函数

画直线:line

画椭圆:ellipse

画矩形:rectangle

画圆:circle

画多边形:polylines

填充的多边形:fillPoly

接下来就是例子&结果:

	Mat image(300, 300, CV_8UC3, Scalar(0, 0, 0));
	
	Point points[1][20];
	points[0][0] = Point(100, 100);
	points[0][1] = Point(200, 100);
	points[0][2] = Point(250, 200);
	points[0][3] = Point(50, 200);

	const Point* pt[1] = { points[0] };
	int npt[1] = { 4 };

	polylines(image, pt, npt, 1, 1, Scalar(0, 0, 255),3);
	fillPoly(image, pt, npt, 1, Scalar(0, 255, 0), 8);
	imshow("image", image);
	waitKey(0);

	Mat image(300, 300, CV_8UC3, Scalar(0, 0, 0));
	
	Point p1, p2;
	p1.x = 100;
	p1.y = 100;
	p2.x = 200;
	p2.y = 200;

	rectangle(image, p1, p2, Scalar(0,255,0),1,8,0);

	Rect rt;
	rt.x = 50;
	rt.y = 50;
	rt.width = 100;
	rt.height = 80;

 
 
	Mat image(300, 300, CV_8UC3, Scalar(0, 0, 0));
	
	Point p1, p2;
	p1.x = 100;
	p1.y = 100;
	p2.x = 200;
	p2.y = 200;

	Point p3, p4;
	p3.x = 40;
	p3.y = 40;
	p4.x = 60;
	p4.y = 60;
	circle(image, p1, 5, Scalar(255,255,0),1,8,0);
	circle(image, p2, 5, Scalar(0, 255, 0), 1, 8, 0);
	circle(image, p3, 1, Scalar(0, 255, 0), 1, 8, 0);
	circle(image, p3, 5, Scalar(0, 255, 0), 1, 8, 0);
	circle(image, p3, 10, Scalar(0, 255, 0), 1, 8, 0);

	circle(image, p4, 5, Scalar(255, 255, 0), 1, 8, 0);

	line(image,p1,p2,Scalar(0,255,255),3,8,0);

	imshow("image", image);
	waitKey(0);



	Mat image(300, 300, CV_8UC3, Scalar(0, 0, 0));
	
	Point p1, p2;
	p1.x = 150;
	p1.y = 150;


	ellipse(image, p1, Size(50,80),0,0,360,Scalar(0,255,255),2,8);
	ellipse(image, p1, Size(50, 80), 90, 0, 360, Scalar(0, 255, 255), 2, 8);
	ellipse(image, p1, Size(50, 80), 45, 0, 360, Scalar(0, 255, 255), 2, 8);
	ellipse(image, p1, Size(50, 80), -45, 0, 360, Scalar(0, 255, 255), 2, 8);
	imshow("image", image);
	waitKey(0);


参考:

1.http://www.opencv.org.cn/opencvdoc/2.3.2/html/index.html#



猜你喜欢

转载自blog.csdn.net/qq_42189368/article/details/80719170