title: Most dominant color in RGB image - OpenCV / NumPy / Python
body: <p>I have a python image processing function, that uses tries to get the dominant color of an image. I make use of a function I found here <a href="https://github.com/tarikd/python-kmeans-dominant-colors/blob/master/utils.py" rel="noreferrer">https://github.com/tarikd/python-kmeans-dominant-colors/blob/master/utils.py</a></p>
  
  <p>It works, but unfortunately I don't quite understand what it does and I learned that <code>np.histogram</code> is rather slow and I should use <code>cv2.calcHist</code> since it's 40x faster according to this: <a href="https://docs.opencv.org/trunk/d1/db7/tutorial_py_histogram_begins.html" rel="noreferrer">https://docs.opencv.org/trunk/d1/db7/tutorial_py_histogram_begins.html</a></p>
  
  <p>I'd like to understand how I have to update the code to use <code>cv2.calcHist</code>, or better, which values I have to input.</p>
  
  <p>My function</p>
  
  <pre><code>def centroid_histogram(clt):
      # grab the number of different clusters and create a histogram
      # based on the number of pixels assigned to each cluster
      num_labels = np.arange(0, len(np.unique(clt.labels_)) + 1)
      (hist, _) = np.histogram(clt.labels_, bins=num_labels)
  
      # normalize the histogram, such that it sums to one
      hist = hist.astype("float")
      hist /= hist.sum()
  
      # return the histogram
      return hist
  </code></pre>
  
  <p>The <code>pprint</code> of <code>clt</code> is this, not sure if this helps</p>
  
  <pre><code>KMeans(algorithm='auto', copy_x=True, init='k-means++', max_iter=300,
      n_clusters=1, n_init=10, n_jobs=1, precompute_distances='auto',
      random_state=None, tol=0.0001, verbose=0)
  </code></pre>
  
  <p>My code can be found here: <a href="https://github.com/primus852/python-movie-barcode" rel="noreferrer">https://github.com/primus852/python-movie-barcode</a></p>
  
  <p>I am a very beginner, so any help is highly appreciated.</p>
  
  <p>As per request:</p>
  
  <h3>Sample Image</h3>
  
  <p><a href="https://i.stack.imgur.com/61LGC.jpg" rel="noreferrer"><img src="https://i.stack.imgur.com/61LGC.jpg" alt="Sample"></a></p>
  
  <h3>Most dominant color:</h3>
  
  <p><code>rgb(22,28,37)</code></p>
  
  <h3>Computation time for the Histogram:</h3>
  
  <p><code>0.021515369415283203s</code></p>