如何在矩形子区域中划分OpenCV Mat
How to divide an OpenCV Mat in rectangular sub-regions?
我想将一个简单的Mat
(200x200)划分为不同的区域(10x10)。我做了两个循环,然后我创建了一个Rect
,在这里我指出了我想要的变量在每次迭代中(x, y, width, height)
。最后,我将图像的这个区域保存在Mat
s的vector
中。
但是我的代码有问题:
Mat face = Mat(200, 200, CV_8UC1);
vector<Mat> regions;
Mat region_frame;
int width = face.cols * 0.05;
int heigth = face.rows * 0.05;
for(int y=0; y<=(face.rows - heigth); y+=heigth)
{
for(int x=0; x<=(face.cols - width); x+=width)
{
Rect region = Rect(x, y, x+width, y+heigth);
region_frame = face(region);
regions.push_back(region_frame);
}
}
问题只是在最后一步,它不与我尝试创建的新region_frame
的大小一起工作。它随着cols的迭代次数而增加。
我该如何解决这个问题?
OpenCV Rect可以构造为:
Rect(int _x, int _y, int _width, int _height);
所以你需要改变你的代码行:
Rect region = Rect(x, y, width, heigth);
看起来你传递的是左上角和右下角的坐标。如果您想这样做,请使用另一个构造函数:
Rect(const Point& pt1, const Point& pt2);
,你可以这样做:
Rect region = Rect(Point(x, y), Point(x+width, y+heigth));
相关文章:
- C++,OpenCV,尝试显示图像时"OpenCV(4.3.0) Error: Assertion failed (size.width>0 && size.height>0)"此错误
- 尝试导入pybind-opencv模块时出现libgtk错误
- 在编译C++代码(具有dlib和opencv)到WASM时面临问题
- 如何使用OpenCV将RBG图像转换为HSV,并将H、S和V值保存为C++中的3个独立图像
- OpenCV EqualizeHist()从彩色图像创建黑白图像
- 将OpenCV C++重写为EmguCV C#-如何使用指针
- OpenCV C++.快速计算混淆矩阵
- 在C++代码中包含opencv时,使用ctypes创建.so文件
- 哪些库可以通过Opencv调整曝光率
- 如何将输出数字划分为奇数和偶数
- 安装opencv失败-粘贴CMakeError.log的内容
- C++中的openCV Mat访问冲突
- OpenCV Android C++ imwrite not found
- 未定义的引用 .. 使用 OpenCV 编译 C++ 代码时,从命令行
- 将 OpenCV 与 CMAKE 中的项目一起构建为第三方库的正确方法
- CV_OCL_RUN宏如何在OpenCV(版本3.4.5)的goodFeaturesToTrack实现中工作?
- OpenCV 4.1.2 - 从网络摄像头获取帧并将其拆分
- 从opencv的视频关注和标量拍摄的图像之间的整数划分
- 划分 OpenCV 垫会产生意想不到的结果
- 如何在矩形子区域中划分OpenCV Mat