Khánh Nguyễn
Khánh Nguyễn

Reputation: 1

Error ROI image in OpenCV

I have a binary image with some noise. I want to reduce the noise by using a rectangle size(10x10) sliding along the image.

If the rectangle consists of more than 20 nonZero pixels, I will copy ROI to the destination image.

for (int i = 0; i < binary.rows-10; i+=10){
    for (int j = 0; j < binary.cols-10; j+=10)
    {
        cv::Rect Roi(i, j, 10, 10);
        cv::Mat countImg = cv::Mat(10, 10, CV_8UC1);
        countImg = cv::Mat(binary, Roi);

        if (cv::countNonZero(countImg)>20)
        {
            countImg.copyTo(binary_filter.rowRange(i, i + 10).colRange(j, j + 10));
        }
    }
}

The program encountered an error at function countImg = cv::Mat(binary, Roi); Who can explain?

Upvotes: 0

Views: 737

Answers (1)

herohuyongtao
herohuyongtao

Reputation: 50667

The real problem happens here:

cv::Rect Roi(i, j, 10, 10);

cv::Rect is of format (x, y, width, height) not (y, x, width_, height).


To make it work, change it to

cv::Rect Roi(j, i, 10, 10);

Upvotes: 1

Related Questions