0

I followed a tutorial of python openCV, and was trying to use HoughLinesP() to detect lines, here is the code:

imgLoc = '...\lines.jpg'

img = cv2.imread(imgLoc)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray, 50, 100)
minLineLength = 20; maxLineGap = 5
lines = cv2.HoughLinesP(edges, 1, np.pi/180, 100, \
    minLineLength, maxLineGap)
for [[x1, y1, x2, y2]] in lines:
    cv2.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2)
cv2.imshow('line', img)
cv2.waitKey(); cv2.destroyAllWindows()

And here's what I get: floatingLines apparently very strange. there's so many lines in the sky which I never expected, and I thought there would be more vertical lines on the building, but there's not.

The tutorial doesn't give pictures demonstrating how the result should be, so I have no idea if that's normal or not.

So, Is there any problem in my code that lead to that wired image? If it does, can I make some change to let it detect more vertical line?

==========Update_1============

I followed the comment's suggestion, now more line can be detected:

#minLineLength = 20; maxLineGap = 5
lines = cv2.HoughLinesP(edges, 1, np.pi/180, 100, \
    minLineLength = 10, maxLineGap = 5)

lineDetection2.0 But still vertical lines are lacked. And the Canny() result: Canny

in the result of Canny() there ARE vertical edges, why would they disappeared after HoughLinesP()? (Or that's just visual error?)

==========Update_2============

I added a blur and the value of minLineLength:

gray = cv2.GaussianBlur(gray, (5, 5), 0)
...
lines = cv2.HoughLinesP(edges, 1, np.pi/180, 100, \
        minLineLength = 50, maxLineGap = 5)

The result is clearer, but still not much vertical lines... Edges detected And I started to wonder where does these slashes comes from

Amarth Gûl
  • 1,040
  • 2
  • 14
  • 33
  • 2
    Are you sure that this question hasn't been asked and answered here before? https://stackoverflow.com/questions/36452491/opencv-python-houghlinesp-strange-results – quamrana Jul 28 '17 at 14:06
  • Possible duplicate of [Opencv python HoughLinesP strange results](https://stackoverflow.com/questions/36452491/opencv-python-houghlinesp-strange-results) – Dan Mašek Jul 28 '17 at 14:22
  • @DanMašek Thanks, that question do helps a lot – Amarth Gûl Jul 28 '17 at 14:33
  • Hough transform detects lines by drawing imaginary lines on the image and counting the points on them, to select those with more points than given in the threshold parameter.Assuming you want only the vertical lines, you can get rid of the angular lines by either eliminating all lines with those angles, or partitioning the image into strips and detecting lines on each of them. To detect only the continuous lines, you will need to modify the Hough lines algorithm or preprocess the Canny image. – Totoro Aug 02 '17 at 00:46

1 Answers1

1

It might be worth starting with some smoothing before performing Canny edge detection.

I suggest higher values for minLineLength (and maxLineGap, though this should not be very large). That should get rid of smaller lines and connect vertical line segments where detected. If that still does not bring out the vertical lines, we might have to look in to the threshold parameter.

Totoro
  • 3,398
  • 1
  • 24
  • 39