# Convert rgb (brg) to rg chromaticity

I simply can't for the love of god do a simple damn conversion and it's been bugging me over the last weekend: https://en.wikipedia.org/wiki/Rg_chro...

Can anyone help out with an efficient way to do this in OpenCV? I keep getting black images, here's my current attempt:

```
for (int i = 0; i < mRgb.cols; i++) {
for (int j = 0; j < mRgb.rows; j++) {
Vec3b intensity = mRgb.at<Vec3b>(j, i);
float b = intensity.val[0];
float g = intensity.val[1];
float r = intensity.val[2];
float rgbSum = (float) (r + g + b);
float redNorm = (float) (r / rgbSum);
float greenNorm = (float) (g / rgbSum);
float blueNorm = (float) (b / rgbSum);
res.at<Vec3b>(j, i)[0] = blueNorm;
res.at<Vec3b>(j, i)[1] = greenNorm;
res.at<Vec3b>(j, i)[2] = redNorm;
}
}
```

I am positive this can be done more efficiently, please help me out.

Isn't there a way in which you can split the channels, divide them to the channel sum and then add up the 3 new channels into a new image? In matlab it's done like this: http://stackoverflow.com/questions/21...

Edit, here's a second attempt using channels:

```
Mat channel[3];
split(mRgb, channel);
double alpha = 1.0;
double beta = 1.0;
Mat tmpSum;
Mat rgbSum;
addWeighted(channel[0], alpha, channel[1], beta, 0.0, tmpSum);
addWeighted(tmpSum, alpha, channel[2], beta, 0.0, rgbSum);
Mat newR;
Mat newG;
Mat newB;
divide(channel[0], rgbSum, newB);
divide(channel[1], rgbSum, newG);
divide(channel[2], rgbSum, newR);
Mat in[] = { newB, newG, newR };
int from_to[] = { 0, 0, 1, 1, 2, 2 };
mixChannels(in, 3, &mRgb, 1, from_to, 3);
```

I get the famous fatal signal 11 on Android using OpenCV in ndk.

Regards!

Well reading a bit on your own wikipedia page, it seems that:

The sum of rgb will always equal one, because of this property the b dimension can be thrown away without causing any loss in information.If you think about this than getting a black image is completely normal if you visualise all 3 channels together! The idea is to do data reduction and convert your 3 channel BGR image into a 2 channel GR image. I will post some code soon!