Skip to content

Instantly share code, notes, and snippets.

@TonsOfFun
Created January 16, 2019 06:34
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/13c6428c5bd7d7d044dd851d80374511 to your computer and use it in GitHub Desktop.
Save TonsOfFun/13c6428c5bd7d7d044dd851d80374511 to your computer and use it in GitHub Desktop.
GreenThumb CV Leaf Area Filter
import numpy as np
import imutils
import cv2
import matplotlib.pyplot as plt
%matplotlib inline
# 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
v2.countNonZero(week_1_mask)
week_1_mask = leaf_mask(week_1)
plt.title('Week 1 Leaf Area')
week_1_image = imutils.resize(week_1, width=500)
week_1_output = cv2.bitwise_and(week_1_image, week_1_image, mask = week_1_mask)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment