2016-05-04 79 views
2

我正在使用opencv和python。图像中物体的颜色检测

我需要检测图像中物体的颜色,例如下图所示,衬衫的颜色是红色的。

enter image description here

enter image description here

在这个环节,我发现一些有用的东西,但皮肤及其检测图像。我想我将不得不使用图像轮廓提取,然后进行颜色检测。

回答

4

获得主色,可以实现使用以下简单的方法:

from sklearn.cluster import KMeans 
import numpy as np 
import matplotlib.pyplot as plt 
import matplotlib.patches as patches 

img = cv2.imread('red_shirt.jpg') 
height, width, dim = img.shape 

编辑:仅取的图像的中心:

img = img[(height/4):(3*height/4), (width/4):(3*width/4), :] 
height, width, dim = img.shape 

img_vec = np.reshape(img, [height * width, dim]) 

kmeans = KMeans(n_clusters=3) 
kmeans.fit(img_vec) 

编辑:计数簇像素,顺序簇由簇大小

unique_l, counts_l = np.unique(kmeans.labels_, return_counts=True) 
sort_ix = np.argsort(counts_l) 
sort_ix = sort_ix[::-1] 

fig = plt.figure() 
ax = fig.add_subplot(111) 
x_from = 0.05 

for cluster_center in kmeans.cluster_centers_[sort_ix]: 
    ax.add_patch(patches.Rectangle((x_from, 0.05), 0.29, 0.9, alpha=None, 
            facecolor='#%02x%02x%02x' % (cluster_center[2], cluster_center[1], cluster_center[0]))) 
    x_from = x_from + 0.31 

plt.show() 

enter image description here

可以移除BG和皮肤像素与this kind of preprocessing

+0

好生病检查出来,这是我寻找的东西 – usernan

+0

如果你只需要一种颜色,你可以使用几种启发式方法来根据位置,饱和度和像素来选择合适的颜色计数,但这取决于您的输入的外观和确切的要求 –

+0

我正在删除背景图像,然后试图找出颜色,贝兹大部分时间它给我白色作为第一选择 – usernan

1
  1. 装入帧
  2. 转换BGR到HSV
  3. 范围像素的值

还检查了这个链接Color detection in opencv

+0

以上sol你说的是当你想要检测给定的颜色例子检测图像中的蓝色。但我想知道什么是图像的颜色 – usernan