【问题标题】:Color detection of object in Image图像中物体的颜色检测
【发布时间】:2016-08-29 14:23:42
【问题描述】:

我正在使用 opencv 和 python。

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

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

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

【问题讨论】:

    标签: python opencv image-processing colors rgb


    【解决方案1】:

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

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

    您可以使用this kind of preprocessing去除背景像素和皮肤像素

    【讨论】:

    • 很高兴检查一下,这就是我要找的东西
    • 如果您只需要一种颜色,您可以使用多种启发式方法根据位置、饱和度和像素数选择正确的颜色,但这取决于您输入的外观和确切要求
    • 我正在删除背景图片,然后尝试找出颜色,因为大多数时候它给了我白色作为首选
    • 我已将代码修改为仅使用图像中心并按大小对簇进行排序
    • 哦,谢谢我正在尝试使用背景减法
    【解决方案2】:
    1. 加载框架
    2. 将 BGR 转换为 HSV
    3. 像素值范围

    还可以查看此链接Color detection in opencv

    【讨论】:

    • 您所说的以上 sol 是当您想要检测给定颜色示例时检测图像中的蓝色。但我想知道图像的颜色是什么
    猜你喜欢
    • 1970-01-01
    • 2018-07-16
    • 2016-08-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-26
    相关资源
    最近更新 更多