Title: Using Mean Color As A Feature
Slug: using_mean_color_as_a_feature
Summary: How to use the mean color of an image as a feature using OpenCV in Python with the Shi-Tomasi Corner Detector.
Date: 2017-09-11 12:00
Category: Machine Learning
Tags: Preprocessing Images
Authors: Chris Albon

Preliminaries


In [1]:
# Load image
import cv2
import numpy as np
from matplotlib import pyplot as plt

Load image


In [2]:
# Load image as BGR
image_bgr = cv2.imread('images/plane_256x256.jpg', cv2.IMREAD_COLOR)

Calculate Mean Color Of Each Color Channel


In [3]:
# Calculate the mean of each channel
channels = cv2.mean(image_bgr)

# Swap blue and red values (making it RGB, not BGR)
observation = np.array([(channels[2], channels[1], channels[0])])

Show Values


In [4]:
# Show mean channel values
observation


Out[4]:
array([[  90.53204346,  133.11735535,  169.03074646]])

View Mean Image Colors


In [5]:
# Show image
plt.imshow(observation), plt.axis("off")
plt.show()