In [1]:
%matplotlib inline
from matplotlib import pyplot as plt, cm
import numpy as np
import cv2
In [2]:
# load the image
image = cv2.imread("./data/gameboy.png")
In [5]:
# defining a list of boundaries in the RGB color space
# saying that all pixels in our image that have a
# R >= 100, B >= 15, and G >= 17 along with R <= 200, B <= 56, and G <= 50
# will be considered red
boundaries = [
([17, 15, 100], [50, 56, 200]),
([86, 31, 4], [220, 88, 50]),
([25, 146, 190], [62, 174, 250]),
([103, 86, 65], [145, 133, 128])
]
# loop over the boundaries
for (lower, upper) in boundaries:
# create NumPy arrays from the boundaries
lower = np.array(lower, dtype = "uint8")
upper = np.array(upper, dtype = "uint8")
# find the colors within the specified boundaries and apply
# the mask
mask = cv2.inRange(image, lower, upper)
# cv2.bitwise_and : showing only pixels in the image that have a corresponding white (255) value in the mask.
output = cv2.bitwise_and(image, image, mask = mask)
# show the images
plt.figure(figsize=(14,10))
plt.subplot(131), plt.imshow(image, cmap = 'gray')
plt.title('Original Image')
plt.subplot(132), plt.imshow(mask, cmap = 'gray')
plt.title('Mask Image')
plt.subplot(133), plt.imshow(output, cmap = 'gray')
plt.title('Bitwise Image')