OpenCV基本图形绘制之填充连接区域内部

先上代码

#include 
#include 
#include 
#include 
#include 
#include 

using namespace cv;
using namespace std;

#define WINDOW_WIDTH 600//自定义窗口大小的宏 

void DrawPolygon(Mat img) {
	int lineType = 8;

	//创建一些点
	Point rookPoints[1][20];
	rookPoints[0][0] = Point(WINDOW_WIDTH / 4, 7 * WINDOW_WIDTH / 8);
	rookPoints[0][1] = Point(3*WINDOW_WIDTH / 4, 7 * WINDOW_WIDTH / 8);
	rookPoints[0][2] = Point(3*WINDOW_WIDTH / 4, 13 * WINDOW_WIDTH / 16);
	rookPoints[0][3] = Point(11*WINDOW_WIDTH / 16, 13 * WINDOW_WIDTH / 16);
	rookPoints[0][4] = Point(19*WINDOW_WIDTH / 32, 3 * WINDOW_WIDTH / 8);
	rookPoints[0][5] = Point(3*WINDOW_WIDTH / 4, 3 * WINDOW_WIDTH / 8);
	rookPoints[0][6] = Point(3*WINDOW_WIDTH / 4, WINDOW_WIDTH / 8);
	rookPoints[0][7] = Point(26*WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);
	rookPoints[0][8] = Point(26*WINDOW_WIDTH / 40,  WINDOW_WIDTH / 4);
	rookPoints[0][9] = Point(22*WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);
	rookPoints[0][10] = Point(22*WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);
	rookPoints[0][11] = Point(18*WINDOW_WIDTH / 40, WINDOW_WIDTH / 8);
	rookPoints[0][12] = Point(18*WINDOW_WIDTH / 40,  WINDOW_WIDTH / 4);
	rookPoints[0][13] = Point(14*WINDOW_WIDTH / 40, WINDOW_WIDTH / 4);
	rookPoints[0][14] = Point(14*WINDOW_WIDTH / 40,  WINDOW_WIDTH / 8);
	rookPoints[0][15] = Point(WINDOW_WIDTH / 4, WINDOW_WIDTH / 8);
	rookPoints[0][16] = Point(WINDOW_WIDTH / 4,  3*WINDOW_WIDTH / 8);
	rookPoints[0][17] = Point(13*WINDOW_WIDTH / 32,  3*WINDOW_WIDTH / 8);
	rookPoints[0][18] = Point(5*WINDOW_WIDTH / 16, 13 * WINDOW_WIDTH / 16);
	rookPoints[0][19] = Point(WINDOW_WIDTH / 4, 13 * WINDOW_WIDTH / 16);

	const Point* ppt[1] = {rookPoints[0] };
	int npt[] = { 20 };
	fillPoly(img,
		ppt,
		npt,
		1,
		Scalar(255, 255, 255),
		lineType);
}

int main(int argc, char** argv)
{
	Mat img = Mat::zeros(WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3);

	DrawPolygon(img);
	imshow("name1", img);
	waitKey(0);
	return 0;
}

效果

OpenCV基本图形绘制之填充连接区域内部_第1张图片

 函数解析

 点位按照向右为正,向下为正。

按照数组中点位的顺序连接,最后一个点位会自动连接到起点,然后封闭区域自动填充。

试验demo

#include 
#include 
#include 
#include 
#include 
#include 

using namespace cv;
using namespace std;

#define WINDOW_WIDTH 600//自定义窗口大小的宏 

void DrawPolygon(Mat img) {
	int lineType = 8;

	//创建一些点
	Point rookPoints[1][3];
	rookPoints[0][0] = Point(50,50);
	rookPoints[0][1] = Point(50, 300);
	rookPoints[0][2] = Point(300, 300);
	const Point* ppt[1] = { rookPoints[0] };
	int npt[] = { 3 };
	fillPoly(img,
		ppt,
		npt,
		1,
		Scalar(255, 255, 255),
		lineType);
}



int main(int argc, char** argv)
{
	Mat img = Mat::zeros(WINDOW_WIDTH, WINDOW_WIDTH, CV_8UC3);

	DrawPolygon(img);
	imshow("name1", img);
	waitKey(0);
	return 0;
}

效果

OpenCV基本图形绘制之填充连接区域内部_第2张图片

 经过测试,无论是三角形或者是矩形或者其他图形,尾坐标都会自动连接起始坐标,然后封闭区域自动填充

你可能感兴趣的:(OpenCV,opencv,c++)