Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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::filimage descriptionlPoly 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();

click to hide/show revision 2
No.2 Revision

updated 2014-09-02 01:34:16 -0600

berak gravatar image

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::filimage descriptionlPoly 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> vector<Point2f> &_pts) {
    vector<point> 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();

waitKey();

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::filimage descriptionlPoly 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();