#include <opencv2/opencv.hpp>
int main()
{
using namespace cv;
std::vector<cv::Point> pts;
{
pts.push_back(cv::Point(70, 190));//1
pts.push_back(cv::Point(467, 190));//3
pts.push_back(cv::Point(138, 380));//5
pts.push_back(cv::Point(280, 61));//2
pts.push_back(cv::Point(392, 380));//4
pts.push_back(cv::Point(70, 190));//6
}
Mat src = imread("D:/500_1s.png", IMREAD_GRAYSCALE);
//cv::resize(src, src, cv::Size(600, 600));//缩放回原始图像
auto t1 = cv::getTickCount();
Mat mask = cv::Mat::zeros(src.size(), CV_8UC1);
Mat dst;
cv::fillPoly(mask, pts, cv::Scalar(255, 255, 255), 8, 0);
if (1)
{
auto t1 = getTickCount();
std::vector<std::vector<cv::Point>> contours;
cv::findContours(mask, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE);
cv::fillPoly(mask, contours[0], cv::Scalar(255, 255, 255), 8, 0);
auto t2 = getTickCount();
std::cout << " " << (t2 - t1) * 1000 / cv::getTickFrequency() << " ms" << std::endl;
}
cv::bitwise_and(src, src, dst, mask);
auto t2 = getTickCount();
std::cout << " " << (t2 - t1) * 1000 / cv::getTickFrequency() << " ms" << std::endl;
imshow("mask", mask);
imshow("dst", dst);
waitKey();
}