title: Converting RGB into LMS model using C++ and OpenCV
body: <p>I'm trying to convert image with RGB colorspace into LMS model. The transformation matrix value I got from <a href="https://arxiv.org/pdf/1711.10662.pdf" rel="nofollow noreferrer">this paper</a>. I read this <a href="https://stackoverflow.com/questions/35671531/rgb-to-lms-color-space-conversion-with-opencv">Question : RGB to LMS color space conversion with OpenCV</a>, we referred to the same paper and I already followed all the instructions in the answer.</p>
  
  <p>To make sure my codes do well, I convert the original RGB into LMS, then convert the LMS back into RGB using inverse matrix of the first matrix, and see if the output matches the original image. 
  <a href="https://i.stack.imgur.com/6YVrl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/6YVrl.png" alt="enter image description here"></a></p>
  
  <p>But the output doesn't match the original image source. 
  Here's my code :
  <strong>[UPDATED]</strong></p>
  
  <pre><code>void test(const Mat &amp;original, Mat &amp;pic, int rows, int cols)
  {
      for (int i = 0; i &lt; original.rows; i++) {
          for (int j = 0; j &lt; original.cols; j++) {
          //RGB into LMS
              pic.at&lt;Vec3b&gt;(i, j)[0] =    original.at&lt;Vec3b&gt;(i, j)[0] * 1.4671 + 
                                          original.at&lt;Vec3b&gt;(i, j)[1] * 0.1843 + 
                                          original.at&lt;Vec3b&gt;(i, j)[2] * 0.003;   //B--&gt;S
              pic.at&lt;Vec3b&gt;(i, j)[1] =    original.at&lt;Vec3b&gt;(i, j)[0] * 3.8671 + 
                                          original.at&lt;Vec3b&gt;(i, j)[1] * 27.1554 + 
                                          original.at&lt;Vec3b&gt;(i, j)[2] * 3.4557;   //G--&gt;M
              pic.at&lt;Vec3b&gt;(i, j)[2] =    original.at&lt;Vec3b&gt;(i, j)[0] * 4.1194 + 
                                          original.at&lt;Vec3b&gt;(i, j)[1] * 43.5161 + 
                                          original.at&lt;Vec3b&gt;(i, j)[2] * 17.8824;  //R--&gt;L
  
  
              //LMS back into RGB
              pic.at&lt;Vec3b&gt;(i, j)[0] = pic.at&lt;Vec3b&gt;(i, j)[0] * 0.6935 + pic.at&lt;Vec3b&gt;(i, j)[1] * -0.0041 + pic.at&lt;Vec3b&gt;(i, j)[2] * -0.0004;   //S--&gt;B
              pic.at&lt;Vec3b&gt;(i, j)[1] = pic.at&lt;Vec3b&gt;(i, j)[0] * -0.1136 + pic.at&lt;Vec3b&gt;(i, j)[1] * 0.0540 + pic.at&lt;Vec3b&gt;(i, j)[2] * -0.0102;   //M--&gt;G
              pic.at&lt;Vec3b&gt;(i, j)[2] = pic.at&lt;Vec3b&gt;(i, j)[0] * 0.1167 + pic.at&lt;Vec3b&gt;(i, j)[1] * -0.1305 + pic.at&lt;Vec3b&gt;(i, j)[2] * 0.0809;   //L--&gt;R
  
          }
      }
  }
  </code></pre>
  
  <p>Main</p>
  
  <pre><code>int main(int argv, char** argc){
  Mat original= imread("original.png", CV_LOAD_IMAGE_COLOR);
  int rows = original.rows;
  int cols = original.cols;
  
  Mat pic(rows, cols, CV_8UC3);
  
  test(original, pic, rows, cols);
  imwrite("pic.png", pic);
  }
  </code></pre>
  
  <p>Any suggestion ? Any help will be appreciated.</p>