user1959079
user1959079

Reputation: 91

Create a rectangle region mask on image with OpenCV

There are a image that read in cv::Mat format and I only want to remain a rectangle region to be detect in the center of image. I tried the cvCopy approach, but it required the image format as cvArr and does any guys have any idea how to implement that still use Mat format as well?


There is my code and screenshot for creating a mask and it seems strange that the size of mask did not match with the original frame. Any tips here please?

    *cap >> frame1;
    Rect roi(100,100,100,100);
    for(int i =0; i<frame1.rows; i++)
    {
        for(int j=0; j<frame1.cols;j++)
        {
            if(!roi.contains(Point(i,j)))
            {
                frame1.at<uchar>(i,j) = 0;
            }
        }
    }

enter image description here

Upvotes: 2

Views: 9954

Answers (1)

Rasim
Rasim

Reputation: 1296

Simply create new image, that refers to same data.

cv::Rect const mask(x1, y1, x2, y2);
cv::Mat roi = image(mask);

Now you can do roi processing. All changes on roi will appply on image too.

Upvotes: 6

Related Questions