Skip to content

Instantly share code, notes, and snippets.

@youngsoul
Last active March 24, 2019 19:33
Show Gist options
  • Save youngsoul/536f7e403b700b43b4ff8ac121237953 to your computer and use it in GitHub Desktop.
Save youngsoul/536f7e403b700b43b4ff8ac121237953 to your computer and use it in GitHub Desktop.
import cv2
import numpy as np
"""
This histogram will be used to charac- terize the color of the flower petals,
which is a good starting point for classifying the species of a flower
"""
class RGBHistogram:
def __init__(self, bins):
self.bins = bins
def get_features(self, imagePath):
img = cv2.imread(imagePath)
features = []
if img is not None:
features.extend(self.extract_color_stats(img))
features.extend(self.describe(img).tolist())
return features
def extract_color_stats(self, image):
# split the input image into its respective RGB color channels
# and then create a feature vector with 6 values: the mean and
# standard deviation for each of the 3 channels, respectively
(B,G, R) = cv2.split(image)
stats = [np.mean(R), np.mean(G), np.mean(B), np.std(R),
np.std(G), np.std(B)]
# return our set of features
return stats
def describe(self, image, mask=None):
hist = cv2.calcHist([image], [0, 1, 2],
mask, self.bins, [0, 256, 0, 256, 0, 256])
cv2.normalize(hist, hist)
# return as a feature vector
return hist.flatten()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment