在图像上找到主色

zak*_*jma 10 opencv colors image-processing histogram image-segmentation

我想在图像上找到主色.为此,我知道我应该使用图像直方图.但我不确定图像格式.应该使用rgb,hsv或灰色图像中的哪一个?

计算直方图后,我应该在直方图上找到最大值.为此,我应该找到hsv图像的最大binVal值以下吗?为什么我的结果图像只包含黑色?

__CODE__

编辑:

我试过下面的代码.我画直方图.我的结果图片就在这里.二进制阈值后我没有找到任何东西.也许我发现最大直方图值不正确.

在此输入图像描述 在此输入图像描述

float binVal = hist.at<float>(h, s);
Run Code Online (Sandbox Code Playgroud)

nat*_*ncy 9

Here's a Python approach using K-Means Clustering to determine the dominant colors in an image with sklearn.cluster.KMeans()


Input image

Results

With n_clusters=5, here are the most dominant colors and percentage distribution

[14.69488554 34.23074345 41.48107857] 13.67%
[141.44980073 207.52576948 236.30722987] 15.69%
[ 31.75790423  77.52713644 114.33328324] 18.77%
[ 48.41205713 118.34814452 176.43411287] 25.19%
[ 84.04820266 161.6848298  217.14045211] 26.69%
Run Code Online (Sandbox Code Playgroud)

Visualization of each color cluster

在此处输入图片说明

Similarity with n_clusters=10,

[ 55.09073171 113.28271003  74.97528455] 3.25%
[ 85.36889668 145.80759374 174.59846237] 5.24%
[164.17201088 223.34258123 241.81929254] 6.60%
[ 9.97315932 22.79468111 22.01822211] 7.16%
[19.96940211 47.8375841  72.83728002] 9.27%
[ 26.73510467  70.5847759  124.79314278] 10.52%
[118.44741779 190.98204701 230.66728334] 13.55%
[ 51.61750364 130.59930047 198.76335878] 13.82%
[ 41.10232129 104.89923271 160.54431333] 14.53%
[ 81.70930412 161.823664   221.10258949] 16.04%
Run Code Online (Sandbox Code Playgroud)

在此处输入图片说明

import cv2, numpy as np
from sklearn.cluster import KMeans

def visualize_colors(cluster, centroids):
    # Get the number of different clusters, create histogram, and normalize
    labels = np.arange(0, len(np.unique(cluster.labels_)) + 1)
    (hist, _) = np.histogram(cluster.labels_, bins = labels)
    hist = hist.astype("float")
    hist /= hist.sum()

    # Create frequency rect and iterate through each cluster's color and percentage
    rect = np.zeros((50, 300, 3), dtype=np.uint8)
    colors = sorted([(percent, color) for (percent, color) in zip(hist, centroids)])
    start = 0
    for (percent, color) in colors:
        print(color, "{:0.2f}%".format(percent * 100))
        end = start + (percent * 300)
        cv2.rectangle(rect, (int(start), 0), (int(end), 50), \
                      color.astype("uint8").tolist(), -1)
        start = end
    return rect

# Load image and convert to a list of pixels
image = cv2.imread('1.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
reshape = image.reshape((image.shape[0] * image.shape[1], 3))

# Find and display most dominant colors
cluster = KMeans(n_clusters=5).fit(reshape)
visualize = visualize_colors(cluster, cluster.cluster_centers_)
visualize = cv2.cvtColor(visualize, cv2.COLOR_RGB2BGR)
cv2.imshow('visualize', visualize)
cv2.waitKey()
Run Code Online (Sandbox Code Playgroud)

  • 这真是太棒了老兄!谢谢! (2认同)

mbs*_*kel 8

或者你可以试试k-means方法.计算k,k ~ 2..5并将最大组的质心作为主色.

OpenCv的python文档有一个插图示例,可以很好地获得主导颜色:


zak*_*jma 8

解决方案

  • 找到HS直方图
  • 找到峰值H值(使用minmaxLoc函数)
  • 分割图像3通道(h,s,v)
  • 适用于门槛.
  • 通过合并3通道创建图像

  • 你介意发布一些代码吗?谢谢 (9认同)

bea*_*ker 5

以下是一些可以帮助您入门的建议.

  • RGB中的所有3个通道都对颜色有贡献,因此您必须以某种方式确定三个不同直方图的最大值.(或者他们的总和是最大的,或者其他什么.)
  • HSV在一个通道中具有所有颜色(井,色调)信息,因此您只需要考虑一个直方图.
  • 灰度抛弃所有颜色信息,因此对于查找颜色几乎没用.

尝试转换为HSV,然后计算H通道上的直方图.

如你所说,你想在直方图中找到最大值.但:

  • 您可能想要考虑一系列值而不仅仅是一个,20-40而不是仅仅考虑30.尝试不同的范围大小.
  • 请记住,色调是圆形的,所以H=0H=360是相同的.
  • 尝试在此后绘制直方图:
    http://docs.opencv.org/doc/tutorials/imgproc/histograms/histogram_calculation/histogram_calculation.html
    ,看看你的结果是否有意义.
  • 如果您正在使用一系列色调并且找到最大范围,则可以使用该范围的中间作为主色,或者您可以找到该范围内的颜色的平均值并使用它.

    尝试一下.玩.