直线检测问题

For slam, 直线相对于点这些特征来说会更robust, 更常见。 毕竟点特征更需要环境纹理信息很丰富。

刚好最近瞎看了一些关于直线检测的问题。

#############################霍夫变换########################

第一个canny edge检测,她基于梯度可以检测出图像的大部分边缘线。 基本步骤canny

然后如何人基于边缘找直线呢?

1)  Hough Transform

2) 利用线的方向 

Straight line detector =

canny + gradient orientations +orientation binning +linking + check for straightness

#############LSD:a Line Segment Detector####################

可以参考别人的一个博客,写的很清楚LSD

##############################################################

#python opencv自带的提取line的特征

def line_detection_save(self,fidin,rgb):

    gray= cv2.cvtColor(rgb, cv2.COLOR_BGR2GRAY)

lsd= cv2.createLineSegmentDetector(2)

# Detect lines in the image

    lines= lsd.detect(gray)# Position 0 of the returned tuple are the detected lines

    for dlinein lines[0]:

          x0= int(round(dline[0][0]))

           y0= int(round(dline[0][1]))

            x1= int(round(dline[0][2]))

            y1= int(round(dline[0][3]))

            string= str(x0)+ ' ' + str(y0)+ ' ' + str(x1)+ ' ' + str(y1)+ '\n'

        fidin.write(string)

return fidin

##########################################

霍夫变换,同样是opencv python的代码

############################################

img = cv2.imread('lines.jpg')

gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

edges = cv2.Canny(gray,50,150,apertureSize = 3)

minLineLength=100

lines = cv2.HoughLinesP(image=edges,rho=0.02,theta=np.pi/500, threshold=10,lines=np.array([]), minLineLength=minLineLength,maxLineGap=100)

a,b,c = lines.shape

for i in range(a):    

       cv2.line(img, (lines[i][0][0], lines[i][0][1]), (lines[i][0][2], lines[i][0][3]), (0, 0, 255), 3,  cv2.LINE_AA)   

      cv2.imwrite('houghlines5.jpg',img)

!!!!另外还有一些基于深度学习的方法,但是不是很robust,pixel误差比较大

#######################LSD line 检测##############

有开源代码,超级好用,推荐

你可能感兴趣的:(直线检测问题)