lsd_lines_extraction.py 970 B

12345678910111213141516171819202122232425262728293031323334353637383940
  1. #!/usr/bin/env python
  2. '''
  3. This example shows the functionalities of lines extraction finished by LSDDetector class.
  4. USAGE: lsd_lines_extraction.py [<path_to_input_image>]
  5. '''
  6. import sys
  7. import cv2 as cv
  8. if __name__ == '__main__':
  9. print(__doc__)
  10. if len(sys.argv) > 1:
  11. fname = sys.argv[1]
  12. else :
  13. fname = '../data/corridor.jpg'
  14. img = cv.imread(fname)
  15. if img is None:
  16. print('Failed to load image file:', fname)
  17. sys.exit(1)
  18. gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
  19. lsd = cv.line_descriptor_LSDDetector.createLSDDetector()
  20. lines = lsd.detect(gray, 2, 1)
  21. for kl in lines:
  22. if kl.octave == 0:
  23. # cv.line only accepts integer coordinate
  24. pt1 = (int(kl.startPointX), int(kl.startPointY))
  25. pt2 = (int(kl.endPointX), int(kl.endPointY))
  26. cv.line(img, pt1, pt2, [255, 0, 0], 2)
  27. cv.imshow('output', img)
  28. cv.waitKey(0)
  29. cv.destroyAllWindows()