【问题标题】:Find dominant color on an image在图像上查找主色
【发布时间】:2015-05-01 20:55:31
【问题描述】:

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

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

float binVal = hist.at<float>(h, s);

编辑:

我已经尝试了下面的代码。我画了 h-s 直方图。我的结果图像在这里。在二进制阈值之后我没有找到任何东西。也许我发现最大直方图值不正确。

cvtColor(src, hsv, CV_BGR2HSV);

// Quantize the hue to 30 levels
// and the saturation to 32 levels
int hbins = 20, sbins = 22;
int histSize[] = {hbins, sbins};
// hue varies from 0 to 179, see cvtColor
float hranges[] = { 0, 180 };
// saturation varies from 0 (black-gray-white) to
// 255 (pure spectrum color)
float sranges[] = { 0, 256 };
const float* ranges[] = { hranges, sranges };
MatND hist;
// we compute the histogram from the 0-th and 1-st channels
int channels[] = {0, 1};

calcHist( &hsv, 1, channels, Mat(), // do not use mask
         hist, 2, histSize, ranges,
         true, // the histogram is uniform
         false );
double maxVal=0;
minMaxLoc(hist, 0, &maxVal, 0, 0);

int scale = 10;
Mat histImg = Mat::zeros(sbins*scale, hbins*10, CV_8UC3);
int maxIntensity = -100;
for( int h = 0; h < hbins; h++ ) {
    for( int s = 0; s < sbins; s++ )
    {
        float binVal = hist.at<float>(h, s);
        int intensity = cvRound(binVal*255/maxVal);
        rectangle( histImg, Point(h*scale, s*scale),
                    Point( (h+1)*scale - 1, (s+1)*scale - 1),
                    Scalar::all(intensity),
                    CV_FILLED );
        if(intensity > maxIntensity)
            maxIntensity = intensity;
    }
}
std::cout << "max Intensity " << maxVal << std::endl;
Mat dst;
cv::threshold(src, dst, maxIntensity, 255, cv::THRESH_BINARY);

namedWindow( "Dest", 1 );
imshow( "Dest", dst );
namedWindow( "Source", 1 );
imshow( "Source", src );

namedWindow( "H-S Histogram", 1 );
imshow( "H-S Histogram", histImg );

【问题讨论】:

    标签: c++ opencv image-processing colors histogram


    【解决方案1】:

    或者,您可以尝试k-means 方法。 Calculate k clustersk ~ 2..5 并以最大组的质心作为主色。

    OpenCv 的 python 文档有一个 illustrated example 可以很好地获得主色:

    【讨论】:

    • 我应该很快找到最大的颜色区域,而且我的图像很大。这种方法对这项任务有效吗?
    • 不,它很可能比简单的直方图慢(确切的版本甚至是 NP 完全的)。但由于此任务执行从N 像素到仅一种颜色的强降维,您很可能只是不考虑所有像素,即首先对图像进行子采样(独立于您用于实际确定之后上色)。
    • 你到底不能使用什么?
    • 对不起我的英语,这不是解决我问题的好方法。所以,我不打算使用 knn 聚类方法。
    【解决方案2】:

    解决办法

    • 查找 H-S 直方图
    • 找到峰值 H 值(使用 minmaxLoc 函数)
    • 分割图像 3 通道(h,s,v)
    • 应用于阈值。
    • 通过合并3通道创建图像

    【讨论】:

      【解决方案3】:

      这是一种 Python 方法,使用 K-Means Clustering 来确定带有 sklearn.cluster.KMeans() 的图像中的主色


      输入图片

      结果

      对于n_clusters=5,这里是最主要的颜色和百分比分布

      [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%
      

      每个颜色簇的可视化

      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%
      

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

      【讨论】:

      • 这家伙真是太棒了!谢谢!
      【解决方案4】:

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

      • RGB 中的所有 3 个通道都对颜色有贡献,因此您必须 以某种方式找出三个不同的直方图都处于最大值的位置。 (或者它们的总和是最大值,或者其他什么。)
      • HSV 在一个通道中包含所有颜色(嗯,色调)信息,所以 您只需考虑一个直方图。
      • 灰度会丢弃所有颜色信息,因此对于 寻找颜色。

      尝试转换成HSV,然后计算H通道的直方图。

      正如您所说,您想在直方图中找到最大值。但是:

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

      【讨论】:

        猜你喜欢
        • 2012-01-18
        • 2015-03-23
        • 2014-02-19
        • 1970-01-01
        • 2011-03-15
        • 2017-12-19
        • 2014-04-19
        • 2015-11-16
        • 2016-08-25
        相关资源
        最近更新 更多