Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Converting RGB into LMS model using C++ and OpenCV

I'm trying to convert image with RGB colorspace into LMS model. The transformation matrix value I got from this paper. I read this Question : RGB to LMS color space conversion with OpenCV, we referred to the same paper and I already followed all the instructions in the answer.

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.

image description

But the output doesn't match the original image source. Here's my code :

void test(const Mat &original, Mat &pic, int rows, int cols)
{
    for (int i = 0; i < original.rows; i++) {
        for (int j = 0; j < original.cols; j++) {
        //RGB into LMS
            pic.at<Vec3b>(i, j)[0] =    original.at<Vec3b>(i, j)[0] * 1.4671 + 
                                        original.at<Vec3b>(i, j)[1] * 0.1843 + 
                                        original.at<Vec3b>(i, j)[2] * 0.003;   //B-->S
            pic.at<Vec3b>(i, j)[1] =    original.at<Vec3b>(i, j)[0] * 3.8671 + 
                                        original.at<Vec3b>(i, j)[1] * 27.1554 + 
                                        original.at<Vec3b>(i, j)[2] * 3.4557;   //G-->M
            pic.at<Vec3b>(i, j)[2] =    original.at<Vec3b>(i, j)[0] * 4.1194 + 
                                        original.at<Vec3b>(i, j)[1] * 43.5161 + 
                                        original.at<Vec3b>(i, j)[2] * 17.8824;  //R-->L


            //LMS back into RGB
            pic.at<Vec3b>(i, j)[0] =    pic.at<Vec3b>(i, j)[0] * 0.6935 + 
                                        pic.at<Vec3b>(i, j)[1] * -0.0041 + 
                                        pic.at<Vec3b>(i, j)[2] * -0.0004;   //S-->B
           pic.at<Vec3b>(i, j)[1] =     pic.at<Vec3b>(i, j)[0] * -0.1136 + 
                                        pic.at<Vec3b>(i, j)[1] * 0.0540 + 
                                        pic.at<Vec3b>(i, j)[2] * -0.0102;   //M-->G
           pic.at<Vec3b>(i, j)[2] =     pic.at<Vec3b>(i, j)[0] * 0.1167 + 
                                        pic.at<Vec3b>(i, j)[1] * -0.1305 + 
                                        pic.at<Vec3b>(i, j)[2] * 0.0809;   //L-->R

        }
    }
}

Main

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);
}

Any suggestion ? Any help will be appreciated.