edge.py 1.1 KB

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