OpenCV绘制多边形和区域填充

OpenCV绘制多边形和区域填充

绘制多边形和区域填充主要用到了2个函数:polylines()和fillPoly()函数。
代码如下:

#include
#include
#include 
#include "opencvLibs.h"

int main()
{
	cv::Mat src(768, 1024,CV_8UC3);
	src.setTo(0);

	std::vector contour;
	contour.reserve(6);
	contour.push_back(cv::Point(389,253));
	contour.push_back(cv::Point(0, 529));
	contour.push_back(cv::Point(0, 768));
	contour.push_back(cv::Point(1024, 768));
	contour.push_back(cv::Point(1024, 643));
	contour.push_back(cv::Point(671, 263));

	std::vector> contours;
	contours.push_back(contour);

	//填充区域之前,首先采用polylines()函数,可以使填充的区域边缘更光滑
	cv::polylines(src, contours, true, cv::Scalar(255, 255, 255), 2, cv::LINE_AA);//第2个参数可以采用contour或者contours,均可
	cv::fillPoly(src, contours, cv::Scalar(255, 255, 255));//fillPoly函数的第二个参数是二维数组!!

	cv::imshow("原图", src);
	cv::waitKey(0);

	return 0;
}

你可能感兴趣的:(OpenCV)