繁体   English   中英

OpenCv图像块,大小错误?

[英]OpenCv image blocks, size error?

我有一个函数,使用C ++和OpenCv将图像分割成块以进行进一步处理。

这是我的代码:

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

        }
    }

它适用于更大的区域(512 x 512工作)但是当我达到100 x 100 px的尺寸时,它给了我:

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.

问题与尺寸有关吗? 如果是这样,有办法吗?

因为berak因为没有真正提交问题答案而臭名昭着。

您的代码需要是:

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

    }
}

这是为了阻止您增加到实际上没有图像的区域。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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