# Revision history [back]

### 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_chromaticity

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/21712828/convert-image-to-a-chromaticity-image

Regards!

### 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_chromaticity

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;
}
}


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/21712828/convert-image-to-a-chromaticity-image

Regards!

### 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_chromaticity

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;
}
}


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/21712828/convert-image-to-a-chromaticity-image

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, newR);
divide(channel[1], rgbSum, newG);
divide(channel[2], rgbSum, newB);

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!

### 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_chromaticity

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;
}
}


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/21712828/convert-image-to-a-chromaticity-image

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, newR);
newB);
divide(channel[1], rgbSum, newG);
divide(channel[2], rgbSum, newB);
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!

 5 retagged sturkmen 6772 ●3 ●48 ●79 https://github.com/stu...

### 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_chromaticity

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;
}
}


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/21712828/convert-image-to-a-chromaticity-image

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!