我正在尝试学习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。这不是我想要做的。我该如何修复它?请帮帮忙。
发布于 2018-06-07 04:52:00
cv::Rect(x, y, x + 3, y + 3);
的使用不是您想的那样,因为最后一个参数并不意味着矩形右下角的坐标。看一看OpenCV doc或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) {}
最后两个参数表示矩形的宽度和高度,因此要修复代码,您需要使用cv::Rect my_region = cv::Rect(x, y, 3, 3);
https://stackoverflow.com/questions/50728895
复制相似问题