How to crop non rectangular area from Mat?
Hello everybody,
I need some help with cropping cv::Mat. There are a src mat image and a polygon to be cropped (for example triangle). For cropping I create mask image having size like src mat one and depth CV_8U. Then I filled my mask with 0 value and draw polygon on it using cv::fillPoly function . Finally, I try to apply the mask to src image using Mat::copyTo function but nothing happens with src img. What I do wrong?
Sorry for my bad English.
here is a part of my code
Mat createMask(const Size &size, const vector<Point2f> &_pts) {
vector<Point> pts(_pts.size());
for(int i = 0; i < _pts.size(); i++) {
pts[i] = _pts[i];
}
const Point* elementPoints[1] = { &pts[0] };
int numPoints = (int)pts.size();
Mat mMask = Mat::zeros(size, CV_8U);
mMask.setTo(255);
//circle(mMask, Point(30,30), 20, Scalar(0), 0);
fillPoly(mMask, elementPoints, &numPoints, 1, Scalar(0));
imshow("mMask", mMask);
return mMask;
}
Mat mMask = createMask(size, pts); //some cv::Size and some predefined polygon points
Mat mTmp = mSrc.clone();
mTmp.copyTo(mSrc, mMask);
imshow("debug_frame", mFrame);
waitKey();
This) helped me alot. In opencv you have cv::bitwise_[and/or/xor/not] function.
I do not think that you can have a Mat that is not rectangular, so the best way is to find the minimum bounding rectangle and have just that region.