图像处理opencv-Rect 排序、合并[转]
opencv进行rect检测时,当检测到多个rect,组成rect vector之后,有些rect是由一个区域误分割得到的,
可以按照某种规格将这些rect合并为一个rect。比如按照x,y,width,height特性。
可以先按照x坐标或者y坐标排序。
//按照X坐标排序
bool BOCR::rect_rank_x(vector<Rect> &vec_rects) {
Rect vec_temp;
for (int l = 1; l < vec_rects.size(); l++) {
for (int m = vec_rects.size() - 1; m >= l; m--) {
if (vec_rects[m].x < vec_rects[m - 1].x) {
vec_temp = vec_rects[m - 1];
vec_rects[m - 1] = vec_rects[m];
vec_rects[m] = vec_temp;
}
}
}
return true;
}
//按照X坐标排序
bool BOCR::rect_rank_y(vector<Rect> &vec_rects) {
Rect vec_temp;
for (int l = 1; l < vec_rects.size(); l++) {
for (int m = vec_rects.size() - 1; m >= l; m--) {
if (vec_rects[m].y < vec_rects[m - 1].y) {
vec_temp = vec_rects[m - 1];
vec_rects[m - 1] = vec_rects[m];
vec_rects[m] = vec_temp;
}
}
}
return true;
} /*将rect上下合并
* 参数:vec_rects:输入的所有的rect集合;
* vec_rects_out:输出的上下合并后的所有的rect集合;
* x_dif:进行上下合并的x差值;y_dif:进行上下合并的y差值;
* width:进行上下合并的width最大值;height:进行上下合并的height最大值;
width_rect:合并后的rect的width的值大于width_rect为满足条件
*/
bool BOCR::rect_combine_uplow(vector<Rect> &vec_rects,
vector<Rect>&vec_rects_out, int x_dif, int y_dif, int width, int height,
int width_rect) {
rect_rank_y(vec_rects);
//将上下部分分裂的,合并
int num_rect = vec_rects.size();
for (int j = 0; j < num_rect; j++) {
if (vec_rects[j].width > 0) {
Rect r;
for (int p = 0; p < num_rect; p++) {
if ((vec_rects[p].width > 0) && (p > j || p < j)) {
if ((((abs(vec_rects[p].x - vec_rects[j].x) < x_dif)
|| (abs(
vec_rects[p].x + vec_rects[p].width
- vec_rects[j].x
- vec_rects[j].width) < x_dif))
&& ((abs(
vec_rects[p].y
- (vec_rects[j].y
+ vec_rects[j].height))
< y_dif)
|| (abs(
vec_rects[j].y
- (vec_rects[p].y
+ vec_rects[p].height))
< y_dif))
&& (vec_rects[p].height < height)
&& (vec_rects[j].height < height)
&& (vec_rects[p].width < width)
&& (vec_rects[j].width < width))) { r.x = min(vec_rects[j].x, vec_rects[p].x);
r.y = min(vec_rects[j].y, vec_rects[p].y);
r.width = max(
vec_rects[p].x + vec_rects[p].width
- vec_rects[j].x,
vec_rects[j].x + vec_rects[j].width
- vec_rects[p].x);
r.height = max(
vec_rects[j].y + vec_rects[j].height
- vec_rects[p].y,
vec_rects[p].y + vec_rects[p].height
- vec_rects[j].y);
if (vec_rects[p].y < vec_rects[j].y) {
vec_rects[p].width = 0;
vec_rects[p].x = 0;
vec_rects[p].height = 0;
vec_rects[p].y = 0;
vec_rects[j] = r;
} else {
vec_rects[j].width = 0;
vec_rects[j].x = 0;
vec_rects[j].height = 0;
vec_rects[j].y = 0;
vec_rects[p] = r;
} }
}
}
}
} for (int j = 0; j < num_rect; j++) {
if (vec_rects[j].width > width_rect) {
vec_rects_out.push_back(vec_rects[j]);
}
}
return true;
} /*将rect左右合并
* 参数:
* show:输入图像;
* vec_rects:输入的所有的rect集合;
* vec_rects_out:输出的左右合并后的所有的rect集合;
* x_dif:进行左右合并的x差值;y_dif:进行左右合并的y差值;
* width:进行左右合并的width最大值;height:进行左右合并的height最大值;
* rate1:rect的长宽比最小值1;rate2:rect的长宽比最小值2;
* width_rect:合并后的rect的width的值大于width_rect为满足条件
*/
bool BOCR::rect_combine_leftright(Mat & show, vector<Rect> &vec_rects,
vector<Rect>&vec_rects_out, int x_dif, int y_dif, int width, int height,
double rate1, double rate2, int width_rect) {
int num = vec_rects.size();
for (int j = 0; j < num - 1; j++) {
if (vec_rects[j].width > 0) {
for (int q = j + 1; q < num; q++) {
if (vec_rects[q].width > 0) {
Rect r;
if ((max(vec_rects[q].x - x_dif, 0)
< min(vec_rects[j].x + vec_rects[j].width,
show.cols))
&& ((abs(vec_rects[q].y - vec_rects[j].y) < y_dif)
|| (abs(
min(
vec_rects[q].y
+ vec_rects[q].height,
show.rows)
- min(
vec_rects[j].y
+ vec_rects[j].height,
show.rows)) < y_dif))
&& (vec_rects[q].width < width)
&& (vec_rects[j].width < width)
&& (((vec_rects[q].height
/ (double) vec_rects[q].width > rate1)
&& (vec_rects[j].height
/ (double) vec_rects[j].width
> rate2))
|| ((vec_rects[j].height
/ (double) vec_rects[j].width
> rate1)
&& (vec_rects[q].height
/ (double) vec_rects[q].width
> rate2)))) {
if ((vec_rects[j].x + vec_rects[j].width
> show.cols / 10 * 8.5)
&& (vec_rects[q].x > show.cols / 10 * 8.5)
&& abs(vec_rects[j].width - vec_rects[q].width)
< 4
&& abs(
vec_rects[j].height
- vec_rects[q].height) < 3) {
;
} else {
r.x = vec_rects[j].x;
r.y = min(vec_rects[j].y, vec_rects[q].y);
r.width = vec_rects[q].x + vec_rects[q].width
- vec_rects[j].x;
r.height = max(vec_rects[j].y + vec_rects[j].height,
vec_rects[q].y + vec_rects[q].height) - r.y;
vec_rects[q].width = 0;
vec_rects[q].x = 0;
vec_rects[j] = r;
}
}
}
}
}
}
for (int j = 0; j < num; j++) {
if (vec_rects[j].width > width_rect) {
vec_rects_out.push_back(vec_rects[j]);
}
}
return true;
}
————————————————
版权声明:本文为CSDN博主「等待破茧」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/boon_228/article/details/51491789
图像处理opencv-Rect 排序、合并[转]的更多相关文章
- Python 图像处理 OpenCV (3):图像属性、图像感兴趣 ROI 区域及通道处理
前文传送门: 「Python 图像处理 OpenCV (1):入门」 「Python 图像处理 OpenCV (2):像素处理与 Numpy 操作以及 Matplotlib 显示图像」 图像属性 图像 ...
- Python 图像处理 OpenCV (16):图像直方图
前文传送门: 「Python 图像处理 OpenCV (1):入门」 「Python 图像处理 OpenCV (2):像素处理与 Numpy 操作以及 Matplotlib 显示图像」 「Python ...
- 排序合并连接(sort merge join)的原理
排序合并连接(sort merge join)的原理 排序合并连接(sort merge join)的原理 排序合并连接(sort merge join) 访问次数:两张表都只会访 ...
- oracle表连接------>排序合并连接(Merge Sort Join)
排序合并连接 (Sort Merge Join)是一种两个表在做连接时用排序操作(Sort)和合并操作(Merge)来得到连接结果集的连接方法. 对于排序合并连接的优缺点及适用场景例如以下: a,通常 ...
- oracle 表连接 - sort merge joins 排序合并连接
https://blog.csdn.net/dataminer_2007/article/details/41907581一. sort merge joins连接(排序合并连接) 原理 指的是两个表 ...
- Python 图像处理 OpenCV (2):像素处理与 Numpy 操作以及 Matplotlib 显示图像
前文传送门: 「Python 图像处理 OpenCV (1):入门」 普通操作 1. 读取像素 读取像素可以通过行坐标和列坐标来进行访问,灰度图像直接返回灰度值,彩色图像则返回B.G.R三个分量. 需 ...
- Python 图像处理 OpenCV (4):图像算数运算以及修改颜色空间
前文传送门: 「Python 图像处理 OpenCV (1):入门」 「Python 图像处理 OpenCV (2):像素处理与 Numpy 操作以及 Matplotlib 显示图像」 「Python ...
- Python 图像处理 OpenCV (5):图像的几何变换
前文传送门: 「Python 图像处理 OpenCV (1):入门」 「Python 图像处理 OpenCV (2):像素处理与 Numpy 操作以及 Matplotlib 显示图像」 「Python ...
- Python 图像处理 OpenCV (6):图像的阈值处理
前文传送门: 「Python 图像处理 OpenCV (1):入门」 「Python 图像处理 OpenCV (2):像素处理与 Numpy 操作以及 Matplotlib 显示图像」 「Python ...
- Python 图像处理 OpenCV (7):图像平滑(滤波)处理
前文传送门: 「Python 图像处理 OpenCV (1):入门」 「Python 图像处理 OpenCV (2):像素处理与 Numpy 操作以及 Matplotlib 显示图像」 「Python ...
随机推荐
- Linux信号1
信号(signal)是一种软中断,他提供了一种处理异步事件的方法,也是进程间唯一的异步通信方式.在Linux系统中,根据POSIX标准扩展以后的信号机制,不仅可以用来通知某进程发生了什么事件,还可以给 ...
- 【swift】用Xib实现自定义警告框(Alert)(安卓叫法:Dialog对话框)
在写这篇博客前,先感谢两篇博客 [如何自定义的思路]:https://www.cnblogs.com/apprendre-10-28/p/10507794.html [如何绑定Xib并且使用]:htt ...
- 【J-Link】J-Link不支持(版本太低)
事情起因,我原本可以烧录和仿真的(版本6.3.4),但是后来安装另一个东西,这个东西里面包含旧的J-Link驱动(版本5.1.2) 它把Keil文件夹下的JLinkARM.dll覆盖了,导致出现下面的 ...
- Ubantu nodejs卸载与二进制安装
#apt-get 卸载 sudo apt-get remove --purge npm sudo apt-get remove --purge nodejs sudo apt-get remove - ...
- Linux运维实战之磁盘分区、格式化及挂载(一)
在网络系统中,磁盘和文件系统管理是两个非常基本.同时也是非常重要的管理任务,特别是文件系统管理,因为它与用户权限和整个网络系统的安全息息相关.本次博文的主题是关于Linux系统中磁盘分区.格式化及挂载 ...
- 二、SpringBoot实现上传文件到fastDFS文件服务器
上篇文章介绍了如何使用docker安装fastDFS文件服务器,这一篇就介绍整合springBoot实现文件上传到fastDFS文件服务器 1.pom.xml文件添加依赖 <!-- 连接fast ...
- Echarts 实现tooltip自动显示自动播放
1.其实这个很容易实现,一个 dispatchAction 方法就解决问题:但是博主在未实现该功能时是花了大力气,各种百度,各种搜: 很难找到简单粗暴的例子,大多数随便回一句你的问题就没下文: 废话太 ...
- spring的不同事务传播行为和用途。
1.PROPAGATION_REQUIRED:如果当前没有事务,就创建一个事务,如果当前存在事务,就加入该事务,该设置是最常用的设置. 2.PROPAGATION_SUPPORTS:支持当前事务,如果 ...
- 【JAVA今法修真】 第一章 今法有万象 百家欲争鸣
大家好,我是南橘,因为这段时间很忙,忙着家里的事情,忙着工作的事情,忙着考试的事情,很多时候没有那么多经历去写新的东西,同时,也是看了网上一些比较新颖的文章输出方式,自己也就在想,我是不是也可以这样写 ...
- Excel如何使用VLOOKUP函数多条件匹配查找数据
一.对应源数据如sheet6所示,对应需查找的数据如sheet7所示 二.在sheet6中添加一列辅助列 三.在sheet7对应位置插入vlookup函数 四.最终结果如下图所示