1

Naive question, I'm working with color processing for the first time. I want to swap RGB channel from Mat image. What I'm trying to do create another image from the original image and assign the R and G value as R. And then on another image, assign R and G value as G. Here's my code :

Mat testcolor = imread("tulip.jpg", CV_LOAD_IMAGE_COLOR);
int rows = testcolor.rows;
int cols = testcolor.cols;

Mat leftimg(rows, cols, CV_8UC3);
Mat rightimg(rows, cols, CV_8UC3);

cvtColor(testcolor, testcolor, COLOR_RGB2BGR);
for (int i = 0; i < testcolor.rows; i++) {
    for (int j = 0; j < testcolor.cols; j++  ){ 
        leftimg.at<Vec3b>(i, j)[0] = testcolor.at<Vec3b>(i,j)[0];
        leftimg.at<Vec3b>(i, j)[1] = testcolor.at<Vec3b>(i,j)[0];
        leftimg.at<Vec3b>(i, j)[2] = testcolor.at<Vec3b>(i,j)[2];

        rightimg.at<Vec3b>(i, j)[0] = testcolor.at<Vec3b>(i, j)[1];
        rightimg.at<Vec3b>(i, j)[1] = testcolor.at<Vec3b>(i, j)[1];
        rightimg.at<Vec3b>(i, j)[2] = testcolor.at<Vec3b>(i, j)[2];
    }
}

cvtColor(leftimg, leftimg, COLOR_BGR2RGB);
imwrite("leftimg.png", leftimg);
cvtColor(rightimg, rightimg, COLOR_BGR2RGB);
imwrite("rightimg.png", rightimg);

But I don't think I got the result that I expected. This is the original image. This is the original image

and this is the reult image : enter image description here

I expect something like, if I assign R and G as R --> The green part will have some red shades. And if I assign R and G as G, the red part will have some green shades. Did I miss something in my code ? or I'm lacking understanding ?

In this case, I dont have raw image, and I didn't convert my color space because I wanted to see how it worked with RGB. And then if needed I will convert the color space later.

raisa_
  • 594
  • 2
  • 10
  • 28

2 Answers2

1

With your current implementation if RGB value at a pixel was say ( 100, 150, 200 ) on performing (R,G)->R conversion you will end up with value ( 100, 100, 200 ). So the pixel will have both Red and Green component.

But, I believe, what you want as a result, is an image with no green component in the pixels.

For that you would have to do something as follows for each pixel.

R = Function( R, G )
and 
G = 0

for each pixel.

Here the Function would represent the way you want to combine the R and G values of a pixel to form the new R value of that pixel. And since you make G as 0, you wont have a green shade in that pixel.

sajas
  • 1,599
  • 1
  • 17
  • 39
1

If we mixture different B-G-R components, we can get more than B-G-R. such as : B(high)+G(high)+R(low) => Yellow; B(high)+G(low)+R(high) => Purple.

Here is an example:

enter image description here

And the dependent channels are as follow:

enter image description here

Now go back to your flowers image, the big difference in R and G channels are the Red flowers regions. If you replace the G by R, then the regions become B(low) + R(high) + R(high) => yellow. If you replace the R by G, then become B(low) + G(low) + G(low) => dark.

enter image description here


Here is a map of Hue in HSV.

And an example to find green in HSV is here.

How to define a threshold value to detect only green colour objects in an image :Opencv

Kinght 金
  • 17,681
  • 4
  • 60
  • 74
  • You explanation saved me. How can we tell if a channel is High or Low ? – raisa_ Dec 22 '17 at 15:12
  • Generally speaking, you should convert to `HSV` to split the specific color(such as green). Maybe this help https://stackoverflow.com/questions/47483951/how-to-define-a-threshold-value-to-detect-only-green-colour-objects-in-an-image/47483966#47483966 – Kinght 金 Dec 22 '17 at 15:18
  • I did convert my color space before, but I still need to decide if what I'm trying to do is best fit with RGB or HSV, that why I want to try with RGB too. – raisa_ Dec 22 '17 at 15:22
  • Then just try as you wish. – Kinght 金 Dec 22 '17 at 15:25