【发布时间】:2018-11-26 17:20:37
【问题描述】:
我有一个 python 图像处理函数,它使用尝试获取图像的主色。我使用了我在这里找到的函数https://github.com/tarikd/python-kmeans-dominant-colors/blob/master/utils.py
它可以工作,但不幸的是我不太明白它的作用,我了解到np.histogram 相当慢,我应该使用cv2.calcHist,因为它比这个快 40 倍:https://docs.opencv.org/trunk/d1/db7/tutorial_py_histogram_begins.html
我想了解如何更新代码以使用cv2.calcHist,或者更好,我必须输入哪些值。
我的功能
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
clt 的pprint 是这个,不确定是否有帮助
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)
我的代码可以在这里找到:https://github.com/primus852/python-movie-barcode
我是一个非常初学者,所以非常感谢任何帮助。
根据要求:
示例图片
最主要的颜色:
rgb(22,28,37)
直方图的计算时间:
0.021515369415283203s
【问题讨论】:
-
添加一个示例图像、计算的主色以及计算它所需的时间。
标签: python python-3.x numpy opencv