簡體   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