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