组合图像中重叠的组
Combining overlapping groups in an image
我正在使用opencv_contrib来检测图像中的文本区域。
这是的原始图像
这是找到文本区域后的图像:
可以看出,图像中存在重叠的组。例如,围绕Hello World
和围绕Some more sample text
似乎有两组
问题在这样的场景中,如何通过合并两个框来保持尽可能宽的框。对于这些示例,它将是以H
开始并以d
结束的示例,从而覆盖Hello World
。我这样做的原因是,我想裁剪此图像的一部分,并将其发送到tesseract。
这是绘制方框的相关代码。
void groups_draw(Mat &src, vector<Rect> &groups)
{
for (int i=(int)groups.size()-1; i>=0; i--)
{
if (src.type() == CV_8UC3)
rectangle(src,groups.at(i).tl(),groups.at(i).br(),Scalar( 0, 255, 255 ), 2, 8 );
}
}
这是我试过的。我的想法在评论中。
void groups_draw(Mat &src, vector<Rect> &groups)
{
int previous_tl_x = 0;
int previous_tl_y = 0;
int prevoius_br_x = 0;
int previous_br_y = 0;
//sort the groups from lowest to largest.
for (int i=(int)groups.size()-1; i>=0; i--)
{
//if previous_tl_x is smaller than current_tl_x then keep the current one.
//if previous_br_x is smaller than current_br_x then keep the current one.
if (src.type() == CV_8UC3) {
//crop the image
Mat cropedImage = src(Rect(Point(groups.at(i).tl().x, groups.at(i).tl().y),Point(groups.at(i).br().x, groups.at(i).br().y)));
imshow("cropped",cropedImage);
waitKey(-1);
}
}
}
更新
我正在尝试使用[groupRectangles][4]
来实现这一点:
void groups_draw(Mat &src, vector<Rect> &groups)
{
vector<Rect> rects;
for (int i=(int)groups.size()-1; i>=0; i--)
{
rects.push_back(groups.at(i));
}
groupRectangles(rects, 1, 0.2);
}
然而,这给了我一个错误:
textdetection.cpp:106:5: error: use of undeclared identifier 'groupRectangles'
groupRectangles(rects, 1, 0.2);
^
1 error generated.
首先,得到重叠边界框的原因是文本检测器模块在反转通道上工作(例如:灰色和反转灰色),因此一些字符(如o和g)的内部区域被错误地检测并分组为字符。因此,如果您只想检测一种文本模式(深色背景上的白色文本),只需通过反转通道即可。替换:
for (int c = 0; c < cn-1; c++)
channels.push_back(255-channels[c]);
带有:
for (int c = 0; c < cn-1; c++)
channels[c] = (255-channels[c]);
现在,对于您的问题,矩形已经定义了交集和组合运算符:
rect = rect1 & rect2 (rectangle intersection)
rect = rect1 | rect2 (minimum area rectangle containing rect2 and rect3 )
rect &= rect1, rect |= rect1 (and the corresponding augmenting operations)
您可以在迭代矩形时使用这些运算符来检测相交的矩形并将其组合,如下所示:
if ((rect1 & rect2).area() != 0)
rect1 |= rect2;
编辑:
首先,按面积从大到小对矩形组进行排序:
std::sort(groups.begin(), groups.end(),
[](const cv::Rect &rect1, const cv::Rect &rect2) -> bool {return rect1.area() > rect2.area();});
然后,在矩形上迭代,当两个矩形相交时,将较小的添加到较大的,然后删除它:
for (int i = 0; i < groups.size(); i++)
{
for (int j = i + 1; j < groups.size(); j++)
{
if ((groups[i] & groups[j]).area() != 0)
{
groups[i] |= groups[j];
groups.erase(groups.begin() + j--);
}
}
}
一种方法是将每个矩形与其他矩形进行比较,看看它们是否重叠或相交。如果它们的数量足够,你可以把它们组合成一个更大的矩形。
相关文章:
- Ardunio UNO解决了多个重叠的定时器循环
- 如何在OMNET++中指定与命令行参数组合的输出文件名
- 可组合的lambda/std::函数与std::可选
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 在c++中检查长方体是否尽可能快地重叠(无迭代)
- C / C++ 移位/偏移/向左或向右移动位图?
- 混合组合和继承的C++问题
- 我需要将多个函数组合为一个函数
- 构建可组合有向图(扫描仪生成器的汤普森构造算法)
- 算术序列与区间的最大重叠
- 通过组合不同的类型来创建唯一的id
- 用常见虚拟函数实现的任意组合来实现派生类的正确方法是什么
- 在无向图中打印循环
- 模板元编程:如何将参数包组合成新的参数包
- 组合图:C 是否有TensorFlow Import_graph_def当量
- 使用 CGAL 简化组合图
- 选择位掩码中与选择器位图中的1位重叠的设置位的跨度
- 使用重叠的关键帧合并两个未定义的贴图
- 组合图像中重叠的组
- 基于关键点组合两个贴图的值