Image中对象的颜色检测 [英] Color detection of object in Image

查看:187
本文介绍了Image中对象的颜色检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用opencv和python。



我需要检测图像中对象的颜色,例如下图所示,衬衫颜色为红色。







在这个链接上,我找到了一些有用的东西,但它的皮肤检测图像。



您可以使用这种预处理


I am using opencv and python .

I need to detect color of object in image for example given below image , the color of shirt is red.

On this link i found something useful but its detecting image of skin . http://lokeshdhakar.com/projects/color-thief/

I think i will have to use image contour extraction and then carry out color detection for that .

解决方案

Getting the dominant colors may be achieved using the following simple approach:

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

EDIT: take only the center of the image:

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 )

EDIT: count cluster pixels, order clusters by cluster size

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

You can remove BG and skin pixels with this kind of preprocessing

这篇关于Image中对象的颜色检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆