gpt4 book ai didi

python - 图像中物体的颜色检测

转载 作者:太空宇宙 更新时间:2023-11-03 21:19:14 25 4
gpt4 key购买 nike

我正在使用 opencv 和 python 。

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

enter image description here

enter image description here

在这个链接上,我发现了一些有用的东西,但它检测皮肤图像。 http://lokeshdhakar.com/projects/color-thief/

我想我将不得不使用图像轮廓提取,然后为此进行颜色检测。

最佳答案

可以使用以下简单方法获得主色:

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

您可以使用 this kind of preprocessing 移除 BG 和皮肤像素

关于python - 图像中物体的颜色检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37022787/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com