0

I would like to find the most present color in an image. (in Java/Android) I have a Mat object from openCv which refer to the image, I can get RGB value of each pixel easily, but I think that just average the red, blue and green separately isn't a viable algorithm.

I think it's better to search the most frequent {r,g,b} as a triple.

Do you agree ? And could you help me to implement this algorithm, I have no idea ! Thank you in advance

Manuel Miranda
  • 823
  • 5
  • 13
Slayer
  • 81
  • 7
  • This isn't going to give you the "most present color" as the eye and the brain would see it. Lets say, that {0,0,0} occurs for 1000 pixels, and that you have 990 times {255,0,0}, 990 times {254,1,0}, 990 times {255,1,0}, 990 times {255,0,1}, and so on for a few 990-sets more. Do you think that this will work? – laune Jun 16 '14 at 10:30
  • do you define color range as a color or is it all the 256*256*256 colors – Vikram Bhat Jun 16 '14 at 10:48

1 Answers1

0

Following is a pseudo code to do count of triple

    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(',')
Vikram Bhat
  • 6,106
  • 3
  • 20
  • 19