Implementation of Matlab function rgb2lab in opencv using c++. [3 Attachments]

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Implementation of Matlab function rgb2lab in opencv using c++. [3 Attachments]

opencv-users mailing list
I am trying to implement Matlab function rgb2lab in opencv using c++. I used opencv cvtcolor function for the
implementation. Then I did another implementation.Here is my 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];
         r=R/255.f;
         G= intensity.val[1];
         g=G/255.f;
         B= intensity.val[0];
         b=B/255.f;
         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;
           xr = X/Xr;
           yr = Y/Yr;
           zr = Z/Zr;
           if ( xr > eps )
              fx =  (float)(xr, 1/3.);
           else
              fx = (float) ((k * xr + 16.) / 116.);
           if ( yr > eps )
              fy =  (float)pow(yr, 1/3.);
           else
              fy = (float) ((k * yr + 16.) / 116.);
           if ( zr > eps )
              fz =  (float)pow(zr, 1/3.);
           else
              fz = (float) ((k * zr + 16.) / 116);
           Ls = ( 116 * fy ) - 16;
           as = 500*(fx-fy);
           bs = 200*(fy-fz);
           Result.at<Vec3b>(i,j)[0]=saturate_cast<uchar>(2.55*Ls + .5);
           Result.at<Vec3b>(i,j)[1]=saturate_cast<uchar>(as + .5);
           Result.at<Vec3b>(i,j)[2]=saturate_cast<uchar>(bs + .5);

       }
     }
Both implementation results are not similar to Matlab output image. Please find the 3 image files  :  1)my first output image(using cvtcolor),2) second output image(using above code) and 3) third matlab ouput image.  Please anyone help me to find the correct way for implementation.
 

Reply | Threaded
Open this post in threaded view
|

Re: Implementation of Matlab function rgb2lab in opencv using c++. [3 Attachments]

keghn
  Can you please put in the headers and includes and other stuff to make program run.
 I would love to copy and past into a file, then trouble shoot it. Other wise it will take a while
hook every thing up:(

 Can you please put in the before and after image of  the matlab processing.

Then in a different area way way away from the matlab images show
the before and after processing by OpneCV.