如何划分矩形子区域中的OpenCV Mat? [英] How to divide an OpenCV Mat in rectangular sub-regions?

查看:151
本文介绍了如何划分矩形子区域中的OpenCV Mat?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to divide a simple Mat (200x200) in different regions (10x10). I make 2 loops, then I create a Rect where I indicate the variables I want in each iteration (x, y, width, height). Finally, I save this region of the image inside a vector of Mats.

但是我的代码有问题:

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的每个迭代数而增加。

The problem is just in the final step, it's not working with size of the new region_frame I try to create. It's increasing with each iteration number of cols.

如何解决这个问题?

推荐答案

OpenCV Rect 可以构建为:

OpenCV Rect can be constructed as:

Rect(int _x, int _y, int _width, int _height);

因此,您需要将代码中的行更改为:

So you need to change the line in your code as:

Rect region = Rect(x, y, width, heigth);

似乎你传递了左上角和右下角的坐标。如果你想这样做,使用其他的构造函数:

It seems that you instead passed the coordinates of the top left and bottom right corners. If you want to do so, use this other constructor:

Rect(const Point& pt1, const Point& pt2);

,您可以这样做:

Rect region = Rect(Point(x, y), Point(x+width, y+heigth));

这篇关于如何划分矩形子区域中的OpenCV Mat?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆