【问题标题】:Most dominant color in RGB image - OpenCV / NumPy / PythonRGB 图像中最主要的颜色 - OpenCV / NumPy / Python
【发布时间】: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

cltpprint 是这个,不确定是否有帮助

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


【解决方案1】:

可以建议使用np.uniquenp.bincount 来获得最主要颜色的两种方法。此外,在链接页面中,它谈到 bincount 是一种更快的替代方案,所以这可能是要走的路。

方法#1

def unique_count_app(a):
    colors, count = np.unique(a.reshape(-1,a.shape[-1]), axis=0, return_counts=True)
    return colors[count.argmax()]

方法 #2

def bincount_app(a):
    a2D = a.reshape(-1,a.shape[-1])
    col_range = (256, 256, 256) # generically : a2D.max(0)+1
    a1D = np.ravel_multi_index(a2D.T, col_range)
    return np.unravel_index(np.bincount(a1D).argmax(), col_range)

在密集范围[0,9) 中对1000 x 1000 彩色图像进行验证和计时,以获得可重复的结果 -

In [28]: np.random.seed(0)
    ...: a = np.random.randint(0,9,(1000,1000,3))
    ...: 
    ...: print unique_count_app(a)
    ...: print bincount_app(a)
[4 7 2]
(4, 7, 2)

In [29]: %timeit unique_count_app(a)
1 loop, best of 3: 820 ms per loop

In [30]: %timeit bincount_app(a)
100 loops, best of 3: 11.7 ms per loop

进一步提升

利用 multi-core with numexpr module 处理大数据进一步提升 -

import numexpr as ne

def bincount_numexpr_app(a):
    a2D = a.reshape(-1,a.shape[-1])
    col_range = (256, 256, 256) # generically : a2D.max(0)+1
    eval_params = {'a0':a2D[:,0],'a1':a2D[:,1],'a2':a2D[:,2],
                   's0':col_range[0],'s1':col_range[1]}
    a1D = ne.evaluate('a0*s0*s1+a1*s0+a2',eval_params)
    return np.unravel_index(np.bincount(a1D).argmax(), col_range)

时间安排 -

In [90]: np.random.seed(0)
    ...: a = np.random.randint(0,9,(1000,1000,3))

In [91]: %timeit unique_count_app(a)
    ...: %timeit bincount_app(a)
    ...: %timeit bincount_numexpr_app(a)
1 loop, best of 3: 843 ms per loop
100 loops, best of 3: 12 ms per loop
100 loops, best of 3: 8.94 ms per loop

【讨论】:

  • 太棒了,而且速度非常快。但是,当我执行color = utils.bincount_app(image).astype('uint8').tolist() 时,我无法从.bincount_app 获得颜色,它显示'tuple' object has no attribute 'astype'。与unique_count 相同的东西就像一个魅力,但似乎更慢。
  • @PrimuS 只需:list(bincount_numexpr_app(a)).
  • 嗯,对不起我觉得没用,但是color = list(utils.bincount_numexpr_app(image))cv2.rectangle(barcode, (0, 0), (width, height), color, -1)导致Scalar value for argument 'color' is not numeric
  • @PrimuS 我不确定那里的颜色参数的预期输入。也许它需要一个元组。所以,试试:color = utils.bincount_numexpr_app(image) 甚至color = tuple(utils.bincount_numexpr_app(image))?
  • @PrimuS barcode 是灰度图像还是彩色图像?
【解决方案2】:

@Divakar 给出了很好的答案。但如果你想将自己的代码移植到 OpenCV,那么:

    img = cv2.imread('image.jpg',cv2.IMREAD_UNCHANGED)

    data = np.reshape(img, (-1,3))
    print(data.shape)
    data = np.float32(data)

    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
    flags = cv2.KMEANS_RANDOM_CENTERS
    compactness,labels,centers = cv2.kmeans(data,1,None,criteria,10,flags)

    print('Dominant color is: bgr({})'.format(centers[0].astype(np.int32)))

图片的结果:

主色为:bgr([41 31 23])

花费时间:0.10798478126525879 秒

【讨论】:

    【解决方案3】:

    cv2.calcHist() 的等效代码是替换:

    (hist, _) = np.histogram(clt.labels_, bins=num_labels)  
    

    dmin, dmax, _, _ = cv2.minMaxLoc(clt.labels_)
    
    if np.issubdtype(data.dtype, 'float'): dmax += np.finfo(data.dtype).eps
    else: dmax += 1
    
    hist = cv2.calcHist([clt.labels_], [0], None, [num_labels], [dmin, dmax]).flatten()
    

    请注意,cv2.calcHist 仅接受 uint8float32 作为元素类型。

    更新

    opencv 和 numpy 的 binning 似乎彼此不同,因为如果 bin 的数量未映射值范围,则直方图会有所不同:

    import numpy as np
    from matplotlib import pyplot as plt
    import cv2
    
    #data = np.random.normal(128, 1, (100, 100)).astype('float32')
    data = np.random.randint(0, 256, (100, 100), 'uint8')
    BINS = 20
    
    np_hist, _ = np.histogram(data, bins=BINS)
    
    dmin, dmax, _, _ = cv2.minMaxLoc(data)
    if np.issubdtype(data.dtype, 'float'): dmax += np.finfo(data.dtype).eps
    else: dmax += 1
    
    cv_hist = cv2.calcHist([data], [0], None, [BINS], [dmin, dmax]).flatten()
    
    plt.plot(np_hist, '-', label='numpy')
    plt.plot(cv_hist, '-', label='opencv')
    plt.gcf().set_size_inches(15, 7)
    plt.legend()
    plt.show()
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-09-23
      • 1970-01-01
      • 2011-03-15
      • 1970-01-01
      • 2011-07-05
      • 1970-01-01
      • 2018-06-15
      • 2014-06-17
      相关资源
      最近更新 更多