1#!/usr/bin/env python 2 3''' 4This sample demonstrates Canny edge detection. 5 6Usage: 7 edge.py [<video source>] 8 9 Trackbars control edge thresholds. 10 11''' 12 13import cv2 14 15# relative module 16import video 17 18# built-in module 19import sys 20 21 22if __name__ == '__main__': 23 print __doc__ 24 25 try: 26 fn = sys.argv[1] 27 except: 28 fn = 0 29 30 def nothing(*arg): 31 pass 32 33 cv2.namedWindow('edge') 34 cv2.createTrackbar('thrs1', 'edge', 2000, 5000, nothing) 35 cv2.createTrackbar('thrs2', 'edge', 4000, 5000, nothing) 36 37 cap = video.create_capture(fn) 38 while True: 39 flag, img = cap.read() 40 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 41 thrs1 = cv2.getTrackbarPos('thrs1', 'edge') 42 thrs2 = cv2.getTrackbarPos('thrs2', 'edge') 43 edge = cv2.Canny(gray, thrs1, thrs2, apertureSize=5) 44 vis = img.copy() 45 vis /= 2 46 vis[edge != 0] = (0, 255, 0) 47 cv2.imshow('edge', vis) 48 ch = cv2.waitKey(5) & 0xFF 49 if ch == 27: 50 break 51 cv2.destroyAllWindows() 52