How to get the right Cr and Cb (YCrCb) values from RGB real time image from android camera using OpenCV?

asked 2015-06-04 01:25:47 -0500

ichaka gravatar image

I am doing my final project about image processing in android. In my project I want to pick YCrCb color through the android camera (rgb image) using OpenCV, and get the value of Cr low, Cr high, Cb low also Cb high. It can happen when i pressed a button, so i do my code inside it. Firstly, I convert rgb image to YCrCb color, then find Cr and Cb value by array. It works out. But, there is a wrong output. When i used "Imgproc.COLOR_RGB2YCrCb" Cr high value always be 254 (maximum), even if i did in another color sample to be picked. Then, when i used "Imgproc.COLOR_BGR2YCrCb", Cb high value always 254. But actually, it should give another value. So, what should i do to fix this problem? I just want it could pick color and give the right value as same as the sample color that is picked. Thanks, and this is my code.

public boolean getSkin(View view) {
getSkin = true;
Imgproc.cvtColor(rect_ROI, rect_ROI, Imgproc.COLOR_BGR2YCrCb);

Cr_low = 255;
Cr_high = 0;
Cb_low = 255;
Cb_high = 0;

for (int x = 0; x < rect_ROI.rows(); x++) 
{
    for (int y = 0; y < rect_ROI.cols(); y++)
    {
        double[] array_value = rect_ROI.get(x, y);

        if (Cr_low > array_value[1]) Cr_low = (int) array_value[1] ;
        if (Cr_high < array_value[1]) Cr_high = (int) array_value[1];
        if (Cb_low > array_value[2]) Cb_low = (int) array_value[2];
        if (Cb_high < array_value[2]) Cb_high = (int) array_value[2];
    }       
}

if (Cr_high > Cr_low + 10) Cr_high = Cr_high - 1;
if (Cb_high > Cb_low + 10) Cb_high = Cb_high - 1;
Cr_low += 1;
Cb_low += 1; 

TextView Crl = (TextView) findViewById(R.id.cr_low);
TextView Crh = (TextView) findViewById(R.id.cr_high);
TextView Cbl = (TextView) findViewById(R.id.cb_low);
TextView Cbh = (TextView) findViewById(R.id.cb_high);

Crl.setText(""+Cr_low);
Crh.setText(""+Cr_high);
Cbl.setText(""+Cb_low);
Cbh.setText(""+Cb_high);

return getSkin;
 }
edit retag flag offensive close merge delete

Comments

hmm, your loops have x and y in reverse.

also, maybe using meanStdDev on your roi might be less error prone and more adequate

berak gravatar imageberak ( 2015-06-04 01:39:21 -0500 )edit