Implementation of rgb2xyz in opencv using c++

asked 2015-09-04 14:10:19 -0600

Tess gravatar image

updated 2015-09-04 15:03:22 -0600

LBerger gravatar image

I am trying to implement matlab rgb2xyz function in opencv using c++. I used cvtColor function for implementation. But my output image is not similar to Matlab output image.Should I use any other function or operations on image ?Here is my input image ,matlab output image and my output image.input imageMatlab output imageMy output image Hai... I got 1 more code for the implementation of rgb2xyz.But it gave me black output image. Here is the code.

 for(int i=0;i<rn;i++)
     {
       for(int j=0;j<cn;j++)
        {
         Vec3b intensity = image.at<Vec3b>(i,j);
         R= intensity.val[2];
         G= intensity.val[1];
         B= intensity.val[0];
         r = R/255.f; //R 0..1
         g = G/255.f; //G 0..1
         b = B/255.f; //B 0..1
         if (r <= 0.04045)
             r = r/12;
         else
             r = (float)pow((r+0.055)/1.055,2.4);
         if (g <= 0.04045)
             g = g/12;
         else
             g = (float)pow((g+0.055)/1.055,2.4);
         if (b <= 0.04045)
             b = b/12;
         else
             b = (float)pow((b+0.055)/1.055,2.4);
         X =  0.436052025f*r     + 0.385081593f*g + 0.143087414f *b;
         Y =  0.222491598f*r     + 0.71688606f *g + 0.060621486f *b;
         Z =  0.013929122f*r     + 0.097097002f*g + 0.71418547f  *b;
         Result.at<Vec3b>(i,j)[0]=(float)X;
         Result.at<Vec3b>(i,j)[1]=(float)Y; 
         Result.at<Vec3b>(i,j)[2]=(float)Z; 
      }
  }
edit retag flag offensive close merge delete

Comments

As stated in the docs, OpenCV BGR2XYZ conversion is based on D65 white point. Are you using such white point in Matlab too? Afaik, D65 is not the default illuminant in Matlab

LorenaGdL gravatar imageLorenaGdL ( 2015-09-04 14:30:10 -0600 )edit

@LorenaGdL: Sorry, I just used the matlab rgb2xyz function for generating output image.

Tess gravatar imageTess ( 2015-09-04 14:49:06 -0600 )edit

Yes, I know, but my question is if you're using such function with specifying D65 as the whitepoint. If not, OpenCV results using cvtColor won't match Matlab ones

LorenaGdL gravatar imageLorenaGdL ( 2015-09-04 15:01:16 -0600 )edit

To solve black output: Result.at<Vec3b>(i, j)[0] = saturate_cast<uchar>(X * 255); Result.at<Vec3b>(i, j)[1] = saturate_cast<uchar>(Y * 255); Result.at<Vec3b>(i, j)[2] = saturate_cast<uchar>(Z * 255);

LorenaGdL gravatar imageLorenaGdL ( 2015-09-04 15:13:01 -0600 )edit

@LorenaGdL: My black output problem is solved.Thanks..

Tess gravatar imageTess ( 2015-09-04 15:37:19 -0600 )edit