title: Opencv L*a*b* to RGB conversion produces grayscale output
body: <p>I've been assigned to change a picture from the L<em>a</em>b* color space to RGB using OpenCV. In order to do that I used the information presented <a href="https://stackoverflow.com/questions/9372626/converting-lab-values-to-rgb-values-in-opencv">here</a> and <a href="https://stackoverflow.com/questions/15583514/image-retrieval-system-by-colour-from-the-web-using-c-with-openframeworks">here</a>.</p>
  
  <p><strong>EDIT:</strong> Was assigned to do it without the  cvtColor function that comes with OpenCV.</p>
  
  <p>Also tried to implement the formulas directly from <a href="http://www.easyrgb.com/index.php?X=MATH&amp;H=08#text8" rel="nofollow noreferrer">here</a>. I'm still a newbie in image processing and don't know if my result is functional. I can see each of the channels and the parameters for the RGB image are between 0 and 255 but when merging the channels I obtain a grayscale image. I expected that after converting from L<em>a</em>b* to RGB I would get the original color image. Is that normal?</p>
  
  <pre><code>    Mat image = imread(argv[1], CV_LOAD_IMAGE_UNCHANGED);    
      Mat labimage = Mat::zeros(image.size(), image.type());  //Matriz para almacenar imagen LAB.
          cvtColor(image, labimage, CV_BGR2Lab);  //Conversion automatica RGB to lab.
  
          Mat lchannel = Mat::zeros(image.size(), labimage.type());   //Matriz para almacenar canal b.
          Mat achannel = Mat::zeros(image.size(), labimage.type());   //Matriz para almacenar canal g.
          Mat bchannel = Mat::zeros(image.size(), labimage.type());   //Matriz para almacenar canal r.
          Mat bwchannel = Mat::zeros(image.size(), labimage.type());  //Matriz para almacenar canal r.
  
  for(int x = 0;x &lt; cols;x++){
              for(int y = 0;y &lt; rows;y++){
                  lchannel.at&lt;Vec3b&gt;(y,x)[0] = labimage.at&lt;Vec3b&gt;(y,x)[0];
                  achannel.at&lt;Vec3b&gt;(y,x)[1] = labimage.at&lt;Vec3b&gt;(y,x)[1];
                  bchannel.at&lt;Vec3b&gt;(y,x)[2] = labimage.at&lt;Vec3b&gt;(y,x)[2];
              }
          }
  
  Mat color = Mat::zeros(image.size(), labimage.type());
      double X, Y, Z, dX, dY, dZ;
      double R, G, B;
      double L, a, b;
      X = Y = Z = dX = dY = dZ = R = G = B = L = a = b = 0;
  
      for(int x = 0;x &lt; cols;x++){
                  for(int y = 0;y &lt; rows;y++){
                      L = (double)(lchannel.at&lt;Vec3b&gt;(y,x)[0] / 255.0) * 100.0;       //Rango 0 a 100.
                      a = (double)(achannel.at&lt;Vec3b&gt;(y,x)[1] / 255) * 128;   //Rango -128 a 128.
                      b = (double)(bchannel.at&lt;Vec3b&gt;(y,x)[2] / 255) * 128;   //Rango -128 a 128.
  
                  // Lab -&gt; normalized XYZ (X,Y,Z are all in 0...1)
                  Y = L * (1.0/116.0) + 16.0/116.0;
                  X = a * (1.0/500.0) + Y;
                  Z = b * (-1.0/200.0) + Y;
  
                  X = X &gt; 6.0/29.0 ? X * X * X : X * (108.0/841.0) - 432.0/24389.0;
                  Y = L &gt; 8.0 ? Y * Y * Y : L * (27.0/24389.0);
                  Z = Z &gt; 6.0/29.0 ? Z * Z * Z : Z * (108.0/841.0) - 432.0/24389.0;
  
                  // normalized XYZ -&gt; linear sRGB (in 0...1)
  
                  R = X * (1219569.0/395920.0)     + Y * (-608687.0/395920.0)    + Z * (-107481.0/197960.0);
                  G = X * (-80960619.0/87888100.0) + Y * (82435961.0/43944050.0) + Z * (3976797.0/87888100.0);
                  B = X * (93813.0/1774030.0)      + Y * (-180961.0/887015.0)    + Z * (107481.0/93370.0);
  
                  // linear sRGB -&gt; gamma-compressed sRGB (in 0...1)
  
                  R = R &gt; 0.0031308 ? pow(R, 1.0 / 2.4) * 1.055 - 0.055 : R * 12.92;
                  G = G &gt; 0.0031308 ? pow(G, 1.0 / 2.4) * 1.055 - 0.055 : G * 12.92;
                  B = B &gt; 0.0031308 ? pow(B, 1.0 / 2.4) * 1.055 - 0.055 : B * 12.92;
  
                  //printf("a0: %d\t L0: %d\t b0: %d\n", achannel.at&lt;Vec3b&gt;(y,x)[1], lchannel.at&lt;Vec3b&gt;(y,x)[0], bchannel.at&lt;Vec3b&gt;(y,x)[2]);
                  //printf("a: %f\t L: %f\t b: %f\n", a, L, b);
                  //printf("X: %f\t Y: %f\t Z: %f\n", X, Y, Z);
                  //printf("R: %f\t G: %f\t B: %f\n", R, G, B);
                  //cout&lt;&lt;"R: "&lt;&lt;R&lt;&lt;" G: "&lt;&lt;G&lt;&lt;" B: "&lt;&lt;B&lt;&lt;endl;
                  //string str = type2str(color.type());
                  //cout&lt;&lt;"Matrix type: "&lt;&lt;str&lt;&lt;endl;
  
                  color.at&lt;Vec3b&gt;(y,x)[0] = R*255;
                  color.at&lt;Vec3b&gt;(y,x)[1] = G*255;
                  color.at&lt;Vec3b&gt;(y,x)[2] = B*255;
              }
          }
  </code></pre>
  
  <p>Is what I'm doing right or I'm misinterpreting the information?</p>