how have a chrominance in RGB channels in opencv?

205 Views Asked by At

I have the next problem

I want to use this equation in openCV

X = blue channel , Y = green channel and Z = red channel

x = X / (X + Y + Z);  

y = Y / (X + Y + Z); 

Z = Z / (X + Y + Z);

But when i run my code I have a window whit 3 images on 1.

First I extract the luminance because I want the crominance of the red channel.

Can anybody help me?

Here is my code:

 Mat source = cv::imread("Image.bmp");
    imshow("Original",source);

Mat src(source.rows, source.cols, CV_32FC3);
normalize(source,src,0,1,CV_MINMAX,CV_32FC1);

Mat Lum = Mat::zeros(src.rows, src.cols, CV_32FC1);
Mat Crom = Mat::zeros(src.rows, src.cols, CV_32FC3);

for (size_t i = 0; i < src.rows; i++)
{
    for (size_t j = 0; j < src.cols; j++)
    {
        Vec3f pixel = src.at<Vec3f>(i, j);
        float B = pixel[0];
        float G = pixel[1];
        float R = pixel[2];

        Lum.at<float>(i, j) = (  B + G + R ) /3;
    }
}   
    imshow("Lum",Lum);
    ///Codigo para la Cromancia   

for (size_t i = 0; i < Lum.rows; i++)
{
    for (size_t j = 0; j < Lum.cols; j++)
    {
        Vec3f pixel = src.at<Vec3f>(i,j);
        float B = pixel[0];
        float G = pixel[1];
        float R = pixel[2];    

        Crom.at<Vec3f>(i,j)[0] = ( Lum.at<Vec3f>(i,j)[0] )/ (  Lum.at<Vec3f>(i,j)[0] + Lum.at<Vec3f>(i,j)[1] + Lum.at<Vec3f>(i,j)[2] );
        Crom.at<Vec3f>(i,j)[1] = ( Lum.at<Vec3f>(i,j)[1] )/ (  Lum.at<Vec3f>(i,j)[0] + Lum.at<Vec3f>(i,j)[1] + Lum.at<Vec3f>(i,j)[2] );
        Crom.at<Vec3f>(i,j)[2] = ( Lum.at<Vec3f>(i,j)[2] )/ (  Lum.at<Vec3f>(i,j)[0] + Lum.at<Vec3f>(i,j)[1] + Lum.at<Vec3f>(i,j)[2] );    
    }
}    
imshow("Cromancia",Crom);
0

There are 0 best solutions below