laplace_demo.py 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. """
  2. @file laplace_demo.py
  3. @brief Sample code showing how to detect edges using the Laplace operator
  4. """
  5. import sys
  6. import cv2 as cv
  7. def main(argv):
  8. # [variables]
  9. # Declare the variables we are going to use
  10. ddepth = cv.CV_16S
  11. kernel_size = 3
  12. window_name = "Laplace Demo"
  13. # [variables]
  14. # [load]
  15. imageName = argv[0] if len(argv) > 0 else 'lena.jpg'
  16. src = cv.imread(cv.samples.findFile(imageName), cv.IMREAD_COLOR) # Load an image
  17. # Check if image is loaded fine
  18. if src is None:
  19. print ('Error opening image')
  20. print ('Program Arguments: [image_name -- default lena.jpg]')
  21. return -1
  22. # [load]
  23. # [reduce_noise]
  24. # Remove noise by blurring with a Gaussian filter
  25. src = cv.GaussianBlur(src, (3, 3), 0)
  26. # [reduce_noise]
  27. # [convert_to_gray]
  28. # Convert the image to grayscale
  29. src_gray = cv.cvtColor(src, cv.COLOR_BGR2GRAY)
  30. # [convert_to_gray]
  31. # Create Window
  32. cv.namedWindow(window_name, cv.WINDOW_AUTOSIZE)
  33. # [laplacian]
  34. # Apply Laplace function
  35. dst = cv.Laplacian(src_gray, ddepth, ksize=kernel_size)
  36. # [laplacian]
  37. # [convert]
  38. # converting back to uint8
  39. abs_dst = cv.convertScaleAbs(dst)
  40. # [convert]
  41. # [display]
  42. cv.imshow(window_name, abs_dst)
  43. cv.waitKey(0)
  44. # [display]
  45. return 0
  46. if __name__ == "__main__":
  47. main(sys.argv[1:])