Skip to content Skip to sidebar Skip to footer

Hough Lines In OpenCV/Python

I'm trying to find hough lines in an image using opencv in python. My code is: import cv2 import numpy as np img = cv2.imread('DLMIA.png') gray = cv2.cvtColor(img,cv2.COLOR_BGR2GR

Solution 1:

I found the solution.

The code example only shows the first hough line.

In case you want to print all the hough lines on an image you have to print all lines.

This is the corrected code:

import cv2
import numpy as np

img = cv2.imread('dave.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)


edges = cv2.Canny(gray,100,200,apertureSize = 3)
cv2.imshow('edges',edges)
cv2.waitKey(0)

minLineLength = 30
maxLineGap = 10
lines = cv2.HoughLinesP(edges,1,np.pi/180,15,minLineLength=minLineLength,maxLineGap=maxLineGap)
for x in range(0, len(lines)):
    for x1,y1,x2,y2 in lines[x]:
        cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2)

cv2.imshow('hough',img)
cv2.waitKey(0)

Solution 2:

A bit more elegant solution would be to use for line in lines: for x1,y1,x2,y2 in line: ...


Solution 3:

I had faced the same problem it is because of the loop,

for x1,y1,x2,y2 in lines[0]:

it will take only the coordinates of first line. the array returned is 3d.

you can rectify it by using the following as loop:

for line in lines:
    for x1,y1,x2,y2 in line:

Post a Comment for "Hough Lines In OpenCV/Python"