I’m trying to extract the top 3 most-prominent colors in an image. I’m no expert on computer vision or image analysis, so I’m relying on the kindness of strangers and borrowed code. I’ve got the following Python implementation (abridged to remove database storage of results, etc., so it won’t be directly runnable)
from sklearn.cluster import KMeans import cv2 import numpy as np import colorsys (...) def centroid_histogram(clusters): numLabels = np.arange(0, len(np.unique(clusters.labels_)) + 1) (hist, _) = np.histogram(clusters.labels_, bins = numLabels) # normalize the histogram, such that it sums to one hist = hist.astype("float") hist /= hist.sum() # return the histogram return hist (...) # load the image and convert it to RGB image = cv2.imread(image) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) # break down the image into one long list of pixels image = image.reshape((image.shape * image.shape, 3)) # perform KMeans clustering clusters = KMeans(n_clusters=3) clusters.fit(image) histogram = centroid_histogram(clusters) for (percent, color) in zip(histogram, clusters.cluster_centers_): r = int(color) g = int(color) b = int(color) h, s, v = colorsys.rgb_to_hsv(r/255.0, g/255.0, b/255.0) h = h * 360.0
I’ve run the same image through this code, and through an image-processing website, and the results are different. I understand we don’t know the exact algorithm or parameters the site is using. I get that. But the site is getting the results I want, and my code is outputting a darker, more muddied palette. Mine is the bars on the right.
My code also generates the palette with a percentage, which is why the palette blocks are unequal. (Is that the issue?)
There you have it. I’ve tried to ask this in a respectful, informed way so it won’t get ignored or voted down into oblivion. It’s not easy to admit you don’t understand what you’re trying to accomplish, and I don’t have the knowledge of mathematics to fully comprehend what’s happening. I’d be grateful for some suggestions.