OpenCV groupRectangles - 获取分组和未分组的矩形 [英] OpenCV groupRectangles - getting grouped and ungrouped rectangles

查看:2336
本文介绍了OpenCV groupRectangles - 获取分组和未分组的矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用OpenCV,并且希望将具有显着重叠的矩形分组在一起。我已经尝试使用 groupRectangles 为此,它需要一个组阈值参数。阈值为0时,它根本不进行任何分组,阈值为1时,只返回至少为2个矩形的矩形。例如,给定下面图像左侧的矩形,最后得到右侧的2个矩形:

I'm using OpenCV and want to group together rectangles that have significant overlap. I've tried using groupRectangles for this, which takes a group threshold argument. With a threshold of 0 it doesn't do any grouping at all, and with a threshold of 1 is only returns rectangles that were the result of at least 2 rectangles. For example, given the rectangles on the left in the image below you end up with the 2 rectangles on the right:

我想要的最终是3个矩形。上图中右侧的2,加上左侧图像右上角的矩形,不与任何其他矩形重叠。

What I'd like to end up with is 3 rectangles. The 2 on the right in the image above, plus the rectangle in the top right of the image to the left that doesn't overlap with any other rectangles. What's the best way to achieve this?

推荐答案

我最终使用的解决方案是在调用之前复制所有初始矩形 groupRectangles 。这样,每个输入矩形都保证与至少一个其他矩形分组,并且将出现在输出中:

The solution I ended up going with was to duplicate all of the initial rectangles before calling groupRectangles. That way every input rectangle is guaranteed to be grouped with at least one other rectangle, and will appear in the output:

int size = rects.size();
for( int i = 0; i < size; i++ )
{
    rects.push_back(Rect(rects[i]));
}
groupRectangles(rects, 1, 0.2);

这篇关于OpenCV groupRectangles - 获取分组和未分组的矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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