I am a newbie in both python
and opencv
and I am facing a problem in detecting lines in the following image, which has strips of black lines laid on the ground:
I used the following code:
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,50,150,apertureSize = 3)
print img.shape[1]
print img.shape
minLineLength = img.shape[1]-1
maxLineGap = 10
lines = cv2.HoughLinesP(edges,1,np.pi/180,100,minLineLength,maxLineGap)
for x1,y1,x2,y2 in lines[0]:
cv2.line(img,(x1,y1),(x2,y2),(0,255,0),2)
but it is unable to detect the lines accurately and only draws a green line on the first black strip from the bottom which does not even cover the entire line,
also,
please suggest a way of obtaining the y
cordinates of each line.
Sanj,
a modified code which detects not one but many Hough lines is shown below. I have improved the way how to loop through the lines array so that you get many more line segments.
You can further tune the parameters, however, I think that the contour approach in your other post will most likely be the better approach to solve your task, as shown there: How to detect horizontal lines in an image and obtain its y-coordinates using python and opencv?
import numpy as np
import cv2
img = cv2.imread('lines.jpg')
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray,50,150,apertureSize = 3)
print img.shape[1]
print img.shape
minLineLength=img.shape[1]-300
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.imshow('edges', edges)
cv2.imshow('result', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With