hough_lines.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. import cv2
  2. import numpy as np
  3. import sys
  4. def onTrackbarChange(max_slider):
  5. global img
  6. global dst
  7. global gray
  8. dst = np.copy(img)
  9. th1 = max_slider
  10. th2 = th1 * 0.4
  11. edges = cv2.Canny(img, th1, th2)
  12. # Apply probabilistic hough line transform
  13. lines = cv2.HoughLinesP(edges, 2, np.pi/180.0, 50, minLineLength=10, maxLineGap=100)
  14. # Draw lines on the detected points
  15. for line in lines:
  16. x1, y1, x2, y2 = line[0]
  17. cv2.line(dst, (x1, y1), (x2, y2), (0,0,255), 1)
  18. cv2.imshow("Result Image", dst)
  19. cv2.imshow("Edges",edges)
  20. if __name__ == "__main__":
  21. # Read image
  22. img = cv2.imread('lanes.jpg')
  23. # Create a copy for later usage
  24. dst = np.copy(img)
  25. # Convert image to gray
  26. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  27. # Create display windows
  28. cv2.namedWindow("Edges")
  29. cv2.namedWindow("Result Image")
  30. # Initialize threshold value
  31. initThresh = 500
  32. # Maximum threshold value
  33. maxThresh = 1000
  34. cv2.createTrackbar("threshold", "Result Image", initThresh, maxThresh, onTrackbarChange)
  35. onTrackbarChange(initThresh)
  36. while True:
  37. key = cv2.waitKey(1)
  38. if key == 27:
  39. break
  40. cv2.destroyAllWindows()