OpenCV groupRectangles - 获取分组和未分组的矩形

2021-12-10 00:00:00 opencv rectangles c++

我正在使用 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);

相关文章