Skip to content

Instantly share code, notes, and snippets.

@TonsOfFun
Created May 22, 2018 02:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TonsOfFun/75cc04248d534a88c6dbcfd98be20033 to your computer and use it in GitHub Desktop.
Save TonsOfFun/75cc04248d534a88c6dbcfd98be20033 to your computer and use it in GitHub Desktop.
# Defined bounds using https://github.com/jrosebr1/imutils/blob/master/bin/range-detector
leaf_lower = (29, 0, 0)
leaf_upper = (255, 255, 128)
def leaf_mask(bgr_image):
frame = imutils.resize(bgr_image, width=500)
blurred = cv2.GaussianBlur(frame, (11, 11), 0)
hsv = cv2.cvtColor(blurred, cv2.COLOR_BGR2HSV)
# construct a mask for the color "green", then perform
# a series of dilations and erosions to remove any small
# blobs left in the mask
mask = cv2.inRange(hsv, leaf_lower, leaf_upper)
mask = cv2.erode(mask, None, iterations=4)
mask = cv2.dilate(mask, None, iterations=2)
return mask
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment