gpt4 book ai didi

c++ - 如何在矩形子区域中划分 OpenCV Mat?

转载 作者:行者123 更新时间:2023-11-28 00:11:30 26 4
gpt4 key购买 nike

我想在不同的区域 (10x10) 中划分一个简单的 Mat (200x200)。我做了 2 个循环,然后我创建了一个 Rect,我在其中指明了我在每次迭代中想要的变量 (x, y, width, height)。最后,我将图像的这个区域保存在 Matvector 中。

但是我的代码有问题:

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 Mat?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32870430/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com