In [2]:
%matplotlib inline
from matplotlib import pyplot as plt, cm
import numpy as np
import cv2
from imutils.convenience import resize
In [6]:
# load the image
image = cv2.imread("./data/IMG_3906_1024.jpg")
image = resize(image, height=500)
In [7]:
# Our Goal: Detect the black shapes in the image.
# find all the 'black' shapes in the image
lower = np.array([0, 0, 0])
upper = np.array([15, 15, 15])
shapeMask = cv2.inRange(image, lower, upper)
# find the contours in the mask
(cnts, _) = cv2.findContours(shapeMask.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
print "I found %d black shapes" % (len(cnts))
plt.subplot()
plt.imshow(shapeMask, cmap = 'gray')
plt.figure(figsize=(14,10))
# loop over the contours
for c in cnts:
# draw the contour and show it
cv2.drawContours(image, [c], -1, (0, 255, 0), 2)
plt.subplot()
plt.imshow(image, cmap = 'gray')