0
$\begingroup$

I have a picture and trying to swap the RGB channel into RRB, it's like I need to change all G pixels value into R. I did that by copying the R pixel value to G and store it into a new image file (Mat). Here's my code :

ori.at<Vec3b>(i, j)[0] = image.at<Vec3b>(i, j)[0]; // B-->B
ori.at<Vec3b>(i, j)[1] = image.at<Vec3b>(i, j)[2]; // G-->R
ori.at<Vec3b>(i, j)[2] = image.at<Vec3b>(i, j)[2]; // R-->R

however the result is a little bit confusing me,

enter image description here

Could someone give me a brief explanation how is that the Red and Yellow pixels in the wall end up have the almost same result color (aquamarine) ? I checked the pixel value at some points and the value is almost the same.

What I expected was, if I copied R pixels --> G, part with original R values shouldn't be affected, right ? And because Yellow is a mixture of Red + Green, if I try to copy Red pixels into Green, shouldn't it be Red + Red then ?

[UPDATE]

I have checked sample color values (RGB) for Red wall and Yellow wall.

Red wall :              Yellow wall :
R --> 200 195 185       R --> 237 226 220
G -->   2   1   1       G --> 197 179 168
B -->   1   1   0       B -->   4  29  23

Copying the value R --> G :
Red wall :              Yellow wall :
R --> 200 195 185       R --> 237 226 220
G --> 200 195 185       G --> 237 226 220
B -->   1   1   0       B -->   4  29  23

will result in the Red wall and Yellow wall have almost the same color. But when I checked the result color, it's not aquamarine. Did I make wrong calculation ?

enter image description here

$\endgroup$
  • $\begingroup$ I don't see how your code and your description and code comments fit together. Your code says you assign R->R, B->G, B->B, assuming normal channel RGB ordering. $\endgroup$ – Jazzmaniac Jan 9 '18 at 20:19
  • $\begingroup$ @Jazzmaniac In OpenCV default color space is BGR. Any picture will get loaded in BGR format by default. So the channel will always be [0]=B, [1]=G, [2]=R. $\endgroup$ – raisa_ Jan 9 '18 at 23:47
0
$\begingroup$

I missed the conversion part.
I converted the result image from BGR into RGB using this command :

cvtColor(image, image, COLOR_BGR2RGB);

Then I exported the result image into image file.

imwrite("image.png", image); 

Turned out the export command will automatically convert the BGR color-space into the RGB color-space. This is why the color result is off because I did 2x conversion.
So, I don't need the conversion command.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.