Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Try this code

#include <opencv2\opencv.hpp>
#include<iostream>

using namespace cv;
using namespace std;




int main (int argc, char** argv)
{


Mat img=imread("E:\\123.jpg",CV_LOAD_IMAGE_COLOR);


Mat A(img.size(),img.type());

if(!img.data)
{
cout<<"could not open or find the image"<<endl;
return -1;
}


namedWindow("Original",0);
imshow("Original",img);



for (int i=0;i<img.rows;i++)
{
for(int j=0;j<img.cols;j++)
{
Vec3f intensity = img.at<Vec3b>(i,j);
float blue=intensity.val[0];
float green=intensity.val[1];
float red=intensity.val[2];

double sum=red+blue+green;
double r= red/sum;
double g= green/sum;
double b = blue/sum;


       A.data[A.step[0]*i + A.step[1]* j + 0] = (b*255);
           A.data[A.step[0]*i + A.step[1]* j + 1] = (g*255);
           A.data[A.step[0]*i + A.step[1]* j + 2] = (r*255); 

    }
}

namedWindow("chrom",0);

imshow("chrom",A);


waitKey(0);

}

image description

image description