title: Find dominant color in a camera frame in OpenCV Android
body: <p>I want to get the dominant color in an Android CvCameraViewFrame object. I use the following OpenCV Android code to do that. This code is converted from OpenCV c++ code to OpenCV Android code. In the following code I loop through all the pixels in my camera frame and find the color of each pixel and store them in a HashMap to find the dominant color at the end of the loop. To loop through each pixel it takes about 30 seconds. This is unacceptable for me. Could somebody please review this code and point me how can I find the dominant color in a camera frame. </p>
  
  <pre><code>private String[] colors = {"cBLACK", "cWHITE", "cGREY", "cRED", "cORANGE", "cYELLOW", "cGREEN", "cAQUA", "cBLUE", "cPURPLE", "cPINK", "cRED"};
  
  public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
          mRgba = inputFrame.rgba();
  
          if (mIsColorSelected) {
              Imgproc.cvtColor(mRgba, mRgba, Imgproc.COLOR_BGR2HSV);
  
              int h = mRgba.height();             // Pixel height
              int w = mRgba.width();              // Pixel width
              int rowSize = (int)mRgba.step1();       // Size of row in bytes, including extra padding
  
              float initialConfidence = 1.0f;
  
              Map&lt;String, Integer&gt; tallyColors = new HashMap&lt;String, Integer&gt;();
  
              byte[] pixelsTotal = new byte[h*rowSize];
              mRgba.get(0,0,pixelsTotal);
  
              //This for loop takes about 30 seconds to process for my camera frame
              for (int y=0; y&lt;h; y++) {
                  for (int x=0; x&lt;w; x++) {
                      // Get the HSV pixel components
  
                      int hVal = (int)pixelsTotal[(y*rowSize) + x + 0];   // Hue
                      int sVal = (int)pixelsTotal[(y*rowSize) + x + 1];   // Saturation
                      int vVal = (int)pixelsTotal[(y*rowSize) + x + 2];   // Value (Brightness)
  
  
                      // Determine what type of color the HSV pixel is.
                      String ctype = getPixelColorType(hVal, sVal, vVal);
                      // Keep count of these colors.
                      int totalNum = 0;
                      try{
                          totalNum = tallyColors.get(ctype);
                      } catch(Exception ex){
                          totalNum = 0;
                      }
                      totalNum++;
                      tallyColors.put(ctype, totalNum);
                  }
              }
  
              int tallyMaxIndex = 0;
              int tallyMaxCount = -1;
              int pixels = w * h;
              for (int i=0; i&lt;colors.length; i++) {
                  String v = colors[i];
                  int pixCount;
                  try{
                      pixCount = tallyColors.get(v);
                  } catch(Exception e){
                      pixCount = 0;
                  }
                  Log.i(TAG, v + " - " + (pixCount*100/pixels) + "%, ");
                  if (pixCount &gt; tallyMaxCount) {
                      tallyMaxCount = pixCount;
                      tallyMaxIndex = i;
                  }
              }
              float percentage = initialConfidence * (tallyMaxCount * 100 / pixels);
              Log.i(TAG, "Color of currency note: " + colors[tallyMaxIndex] + " (" + percentage + "% confidence).");
  
          }
  
          return mRgba;
      }
  
      private String getPixelColorType(int H, int S, int V)
      {
          String color;
          if (V &lt; 75)
              color = "cBLACK";
          else if (V &gt; 190 &amp;&amp; S &lt; 27)
              color = "cWHITE";
          else if (S &lt; 53 &amp;&amp; V &lt; 185)
              color = "cGREY";
          else {  // Is a color
              if (H &lt; 14)
                  color = "cRED";
              else if (H &lt; 25)
                  color = "cORANGE";
              else if (H &lt; 34)
                  color = "cYELLOW";
              else if (H &lt; 73)
                  color = "cGREEN";
              else if (H &lt; 102)
                  color = "cAQUA";
              else if (H &lt; 127)
                  color = "cBLUE";
              else if (H &lt; 149)
                  color = "cPURPLE";
              else if (H &lt; 175)
                  color = "cPINK";
              else    // full circle 
                  color = "cRED"; // back to Red
          }
          return color;
      }
  </code></pre>
  
  <p>Thank you very much.</p>