Created
January 26, 2018 21:08
-
-
Save skt7/b3d0445ea902c7907c6d005d1079b76a to your computer and use it in GitHub Desktop.
Plotting order of dominance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import cv2 | |
import numpy as np | |
import matplotlib.pyplot as plt | |
class DominantColors: | |
def plotHistogram(self): | |
#labels form 0 to no. of clusters | |
numLabels = np.arange(0, self.CLUSTERS+1) | |
#create frequency count tables | |
(hist, _) = np.histogram(self.LABELS, bins = numLabels) | |
hist = hist.astype("float") | |
hist /= hist.sum() | |
#appending frequencies to cluster centers | |
colors = self.COLORS | |
#descending order sorting as per frequency count | |
colors = colors[(-hist).argsort()] | |
hist = hist[(-hist).argsort()] | |
#creating empty chart | |
chart = np.zeros((50, 500, 3), np.uint8) | |
start = 0 | |
#creating color rectangles | |
for i in range(self.CLUSTERS): | |
end = start + hist[i] * 500 | |
#getting rgb values | |
r = colors[i][0] | |
g = colors[i][1] | |
b = colors[i][2] | |
#using cv2.rectangle to plot colors | |
cv2.rectangle(chart, (int(start), 0), (int(end), 50), (r,g,b), -1) | |
start = end | |
#display chart | |
plt.figure() | |
plt.axis("off") | |
plt.imshow(chart) | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment