Ask Your Question

Revision history [back]

Hi Please note that conversion from one color space to another color space is not always reversible. i.e You cannot expect to get back the original image after conversion.

Here is a Sample code for Transforming an Image using 3x3 Color Transformation Matrix. You have to find correct transformation matrix for your color Space.

Note: Please refrain from using loops in your code and try to use existing OpenCV functions as much as possible.

void imshow_RGB(string WindowName,Mat mSrc)
{
    Mat mSrc_BGR;
    cvtColor(mSrc, mSrc_BGR, CV_RGB2BGR);
    imshow(WindowName, mSrc_BGR);
}

int main(int argc, const char** argv)
{
    cv::Mat mSrc = cv::imread("C:\\Users\\Public\\Pictures\\Sample Pictures\\Koala.jpg", 1);
    cv::Mat mDst;
    if (mSrc.empty())
    {
        cout << "Invalid input image";
        return 0;
    }

    cvtColor(mSrc, mSrc, CV_BGR2RGB);
    imshow_RGB("Source Image", mSrc);

    Mat mRGB2LMS_Matrix= Mat::eye(3,3,CV_32FC1);
    Mat mLMS2RGB_Matrix = Mat::eye(3, 3, CV_32FC1);

    //Fill your 3x3 Transformation Matrix here!!
    /*mRGB2LMS_Matrix = (Mat_<float>(3, 3) <<  17.8824, 43.5161, 4.1194
                                                3.4557, 27.1554, 3.8671,
                                                0.0300, 0.1843 , 1.4671  );

    mLMS2RGB_Matrix = (Mat_<float>(3, 3) << -0.0004, -0.0041, 0.6935,
                                            -0.0102, 0.0540, -0.1136,
                                            0.0809, -0.1305, 0.1167);*/

    //Forward Transformation from RGB -> Destination Color Space
    transform(mSrc, mDst, mRGB2LMS_Matrix);
    imshow_RGB("Forward Transformation", mDst);

    //Inverse Transformation from Destination Color Space -> RGB Color Space 
    transform(mDst, mSrc, mLMS2RGB_Matrix);
    imshow_RGB("Inverse Transformation Image", mSrc);

    waitKey();
    return 0;
}

Hi Please note that conversion from one color space to another color space is not always reversible. i.e You cannot expect to get back the original image after conversion.

Here is a Sample code for Transforming an Image using 3x3 Color Transformation Matrix. You have to find correct transformation matrix for your color Space.

Note: Please refrain from using loops in your code and try to use existing OpenCV functions as much as possible.

void imshow_RGB(string WindowName,Mat mSrc)
{
    Mat mSrc_BGR;
    cvtColor(mSrc, mSrc_BGR, CV_RGB2BGR);
    imshow(WindowName, mSrc_BGR);
}

int main(int argc, const char** argv)
{
    cv::Mat mSrc = cv::imread("C:\\Users\\Public\\Pictures\\Sample Pictures\\Koala.jpg", 1);
    cv::Mat mDst;
    if (mSrc.empty())
    {
        cout << "Invalid input image";
        return 0;
    }

    cvtColor(mSrc, mSrc, CV_BGR2RGB);
    imshow_RGB("Source Image", mSrc);

    Mat mRGB2LMS_Matrix= Mat::eye(3,3,CV_32FC1);
    Mat mLMS2RGB_Matrix = Mat::eye(3, 3, CV_32FC1);

    //Fill your 3x3 Transformation Matrix here!!
    /*mRGB2LMS_Matrix = (Mat_<float>(3, 3) <<  17.8824, 43.5161, 4.1194
        4.1194,
                                        3.4557, 27.1554, 3.8671,
                                         0.0300, 0.1843 , 1.4671  );

    0.1843, 1.4671);

mLMS2RGB_Matrix = (Mat_<float>(3, 3) << -0.0004, -0.0041, 0.6935,
    0.0809, -0.1305, 0.1167,
                                        -0.0102, 0.0540, -0.1136,
                                            0.0809, -0.1305, 0.1167);*/
-0.0004, -0.0041, 0.6935);*/

    //Forward Transformation from RGB -> Destination Color Space
    transform(mSrc, mDst, mRGB2LMS_Matrix);
    imshow_RGB("Forward Transformation", mDst);

    //Inverse Transformation from Destination Color Space -> RGB Color Space 
    transform(mDst, mSrc, mLMS2RGB_Matrix);
    imshow_RGB("Inverse Transformation Image", mSrc);

    waitKey();
    return 0;
}