【问题标题】:Predominant color in an image [duplicate]图像中的主要颜色 [重复]
【发布时间】:2014-08-06 03:29:07
【问题描述】:

我想在图像中找到最常见的颜色。 (在 Java/Android 中) 我有一个来自 openCv 的 Mat 对象,它引用图像,我可以获得每个像素的 RGB 值 很容易,但我认为单独平均红色、蓝色和绿色并不是一个可行的算法。

我认为最好将最频繁的 {r,g,b} 搜索为三元组。

你同意吗? 你能帮我实现这个算法吗,我不知道!提前谢谢你

【问题讨论】:

  • 这不会给你眼睛和大脑看到的“最常见的颜色”。假设,{0,0,0} 出现在 1000 个像素上,并且您有 990 次 {255,0,0}、990 次 {254,1,0}、990 次 {255,1,0}, 990 次 {255,0,1},以此类推,再增加几个 990 组。你认为这会奏效吗?
  • 你是把颜色范围定义为一种颜色还是256*256*256的全部颜色

标签: java algorithm opencv colors


【解决方案1】:

以下是计算三倍数的伪代码

    Hashmap<string,int> colors 

    int max;
    string max_color;

    for(int i=0;i<height;i++) {

      for(int j=0;j<width;j++) {

         int count = 0;
         string red = pixel[i][j].red.to_string;
         string green = pixel[i][j].green.to_string;
         string blue = pixel[i][j].blue.to_string;
         string key = red + "," + green + "," + blue;
         if(hashmap contains pixel[i][j]) {

             count = hashmap.get(key)
             count++;
             hashmap.write(key,count);
         }

         else {
           count = 1;
           hashmap.insert(key,count);  
         }

         if(count > max) {
            max = count;
            max_color = key;
         }

      }



    }

  res = max_color.split(',')

【讨论】:

  • 谢谢kkkkkk youuuu :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-02-19
  • 2013-02-11
  • 2018-11-26
  • 2018-10-24
  • 2010-12-19
  • 1970-01-01
  • 2011-03-15
相关资源
最近更新 更多