polylines()//只能绘制多边形,不能填充

void cv::polylines(

InputOutputArray img,

InputArrayOfArrays pts,

bool isClosed,

const Scalar & color,

int thickness = 1,

int lineType = LINE_8,

int shift = 0

)

img表示绘制画布,图像

pts表示多边形的点

isClosed表示是否闭合,默认闭合

color表示颜色

thickness表示线宽,必须是正数

lineType表示线渲染类型

shift表示相对位移

fillPoly() //填充多边形

drawContours() //可同时绘制填充多个多边形

需要提前构造多个点集的多边形

void drawContours(

InputOutputArray image,

InputArrayOfArrays contours,

int contourIdx,

const Scalar& color,

int thickness=1,

int lineType=8,

InputArray hierarchy=noArray(),

int maxLevel=INT_MAX, Point offset=Point() )

参数image 表示 目标图像,

参数contours 表示输入的轮廓组,每一组轮廓由点vector构成,

参数contourIdx 为画第几个轮廓,如果该参数为负值,则画全部轮廓,

参数color为轮廓的颜色,

参数thickness为轮廓的线宽,如果为负值或CV_FILLED表示填充轮廓内部,

参数 lineType 为 线型,

参数为 轮廓结构 信息,

参数为 maxLevel

如:std::vector<std::vector > contours;

contours.push_back(pts);

//drawContours(canvas, contours,-1, Scalar(255,0, 0),0); //一次性绘制多个

drawContours(canvas, contours, -1, Scalar(255, 0, 0),-1); //一次性填充绘制函数

void QuickDemo::polygon_drawing_demo(Mat &image)//画多边形
{
	Mat canvas = Mat::zeros(Size(512, 512), CV_8UC3); //新建一个图像
	Point p1(100,100);
	Point p2(300, 100);
	Point p3(400, 280);
	Point p4(280,320);
	Point p5(50,400);
	std::vector<Point> pts;//是 C++中的一种数据结构,确切的说是一个类.它相当于一个动态的数组,当不确定或者不知道自己需要的数组的规模多大时,用其来解决问题可以达到最大节约空间的目的. 
	pts.push_back(p1);
	pts.push_back(p2);
	pts.push_back(p3);
	pts.push_back(p4);
	pts.push_back(p5);
	//fillPoly(canvas, pts, Scalar(255, 255, 0), 8, 0);    //填充多边形
	polylines(canvas, pts, true, Scalar(0, 0, 255), 2, 8, 0);   //这个函数不能进行填充,即参数2的位置不能为-1;
	//polylines(canvas, pts, true, Scalar(0, 0, 255), 2,LINE_AA, 0);   //LINE_AA 线无锯齿
	
	std::vector<std::vector<Point> > contours;
	contours.push_back(pts);
	//drawContours(canvas, contours,-1, Scalar(255,0, 0),0); //一次性绘制多个
	drawContours(canvas, contours, -1, Scalar(255, 0, 0),-1); //一次性填充绘制函数
	imshow("多边形绘制演示", canvas);
}



版权声明:本文为weixin_41768870原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/weixin_41768870/article/details/116062026