Title: Isolate Colors
Slug: isolate_colors
Summary: How to isolate colors in images using OpenCV in Python.
Date: 2017-09-11 12:00
Category: Machine Learning
Tags: Preprocessing Images
Authors: Chris Albon
In [1]:
# Load image
import cv2
import numpy as np
from matplotlib import pyplot as plt
In [2]:
# Load image
image_bgr = cv2.imread('images/plane_256x256.jpg')
In [3]:
# Convert BGR to HSV
image_hsv = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2HSV)
In [4]:
# Define range of blue values in HSV
lower_blue = np.array([50,100,50])
upper_blue = np.array([130,255,255])
# Create mask
mask = cv2.inRange(image_hsv, lower_blue, upper_blue)
In [5]:
# Mask image
image_bgr_masked = cv2.bitwise_and(image_bgr, image_bgr, mask=mask)
In [6]:
# Convert BGR to RGB
image_rgb = cv2.cvtColor(image_bgr_masked, cv2.COLOR_BGR2RGB)
In [7]:
# Show image
plt.imshow(image_rgb), plt.axis("off")
plt.show()