Page 1 of 1

Replace one RGB channel with another

Posted: 2011-03-11T07:43:05-07:00
by stroker
I want to replace the red channel with the green one in an image. I have a grayscale image mask (mask.png) that represents the region I want to change. How do I do it?

The code is for a script to reduce red eyes. I want to copy the "good" pupil from the green channel into the "bad" in the red.

Re: Replace one RGB channel with another

Posted: 2011-03-11T10:34:00-07:00
by fmw42
convert image -separate -swap 0,1 -combine result

the above will swap the red and green channels in the image (if there is no alpha channel yet).

Can you provide your example images so that one can test with your mask to see how to show you how to do that as well. You need to use parenthesis processing and clones. But I am not quite sure I know exactly what you are trying to do -- are you just trying to swap the channels in one eye with the mask? Not sure if this will work if I don't understand your problem.

convert image -separate \
\( -clone 0 -clone 1 mask -compose over -composite \)\
-swap 0,3 -delete 0 result

not sure how your mask is composed, so you may need to reverse the order of the clones or use -negate on the mask.

Again it would better to have your images to test with.

Re: Replace one RGB channel with another

Posted: 2011-03-12T03:52:23-07:00
by anthony
I would probably just make a clone of the original. and swap the channels in the clone.
After that a masked composition (using '-compose Src' for correct transparency handling) will merge the two images according to your mask.

See Masked Composition, the final example.
http://www.imagemagick.org/Usage/compose/#mask_trans
As wells as..
http://www.imagemagick.org/Usage/maskin ... ed_compose
Actually that last section is not quite correct, as the blend is of the 'composited images' and the original background.
That is why I suggest you use 'src' composition, in case your images has some transparency, so that semi-transparent pixels are not adversely effected.

This by the way is also actually how Regions, Clips, Clip Paths, and Clip Masks all work.


Of course without you providing link to some example images, giving you the exact solution is difficult.