1

I want to detect which value is maximum from RGB. how can I detect that? I want to display which colour has highest occurrence with their RGB value. For example, In a image the RED colour has highest occurrence so it will display colour as RED and with their value in percentage.

I have tried it by getting rows and cols of image like below:

   public Mat onCameraFrame(CvCameraViewFrame cvf) {
        mRgba = cvf.rgba();

        int rows = mRgba.rows();
        int cols = mRgba.cols();
       // int ch = mRgba.channels();
        double R=0,G=0,B=0;

        for (int i=0; i<rows; i++)
        {
            for (int j=0; j<cols; j++)
            {
                double[] data = mRgba.get(i, j); //Stores element in an array
                R = data[0];
                G= data[1];
                B = data[2];
            }
        }

        Imgproc.putText(mRgba,"R:"+R + "G:"+G +"B:"+B,new Point(10,52),Core.FONT_HERSHEY_COMPLEX,.7,new Scalar(5,255,255), 2,8,false );
return mRgba;
}

but it is taking to much time and screen is lagging because I have implemented code in onCameraFrame. So how can I detect it fast in this method and which is the best way to it?

Thanks.

Ahmad F
  • 30,560
  • 17
  • 97
  • 143
Vishal Thakkar
  • 652
  • 4
  • 18

2 Answers2

0

For such tasks you should use AsyncTask as it does not run on MainThread thus your UI wont freeze.

https://developer.android.com/reference/android/os/AsyncTask.html

I assume that this will be faster compared to your current solution.

glethien
  • 2,440
  • 2
  • 26
  • 36
0

Split the image into its R, G and B channels, and compute the sum of each Mat.

vector<Mat> channels;
split(mRgba, channels);
B = sum(channels[0])[0];
G = sum(channels[1])[0];
R = sum(channels[2])[0];

Compare the values of R, G and B to know which colour has occurred the most in the frame.

Blue
  • 653
  • 1
  • 11
  • 26