给出区域轮廓点集,通过
drawContours
函数填充区域,生成mask图像,与原图相与
简要代码如下
#include<iostream>
#include<opencv2\opencv.hpp>
using namespace std;
using namespace cv;
int main()
{
Mat src = imread("F:/testdata/input.png");
Mat mask = Mat::zeros(src.size(), CV_8UC1);
Mat dst;
vector<vector<Point2i>> contours;
vector<Point2i> points;
points.push_back(Point2i(100, 100));
points.push_back(Point2i(50, 150));
points.push_back(Point2i(100, 200));
points.push_back(Point2i(200, 200));
points.push_back(Point2i(220, 150));
points.push_back(Point2i(200, 100));
contours.push_back(points);
drawContours(mask, contours, 0, Scalar::all(255), -1);
imshow("src", src);
src.copyTo(dst, mask);
imshow("dst", dst);
waitKey(0);
return 0;
}
结果如下
原图截取区域
网友评论