简体   繁体   中英

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?

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM