laplace.py 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #!/usr/bin/env python
  2. '''
  3. This program demonstrates Laplace point/edge detection using
  4. OpenCV function Laplacian()
  5. It captures from the camera of your choice: 0, 1, ... default 0
  6. Usage:
  7. python laplace.py <ddepth> <smoothType> <sigma>
  8. If no arguments given default arguments will be used.
  9. Keyboard Shortcuts:
  10. Press space bar to exit the program.
  11. '''
  12. # Python 2/3 compatibility
  13. from __future__ import print_function
  14. import numpy as np
  15. import cv2 as cv
  16. import sys
  17. def main():
  18. # Declare the variables we are going to use
  19. ddepth = cv.CV_16S
  20. smoothType = "MedianBlur"
  21. sigma = 3
  22. if len(sys.argv)==4:
  23. ddepth = sys.argv[1]
  24. smoothType = sys.argv[2]
  25. sigma = sys.argv[3]
  26. # Taking input from the camera
  27. cap=cv.VideoCapture(0)
  28. # Create Window and Trackbar
  29. cv.namedWindow("Laplace of Image", cv.WINDOW_AUTOSIZE)
  30. cv.createTrackbar("Kernel Size Bar", "Laplace of Image", sigma, 15, lambda x:x)
  31. # Printing frame width, height and FPS
  32. print("=="*40)
  33. print("Frame Width: ", cap.get(cv.CAP_PROP_FRAME_WIDTH), "Frame Height: ", cap.get(cv.CAP_PROP_FRAME_HEIGHT), "FPS: ", cap.get(cv.CAP_PROP_FPS))
  34. while True:
  35. # Reading input from the camera
  36. ret, frame = cap.read()
  37. if ret == False:
  38. print("Can't open camera/video stream")
  39. break
  40. # Taking input/position from the trackbar
  41. sigma = cv.getTrackbarPos("Kernel Size Bar", "Laplace of Image")
  42. # Setting kernel size
  43. ksize = (sigma*5)|1
  44. # Removing noise by blurring with a filter
  45. if smoothType == "GAUSSIAN":
  46. smoothed = cv.GaussianBlur(frame, (ksize, ksize), sigma, sigma)
  47. if smoothType == "BLUR":
  48. smoothed = cv.blur(frame, (ksize, ksize))
  49. if smoothType == "MedianBlur":
  50. smoothed = cv.medianBlur(frame, ksize)
  51. # Apply Laplace function
  52. laplace = cv.Laplacian(smoothed, ddepth, 5)
  53. # Converting back to uint8
  54. result = cv.convertScaleAbs(laplace, (sigma+1)*0.25)
  55. # Display Output
  56. cv.imshow("Laplace of Image", result)
  57. k = cv.waitKey(30)
  58. if k == 27:
  59. return
  60. if __name__ == "__main__":
  61. print(__doc__)
  62. main()
  63. cv.destroyAllWindows()