簡體   English   中英

c ++ OpenCV Rect大小增加

[英]c++ OpenCV Rect size increasing

我試圖了解cv :: Rect()的工作原理。 我有一個200x200的圖像,我想把這個圖像分成3x3矩形。 這是我的代碼:

for(int y = 0; y < img.rows; y+=3;) // img.rows = 200
{
    for(int x =0; x < img.cols; x+=3){ // img.cols = 200
        cv:: Rect my_region = cv::Rect(x, y, x + 3, y + 3);
        Mat my_mat = img(my_region).clone();
        int mat_Size = mymat.total(); 
        cout << "region size: " << mat_Size << endl;
        if(x==6){  // test up to 6
           return - 1;
        }
    }
}

mat_size應該始終為9,因為我正在生成3x3矩形。 然而,它實際上每次增加9:9 - > 18 - > 27.這不是我打算做的。 我如何解決它? 請幫忙。

使用cv::Rect(x, y, x + 3, y + 3); 不是你的意思,因為你想到的最后一個參數並不意味着Rectangle右下角的坐標。 看看OpenCV文檔或者看看types.hpp ,你會看到

template<typename _Tp> inline
Rect_<_Tp>::Rect_(_Tp _x, _Tp _y, _Tp _width, _Tp _height)
: x(_x), y(_y), width(_width), height(_height) {}

最后兩個參數代表Rectangle的寬度和高度,所以要修復你的代碼,你需要cv::Rect my_region = cv::Rect(x, y, 3, 3);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM