0

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);
Malik
  • 3,763
  • 1
  • 22
  • 35
  • As per your declaration "Mat Lum" will save a value of 32-bit floating point integer. "Lum.at(i,j)[0]", "Lum.at(i,j)[1]", and "Lum.at(i,j)[2]" cannot be used it as an array for each pixel because it returns only a integer value (you have declared only one channel ). – Dr. Mallikarjun Anandhalli Aug 08 '18 at 06:43
  • I'm not clear what you're trying to do. As @Arjun says, Lum is a single channel, so using Lum.at(i,j)[0] makes no sense. Did you mean to use src instead there? I presume you want Crom = [x,y,y]? – Pam Aug 08 '18 at 09:00

0 Answers0