c++ - OpenCv图像 block ,大小错误?

标签 c++ opencv assertion

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

这是我的代码:

  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 像素这样的尺寸时,它给了我:

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

    }
}

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

关于c++ - OpenCv图像 block ,大小错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29672607/

相关文章:

python - OpenCV python 重叠粒子大小和数量

c++ - 在 vector c++ 中显示运行时断言错误

python - Python 中 'none' 类型的单元测试

c++ - 如何在卸载 DLL 或进程终止时释放资源

c++ - QT 信号/槽

c++ - 使用 boost::posix_time::microsec_clock 的测量误差超过十微秒?

c++ - 如何通过另一个具有位置的垫子访问opencv中的矩阵数据(索引)

python - 如何使用 python OpenCV 在匹配特定值的单 channel 图像中查找最大连通分量?

C++ 找不到在继承自当前模板类的模板基类中定义的类型

json - 不使用 JsonSlurper 的两个不同字符串的 SoapUI JSON 路径断言