OpenCv image blocks, size error?

I have a function that breaks the image into blocks for further processing using C ++ and OpenCv.

This is my code:

  void imageSplit(Mat image)
    {
        int blockNumber = 8;

        // get the image data
        int height = image.rows;
        int width = image.cols;


        //set how many blocks and create vector to store
        cv::Size smallSize(height / blockNumber, width / blockNumber);

        std::vector < Mat > smallImages;

        for (int y = 0; y < image.rows; y += smallSize.height)
        {
            for (int x = 0; x < image.cols; x += smallSize.width)
            {

                cv::Rect rect = cv::Rect(x, y, smallSize.width, smallSize.height);
                //cout << x << " " << y << " " << smallSize.width << " " << smallSize.height << endl;
                smallImages.push_back(cv::Mat(image, rect));
            }

        }
    }

It works great with a large region (512 x 512 works). But when I come to sizes equal to 100 x 100 px, it gives me:

OpenCV Error: Assertion failed (0 <= roi.x && 0 <= roi.width && roi.x + roi.widt
h <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows) in
 cv::Mat::Mat, file src\matrix.cpp, line 323
default exception.

Is the problem related to size? And if so, is there a way around this?

+4
source share
1 answer

Because berak is known for not really answering questions.

The code should be:

    for (int y = 0; y < image.rows-smallSize.height; y += smallSize.height)
    {
        for (int x = 0; x < image.cols-smallSize.width; x += smallSize.width)
        {

            cv::Rect rect = cv::Rect(x, y, smallSize.width, smallSize.height);
            //cout << x << " " << y << " " << smallSize.width << " " << smallSize.height << endl;
            smallImages.push_back(cv::Mat(image, rect));
        }

    }
}

This means that you are not increasing the number of areas in which you actually have no image.

+2
source

All Articles