如果有人正在寻找创建一个非矩形蒙版,然后将其应用到图像上,那么请看这里:
Mat& obtainIregularROI(Mat& origImag, Point2f topLeft, Point2f topRight, Point2f botLeft, Point2f botRight){
static Mat black(origImag.rows, origImag.cols, origImag.type(), cv::Scalar::all(0));
Mat mask(origImag.rows, origImag.cols, CV_8UC1, cv::Scalar(0));
vector< vector<Point> > co_ordinates;
co_ordinates.push_back(vector<Point>());
co_ordinates[0].push_back(topLeft);
co_ordinates[0].push_back(botLeft);
co_ordinates[0].push_back(botRight);
co_ordinates[0].push_back(topRight);
drawContours( mask,co_ordinates,0, Scalar(255),CV_FILLED, 8 );
origImag.copyTo(black,mask);
return black;
}
“黑色”是我们最终将通过从原始图像中裁剪掉不规则的 ROI 来获得结果的图像。
static Mat black(origImag.rows, origImag.cols, origImag.type(), cv::Scalar::all(0));
“mask”是一个Mat,初始化为和原图一样大小,填充0。 Mat mask(origImag.rows, origImag.cols, CV_8UC1, cv::Scalar(0));
将坐标置于逆时针方向
vector< vector<Point> > co_ordinates;
co_ordinates.push_back(vector<Point>());
co_ordinates[0].push_back(topLeft);
co_ordinates[0].push_back(botLeft);
co_ordinates[0].push_back(botRight);
co_ordinates[0].push_back(topRight);
现在实际生成掩码
drawContours( mask,co_ordinates,0, Scalar(255),CV_FILLED, 8 );
最后从原始图像(origImag)复制蒙版部分/ ROI,并将原始图像(使用蒙版)的ROI部分粘贴到名为“黑色”的图像中
origImag.copyTo(black,mask);