一.横平竖直”进行连线

解法1.将一些坐标点按照x相等,y相等连起来

解法1.根据 x或y总有一个相等的,用np.sum来找出和为1的点,然后在连起来,存在重复连线的问题.

import numpy as npcoord = np.array([[10, 60],[10, 20],[20, 20],[40, 40],[40, 60],[20, 40]])img = np.zeros((100, 100))h, w = coord.shapefor i in range(h):index = np.sum(np.array(coord[i]).reshape(-1, 2) == coord, axis=-1)y = np.where(index == 1)for point in coord[y]:cv2.line(img, (coord[i][0], coord[i][1]), (point[0], point[1]), color=(255,255,255), thickness=3)cv2.imshow('img', img)cv2.waitKey(0)

解法2.先连x相等,在连y相等的

def coord_sort_ind(x,y):#按照x,y来排序ind = np.lexsort((y.tolist(), x.tolist())) # Sort by x, then by yreturn ind
def simple_line_():import numpy as npcoord = np.array([[10, 60],[10, 20],[20, 20],[40, 40],[40, 60],[20, 40]])# 按照先X后Y排的序ind = coord_sort_ind(coord[:, 0], coord[:, 1])coord_X=coord[ind]print(coord_X)# # 按照先Y后X排的序ind = coord_sort_ind(coord[:, 1], coord[:, 0])print(ind)coord_Y = coord[ind]print(coord_Y)img = np.zeros((100, 100))h, w = coord.shape# x想等的连接for i in range(0, h, 2):cv2.line(img, (coord_X[i][0], coord_X[i][1]), (coord_X[i + 1][0], coord_X[i + 1][1]), color=(255, 255, 255),thickness=3)# y想等的连接cv2.line(img, (coord_Y[i][0], coord_Y[i][1]), (coord_Y[i + 1][0], coord_Y[i + 1][1]), color=(255, 255, 255),thickness=3)cv2.imshow('img', img)cv2.waitKey(0)

二.将相邻框进行合并

目前解法是将两个相邻的框合并,得到大框在继续合并,算法还需要优化成一次性合并成功

import numpy as np
import cv2
def get_cluster(points,x_dis,y1_dis):points = points[points[:, 0].argsort()]x1,y1,x2,y2=points[:,0],points[:,1],points[:,-2],points[:,-1]#只对竖值框进行合并操作points=points[np.where(x2-x1<y2-y1)[0]]# points=points[:13]# print('points:',points)# # debug to show# for i,point in enumerate(points):#         font = cv2.FONT_HERSHEY_SIMPLEX#         cv2.putText(img, str(i), (point[0], point[1]), font, 2.5, (255, 255, 255), 2)#         cv2.rectangle(img, (point[0], point[1]), (point[-2], point[-1]), color=(255, 255, 255), thickness=3)# cv2.imwrite('img_rect.jpg', img)# print(points.shape[0])cluster = []while points.shape[0]:temp=[]# print('points:',points)#x1-x2<index_x = np.where(abs(points[0][-2]-points[:, 0]) < x_dis)[0]# print('index_x:',index_x)# if len(index_x):#     #y1-y1<index_y1 = np.where(abs(points[0][1]-points[:, 1]) < y1_dis)[0]# print('index_y: ',index_y1)#y2-y2<hindex_y2 = np.where(abs(points[0][-1]-points[:, -1]) < (points[:, -1]-points[:, 1]))[0]# print('index_y2:',index_y2)# common_index=[i for i in index_x  if i in index_y]common_index = list(set(index_x) & set(index_y1) & set(index_y2))# print('common_index:',common_index)# print('common_index:',common_index)if len(common_index):temp.append(points[0])temp.extend(points[common_index])points = np.delete(points, obj=common_index+[0], axis=0)else:temp.append(points[0])points=points[1:]cluster.append(temp)# # print(points)# print('cluster:',cluster)# print(len(cluster))# for i in range(len(cluster)):#     every_cluster=np.array(cluster[i]).reshape(-1,4)#     print('every_cluster:',every_cluster)#     for point in every_cluster:#         font = cv2.FONT_HERSHEY_SIMPLEX#         cv2.putText(img, str(i), (point[0],point[1]), font, 2, (255, 255, 255), 2)#         cv2.rectangle(img,(point[0],point[1]),(point[-2],point[-1]),color=(255,255,255),thickness=3)# cv2.imwrite('img_rect_clu.jpg',img)return cluster
def fix_bbox_point(cluster):points=[]for i,evry_cluster in enumerate(cluster):every_cluster = np.array(cluster[i]).reshape(-1, 4)x1,y1,x2,y2=np.min(every_cluster[:,0]),np.min(every_cluster[:,1])\,np.max(every_cluster[:,-2]),np.max(every_cluster[:,-1])points.append([x1,y1,x2,y2])return np.array(points)def union_bboxs(points):cluster_first = get_cluster(points, x_dis=50, y1_dis=300)# print('===cluster=====')# print(len(cluster_first))first_bboxs = fix_bbox_point(cluster_first)cluster_second = get_cluster(first_bboxs, x_dis=50, y1_dis=300)# print('===cluster=====')# print(len(cluster_second))second_bboxs = fix_bbox_point(cluster_second)cluster_third = get_cluster(second_bboxs, x_dis=50, y1_dis=400)# print('===cluster=====')# print(len(cluster_third))third_bboxs = fix_bbox_point(cluster_third)return third_bboxsif __name__ == '__main__':# points = np.array([[1264, 1480, 1346, 1790],#                    [2278, 1873, 2380, 2542],#                    [3209, 2279, 3286, 2599],#                    [2195, 3468, 2277, 3772],#                    [844, 1087, 924, 1399],#                    [92, 876, 181, 1327],#                    [951, 306, 1033, 596],#                    [1652, 299, 1729, 604],#                    [2196, 1957, 2270, 2535],#                    [916, 4251, 994, 4563],#                    [1131, 1915, 1214, 2505],#                    [1042, 265, 1116, 532],#                    [2124, 2036, 2195, 2603],#                    [934, 1054, 1003, 1258],#                    [1522, 3561, 1585, 4086],#                    [999, 4220, 1068, 4422],#                    [2404, 586, 2516, 1209],#                    [1452, 3618, 1528, 4151],#                    [1354, 1449, 1425, 1655],#                    [1583, 3532, 1683, 4109],#                    [1207, 1525, 1256, 1792],#                    [2144, 3511, 2191, 3779],#                    [3162, 2319, 3205, 2549],#                    [787, 1124, 837, 1395],#                    [896, 339, 944, 606],#                    [1741, 260, 1813, 467],#                    [219, 3230, 330, 3785],#                    [1424, 2675, 1506, 2818],#                    [2528, 538, 2714, 1709],#                    [2287, 664, 2394, 1596],#                    [231, 1703, 428, 2598],#                    [287, 248, 473, 1638],#                    [186, 280, 265, 1025],#                    [1670, 3480, 1756, 3852],#                    [1754, 2277, 1836, 2431],#                    [3284, 2241, 3360, 2455],#                    [2823, 1662, 3163, 1730],#                    [2897, 2852, 3285, 2946],#                    [2972, 2700, 3049, 2790]])points=np.array([[700,1888,  789, 2199],[1918, 1487, 2000, 1798],[1045,  683, 1129,  995],[2867,  279, 2975,  958],[1929, 3088, 2012, 3393],[1006,  272, 1087,  583],[2476, 3089, 2560, 3396],[ 820, 3094,  904, 3406],[2532, 3499, 2616, 3810],[1335, 1080, 1411, 1378],[2204, 2283, 2288, 2587],[1138,  644, 1213,  975],[1611, 1884, 1695, 2196],[2823, 2283, 2906, 2603],[ 773, 2286,  853, 2606],[1562,  262, 1642,  585],[2458, 1883, 2540, 2196],[1749, 3491, 1831, 3803],[ 634, 1266,  715, 1785],[1646,  236, 1722,  515],[ 988, 3889, 1060, 4221],[ 725, 1167,  836, 1743],[2592,  455, 2703, 1365],[2721,  375, 2853, 1289],[ 846, 1104,  938, 1481],[ 857, 2255,  935, 2583],[1419, 1043, 1494, 1316],[1707, 2310, 1814, 3001],[1327, 3046, 1405, 3387],[1244, 3081, 1327, 3404],[1814, 2264, 1903, 2656],[2543, 1844, 2617, 2053],[2845, 3894, 2918, 4215],[2147, 2330, 2197, 2559],[2402, 1922, 2452, 2198],[1058, 3860, 1122, 4065],[3308, 1779, 3396, 2183],[2293, 2248, 2370, 2454],[1857, 1531, 1908, 1760],[1697, 3535, 1745, 3803],[1189, 3133, 1236, 3367],[2419, 3135, 2468, 3360],[1873, 3124, 1923, 3400],[ 988,  719, 1038,  995],[2480, 3539, 2529, 3809],[2017, 3057, 2083, 3278],[2095, 2368, 2138, 2598],[ 766, 3135,  815, 3409],[1559, 1964, 1607, 2160],[1504, 1966, 1550, 2202],[2913, 2248, 2996, 2455],[ 766, 4318,  838, 4580],[1802, 1571, 1848, 1797],[ 374,  801,  457, 1032],[ 637, 1926,  690, 2164],[1636, 2441, 1708, 2998],[ 964,  353, 1012,  549],[3243,  290, 3389, 1431],[1869, 3963, 1952, 4206],[3131, 2120, 3215, 2800],[2858, 4314, 2934, 4593],[2802, 3939, 2844, 4189],[ 484,  716,  570,  955],[2913, 3860, 2988, 4126],[ 330,  645,  566,  717]])# print(points[30:33])# points = points[12:21]img = np.zeros((5000, 4000))cluster_first=get_cluster(points,x_dis=50,y1_dis=300)print('===first cluster=====')print(len(cluster_first))first_bboxs=fix_bbox_point(cluster_first)print('===second cluster===')cluster_second = get_cluster(first_bboxs, x_dis=50, y1_dis=400)print(len(cluster_second))second_bboxs = fix_bbox_point(cluster_second)# #debug# for i,point in enumerate(first_bboxs):#     font = cv2.FONT_HERSHEY_SIMPLEX#     cv2.putText(img, str(i), (point[0], point[1]), font, 2.5, (255, 255, 255), 2)#     cv2.rectangle(img, (point[0], point[1]), (point[-2], point[-1]), color=(255, 255, 255), thickness=3)# cv2.imwrite('img_rect_first_bbx.jpg', img)### # debug# for i, point in enumerate(second_bboxs):#     font = cv2.FONT_HERSHEY_SIMPLEX#     cv2.putText(img, str(i), (point[0], point[1]), font, 2.5, (255, 255, 255), 2)#     cv2.rectangle(img, (point[0], point[1]), (point[-2], point[-1]), color=(255, 255, 255), thickness=3)# cv2.imwrite('img_rect_second_bbx.jpg', img)cluster_third = get_cluster(second_bboxs, x_dis=50, y1_dis=400)print('===cluster=====')print(len(cluster_third))third_bboxs = fix_bbox_point(cluster_third)# debugfor i, point in enumerate(third_bboxs):font = cv2.FONT_HERSHEY_SIMPLEXcv2.putText(img, str(i), (point[0], point[1]), font, 2.5, (255, 255, 255), 2)cv2.rectangle(img, (point[0], point[1]), (point[-2], point[-1]), color=(255, 255, 255), thickness=3)cv2.imwrite('img_rect_third_bbx.jpg', img)

合并前:

合并后:

“横平竖直”进行连线+将相邻框进行合并相关推荐

  1. OCR文字检测框的合并

    OCR文字检测框的合并 项目的github地址:https://github.com/zcswdt/merge_text_boxs 在我们使用文字检测模型的对文本进行检测的时候,可能效果不能如愿以偿, ...

  2. html怎么表单变成一条线,让文本框变成一条直线

    让文本框的边框变成一条线,很多朋友都喜欢把他的文本框变为一条横线, 体现个性.本例中还加入了背景透明,两者配合相信会有更加不错的效果.这样的话你就可以使用一张图片作为表单的背景,从而起到美化表单的作用 ...

  3. 基于socket的线上聊天框

    聊天框1.0 使用说明: 要求Linux环境,虚拟机或者WSL均可 命令行执行 g++ Server.cpp -o Server 和 g++ Client.cpp -o Client 进行编译 线程A ...

  4. echarts多线图表 提示框自定义

    需求:在提示框中加上单位 效果: 解决方案: 第一步.图表子组件中将tooltip赋值 tooltip: tooltip, setOptions({ seriesData, xAxisData, to ...

  5. CAD用多义线动态画矩形框

    动态拖放时的绘制事件: 1 2 3 4 5 6 7 8 9 10 11     function DynWorldDrawMatrix1( pCustomEntity, pWorldDraw, cur ...

  6. html设置表格外框线内框线6,Word里的表格怎么分别设置外框线和内框线?我怎么弄都不行?...

    回答: 首先,打开Word文档,将需要调整的表格整体选中,如下图所示. 请点击输入图片描述 接着,打开"设计"菜单,点击菜单栏右侧的"笔颜色"选项,然后根据自己 ...

  7. R中大数据量数据框的合并慎重使用rbind

    最近在用R处理百万级的数据,程序本身是线性扫描,可是随着数据量的增加,运行时间却不是线性增加,一度几天都运行不完. 怀疑是其中rbind函数造成的,查询到这篇文章,也说了这个问题 http://blo ...

  8. argcgis线裁剪线、多图层线合并为一个图层

    1.线裁剪线 在arcgis里用矢量线段裁剪线段,1.开始编辑,选择你要编辑的线段,选中要裁剪的线段和裁剪器线段,选中高级编辑工具里的"修剪工具",就裁好了. 2.多图层线合并为一 ...

  9. easyui datagrid不是相邻的能合并单元格吗_万能的Ctrl+E快捷键,学会能一键批量解决Excel中90%的问题!...

    Hello,各位叨友们好呀!我是叨叨君~ Excel中你们最常用的快捷键是啥?Ctrl+C复制.Ctrl+V粘贴?其实除了这两大快捷键外,还有一个少有人用的快捷键「Ctrl+E」,它被大神们称为表格中 ...

最新文章

  1. Spring Boot错误处理机制以及定制自己的错误页面
  2. Hadoop Yarn公平调度器的特点、缺额、DRF策略
  3. Daily Scrum Meeting ——ZeroDay(Beta)12.08
  4. 伪元素::before与::after的用法
  5. Redis存储(实现)原理
  6. PhotoShop基础知识
  7. python3捕获异常_python中异常捕获方法详解
  8. [MySQL]SQL
  9. 11.9 noip模拟试题
  10. Advertising on Instagram 如何在Instagram上发布广告 Lynda课程中文字幕
  11. SpringBoot内置servlet容器分析
  12. suse linux安装rpm包,suse linux rpm 安装
  13. 什么叫做股票实时行情接口api?
  14. python.exe无法找到入口 无法定位程序输入点
  15. 【Python】Marshmallow:Python中的“棉花糖”
  16. iFunk超极本或出新,你最想知道什么
  17. B站怎么就成为了教育类的视频网站了
  18. 区块链在司法存证领域的应用报告 | 陀螺研究院
  19. 日语二级语法汇总(part4/16)
  20. fft画图出现乱直线情况

热门文章

  1. RestQL:现代化的 API 开发方式
  2. 美团AI全景图:吃喝玩乐背后的黑科技
  3. 阿里P8架构师谈:Zookeeper的原理和架构设计,以及应用场景
  4. Java经典基础与高级面试36题和答案
  5. DGL_图的创建、保存、加载
  6. 滴水课后作业(1-5)
  7. Python shutil.md
  8. 织梦后台如何生成站点地图sitemap.xml
  9. css在盒子中垂直居中和固定居中
  10. 使用 JavaScript 实现简单候选项推荐功能(模糊搜索)【收藏】【转】