use*_*nan 2 python rgb opencv colors image-processing
我正在使用 opencv 和 python 。
我需要检测图像中物体的颜色,例如下图所示,衬衫的颜色是红色。
在此链接上,我发现了一些有用的东西,但它检测皮肤图像。 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
Run Code Online (Sandbox Code Playgroud)
编辑: 仅取图像的中心:
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 )
Run Code Online (Sandbox Code Playgroud)
编辑:计算簇像素,按簇大小对簇进行排序
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()
Run Code Online (Sandbox Code Playgroud)
您可以通过这种预处理去除 BG 和皮肤像素
| 归档时间: |
|
| 查看次数: |
8532 次 |
| 最近记录: |