opencv任意形状ROI

相关代码,比较简单,一看就懂:

	Mat srcImg = imread("lena.jpg");
	Mat dstImg;
	Mat mask = Mat::zeros(srcImg.size(),CV_8UC1);

	vector<vector<Point>> contour;
	vector<Point> pts;
	pts.push_back(Point(30,45));
	pts.push_back(Point(500,35));
	pts.push_back(Point(430,240));
	pts.push_back(Point(50,250));
	contour.push_back(pts);

	drawContours(mask,contour,0,Scalar::all(255),-1);
	srcImg.copyTo(dstImg,mask);

	imshow("src",srcImg);
	imshow("mask",mask);
	imshow("roi",dstImg);
	waitKey();


结果:

opencv任意形状ROI_第1张图片


备注:如果是凸包轮廓,可用fillConvexPoly




你可能感兴趣的:(opencv任意形状ROI)