Skip to content

Instantly share code, notes, and snippets.

@skt7
Created January 26, 2018 21:08
Show Gist options
  • Save skt7/b3d0445ea902c7907c6d005d1079b76a to your computer and use it in GitHub Desktop.
Save skt7/b3d0445ea902c7907c6d005d1079b76a to your computer and use it in GitHub Desktop.
Plotting order of dominance
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