首页 > 解决方案 > Opencv - python - 从线段检测器(LSD)连接线段以形成一条线

问题描述

我正在研究寻找板球场的爆裂折痕的问题,我已经在一定程度上实现了我想要做的事情,即我可以使用来自 opencv 的 LSD 检测垂直线段,但我似乎不明白如何加入不同的线段在同一条线上组成一条完整的线。

查找线段的代码:

import cv2
import math
import numpy as np

#Read gray image
img = cv2.imread("2.jpg",0)

#Create default parametrization LSD
lsd = cv2.createLineSegmentDetector(0)

#Detect lines in the image
lines = lsd.detect(img)[0] #Position 0 of the returned tuple are the detected lines
print(lines[0][0][0])

ver_lines = []

for line in lines:
    angletan = math.degrees(math.atan2((round(line[0][3],2) - round(line[0][1],2)), (round(line[0][2],2) - round(line[0][0],2))))

    if(angletan > 85 and angletan < 95):
        ver_lines.append(line)

#Draw detected lines in the image
drawn_img = lsd.drawSegments(img,np.array(ver_lines))

#Show image
cv2.imshow("LSD",drawn_img )
cv2.waitKey(0)

输入图像:在此处输入图像描述

检测到的线路:在此处输入图像描述

我想拥有什么:在此处输入图像描述

标签: pythonopencvimage-processingstraight-line-detection

解决方案


查看霍夫投票。我已经将它用于类似的事情。

https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/py_houghlines/py_houghlines.html


推荐阅读