垂直线条聚类分组问题求助:现有算法存在误差需优化
垂直线分组算法优化求助
我需要将坐标(x1,y1,x2,y2)相近的线条进行分组,已成功实现水平线分组,但垂直线分组存在误差。我采用了类似水平线的处理方法(仅反向排序并匹配Y轴操作),但结果未达预期。图像尺寸为2675*2275,恳请大家提供帮助。
问题代码
def connect_close_vertical_lines(Test_line_bg, group, distance_threshold=30): sorted_group = sorted(group, key=lambda line: line[1]) # sorted_group_vertical_avg = [] # total_x = 0 # len_x = 0 # for line in sorted_group: # x1, y1, x2, y2 = line[:4] # total_x += x1 # len_x += 1 # avg_x = total_x / len_x # for line in sorted_group: # x1, y1, x2, y2 = line[:4] # x1 = x2 = avg_x # sorted_group_vertical_avg.append((x1, y1, x2, y2)) connected_line = sorted_group[0] for i in range(1, len(sorted_group)): line = sorted_group[i] x1, y1, x2, y2 = map(int, connected_line[:4]) x3, y3, x4, y4 = map(int, line[:4]) dist_2 = abs(x2 - x3) if dist_2 < 30: distance_threshold = distance_threshold*2 dist = abs(y2 - y3) if dist < distance_threshold: connected_line = (x1, y1, x1, y4) else: connected_lines_drawn_vertical.append(connected_line) cv2.line(Test_line_bg, (x1, y1), (x1, y2), (255, 255, 255), 3) connected_line = line connected_lines_drawn_vertical.append(connected_line) x1, y1, x2, y2 = map(int, connected_line[:4]) cv2.line(Test_line_bg, (x1, y1), (x1, y2), (255, 255, 255), 3)
补充说明
- 效果示意图:显示部分垂直线未被正确合并,仍存在零散短线条
- 坐标数据:包含待处理的垂直线坐标集合
内容的提问来源于stack exchange,提问作者lee
相关产品推荐
相关产品推荐

