Guest User

Untitled

a guest
Jul 11th, 2018
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. import argparse
  2. import cv2 as cv
  3. import numpy as np
  4.  
  5. parser = argparse.ArgumentParser(description="Find bright spots in an image.")
  6. parser.add_argument("image_path", help="path of the image file.")
  7. parser.add_argument("--threshold", type=int, default=0,
  8. help="Threshold for binary threshold. Set to 0 to use"
  9. " automatic thresholding.")
  10. parser.add_argument("--contour-area-lower-bound", type=int, default=0.01,
  11. help="lower bound for contour area.")
  12. parser.add_argument("--contour-area-upper-bound", type=int, default=100,
  13. help="upper bound for contour area.")
  14. parser.add_argument("--arc-length-lower-bound", type=int, default=0,
  15. help="lower bound for contour arc length.")
  16. parser.add_argument("--arc-length-upper-bound", type=int, default=80,
  17. help="upper bound for contour arc length.")
  18. parser.add_argument("--contour-color",
  19. choices=["red", "green", "blue", "black", "white"],
  20. default="red",
  21. help="color of the contours.")
  22. parser.add_argument("--contour-thickness", type=int, default=2,
  23. help="thickness of the contour.")
  24. args = parser.parse_args()
  25.  
  26. image = cv.imread(args.image_path)
  27. cv.imshow("Image", image)
  28. print("Press key to continue...")
  29. cv.waitKey(0)
  30.  
  31. image_gray = cv.cvtColor(image, cv.COLOR_RGB2GRAY)
  32. image_blurred = cv.GaussianBlur(image_gray, (11, 11), 0)
  33.  
  34. if args.threshold == 0:
  35. # Automatic thresholding with Otsu's method
  36. threshold, mask = cv.threshold(image_gray, 0, 255,
  37. cv.THRESH_BINARY+cv.THRESH_OTSU)
  38. else:
  39. threshold, mask = cv.threshold(image_gray, args.threshold, 255,
  40. cv.THRESH_BINARY)
  41.  
  42. cv.imshow("Image", mask)
  43. print("\nThresholded image")
  44. print("Press key to continue...")
  45. cv.waitKey(0)
  46. _, contours, hierarchy = cv.findContours(mask, cv.RETR_TREE,
  47. cv.CHAIN_APPROX_SIMPLE)
  48. print("\nFound {} contours.".format(len(contours)))
  49.  
  50. def filter_contour(contour):
  51. area = cv.contourArea(contour)
  52. arc_length = cv.arcLength(contour, closed=True)
  53. return (area > args.contour_area_lower_bound
  54. and area < args.contour_area_upper_bound
  55. and arc_length > args.arc_length_lower_bound
  56. and arc_length < args.arc_length_upper_bound)
  57.  
  58. filtered_contours = list(filter(filter_contour, contours))
  59.  
  60. # Set color
  61. if args.contour_color == "red":
  62. color = (0, 0, 255)
  63. elif args.contour_color == "green":
  64. color = (0, 255, 0)
  65. elif args.contour_color == "blue":
  66. color = (255, 0, 0)
  67. elif args.contour_color == "black":
  68. color = (0, 0, 0)
  69. elif args.contour_color == "white":
  70. color = (255, 255, 255)
  71.  
  72. cv.drawContours(image, filtered_contours, -1, color, args.contour_thickness)
  73. cv.imshow("Image", image)
  74. print("Found {} spots.".format(len(filtered_contours)))
  75. print("Press key to exit...")
  76. cv.waitKey(0)
Add Comment
Please, Sign In to add comment