RGB图像中最主要的颜色- OpenCV / NumPy / Python

11dmarpk  于 12个月前  发布在  Python
关注(0)|答案(4)|浏览(137)

我有一个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

nom7f22z

nom7f22z1#

可以建议使用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-corenumexpr模块进一步提升大数据处理能力-

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

ulmd4ohb

ulmd4ohb2#

@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秒

qlzsbp2j

qlzsbp2j3#

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的分箱方式是不同的,因为如果分箱的数量没有Map到值的范围,直方图就会不同:

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()

apeeds0o

apeeds0o4#

@Divakar的答案的改进版本。RGBA图像的单独解决方案。RGBA图像的完全透明像素不会被计算在内。

import numpy as np

def most_common_color_RGB(image: np.ndarray):
    """input image ndarray shape should be RGB shape, for example: (512, 512, 3)"""
    a2D = image.reshape(-1, image.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)

def most_common_color_RGBA(image_RGBA: np.ndarray):
    """input image ndarray shape should be RGBA shape, for example: (512, 512, 4)"""
    RGB_pixels = image_RGBA.reshape(-1, 4)
    # remove transparent pixels
    just_non_alpha = RGB_pixels[RGB_pixels[:, 3] != 0]
    if just_non_alpha.shape[0] == 0:
        return False
    # delete alpha channel
    just_non_alpha = np.delete(just_non_alpha, 3, axis=1)
    col_range = (256, 256, 256)  # generically : a2D.max(0)+1
    a1D = np.ravel_multi_index(just_non_alpha.T, col_range)
    return np.unravel_index(np.bincount(a1D).argmax(), col_range)

字符串

相关问题