OpenCV分割图像成矩形

OpenCV Split Image into Rectangles

本文关键字:图像 分割 OpenCV      更新时间:2023-10-16

我有一个图像,我希望它被分割成甚至矩形到目前为止,我有这个代码,但我一直崩溃,我不知道为什么。

        int cells = 10;
        int rows_to_process = image.rows / cells;
        int cols_to_process = image.cols;
        cv::Mat img;
        std::vector<cv::Mat> imageSections(cells);
        int x = 0;
        int y = 0;
        cout << imageSections.size() << endl;
        for(int i=0; i<image.rows/rows_to_process; i++)
        {   
            cout << "i is: " << i << " x is: " << x << endl;
            imageSections[i] = image(cv::Rect(x, y, cols_to_process, rows_to_process));
            x += rows_to_process;
        }

我已经测试了只输出x,并且我从0到图像的大小以相等的增量获得维度。我希望将x增加到我想要开始的下一个点,但我总是崩溃。有人知道为什么吗?

我的错误在终端显示为

OpenCV Error: Assertion failed (0 <= roi.x && 0 <= roi.width && roi.x + roi.width <= m.cols && 0 <= roi.y && 0 <= roi.height && roi.y + roi.height <= m.rows) in Mat,

这个怎么样(没有测试):

    int cell_Height = image.rows / 10;
    int cell_width = image.cols;
    std::vector<cv::Mat> imageSections(rows_to_process);
    int x = 0;
    int y = 0;
    for(y=0; y+cell_Height<image.rows; y+=cell_Height)
    {   
        cout << y << endl;
        imageSections[i] = image(cv::Rect(x, y, cell_Width, cell_Height));
    }