channel mixer

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
bablokb
Posts: 29
Joined: 2005-06-11T06:21:59-07:00

channel mixer

Post by bablokb »

Hi,

is there a chance to get a real channel-mixer (for better B&W-conversion)? Using something like

-fx 'r*.2+g*.5+b*.3'

does work, but it is so awefully slow that it is unuseable for real-life images.

Thanks, Bernhard
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: channel mixer

Post by anthony »

for speed try...

convert image.png \
-channel R -evaluate multiply .2 \
-channel G -evaluate multiply .5 \
-channel B -evaluate multiply .3 \
+channel -separate -compose add -flatten \
image_253grey.png

I have added this to the Colors, grayscaling Images, section of IM Examples
Give it a couple of days to appear.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
bablokb
Posts: 29
Joined: 2005-06-11T06:21:59-07:00

Re: channel mixer

Post by bablokb »

Thanks for the tipp - this is much faster than the fx-method. It is still factor 2.25-slower (on my machine) than -modulate 100,0, but it is in a range that makes the conversion possible (and it's worth the effort).

Bernhard
LarsBT

Re: channel mixer

Post by LarsBT »

Hello,

What are the possibilities of applying something like gimp's vivid-saturation filter http://www.trsqr.net/photokit/vs.html - also called velvia since it mimikcs fujifilms velvia type slide film.

This uses a channel mixer. I can get the effect by issuing the following

Code: Select all

convert rose.png \
    -channel r -fx '+u.r*1.2 - u.g*0.1 - u.b*0.1' \
    -channel g -fx '-u.r*0.1 + u.g*1.2 - u.b*0.1' \
    -channel b -fx '-u.r*0.1 - u.g*0.1 + u.b*1.2' \
    rose-vivid.png
But this is also really slow. I wondered if one could do the same as above by first separating the channels, evaluate multiply and composing. But I fail to find a way to subtract the pixel values as required when mixing from across the other channels.

Thanks, Lars
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: channel mixer

Post by anthony »

You can do something like this using -recolor
It applys a RGB color matrix to the images.

However I have not tryed to figure out the exact workings as yet.

But it is there!
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: channel mixer

Post by anthony »

Hmmm... WARNING each -fx operator modified that set output channel, that is then being feed into the next -fx operator. That is it is probably NOT doing what you expect!

Look at the -recolor operator
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
LarsBT

Re: channel mixer

Post by LarsBT »

Hey thanks,

I see what you mean with the -fx operater. Of course I should keep the original and mix the channels from that one. Good point.

The -recolor operator looks promising. Not much info though.

I know you say you haven't looked into it proper, but is it correct to assume that the matrix is multiplyed on each pixel according to the channels and makeup of the matrix? (4x4 for RGBA) Thus mixing the channels.

Note, I just use the fancy buzz word "channel mixer" because that is the known tool in the point-n-clicky progs. I think it would be great for somekind of "common language". It would be so much easier to learn new tools.

/Lars
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: channel mixer

Post by anthony »

Yes that is correct. The values are a single argument of comma/space floating point values.

Hmmm... This link was mentioned at the time of the operators creation...
http://www.graficaobscura.com/matrix/index.html

I am just not certain of the matrix argument ordering, but I assume that it would be reasonably easy to work out.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
LarsBT

Re: channel mixer

Post by LarsBT »

All right

The following works beautifully

Code: Select all

convert rose.png -channel rgba -recolor \
         1.2, -0.1, -0.1, 0, \
        -0.1,  1.2, -0.1, 0, \
        -0.1, -0.1,  1.2, 0, \
           0,    0,    0, 1  \
        rose-vivid.png
Anyway, it looks the same as using the mixer in gimp with the values of 120 for the main channel and -10 for the mixing channels. I haven't investigated the exact pixel values as yet, so I'm not completely sure it's exactly the same. But it's definitely good for my purpose.

Btw it's lightning fast compared to the -fx method.

PS I'm not sure with the newlines as I'm on a windows machine right now and that's hardly bash scripting!

/Lars
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: channel mixer

Post by anthony »

I'm surprised that worked without the quotes around all the numbers!!!!
All the other options that use a lot of numbers need quotes when spaces or other special characters are involved.

Also as it is diagonally symmetrical, it would not tell us the matrix order.


What does this matrix do in any case?
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
LarsBT

Re: channel mixer

Post by LarsBT »

As to what the operater does I think (for matrix operations to work) a pixel is represented by a row vector, with the values for RGBA. Thus the result is also a row vector. That means the matrix column is the channel mix for the selected output channel of the other channels. Ie first coloumn is output to the red channel. A mix of RGBA respectively. Second coloumn is the output to the blue channels etc. So in my example the red output (first column) channel is 120% of the original red channel minus 10% of the values of the green and blue channels and nothing from the alpha channel.

The recolor matrix I show boosts the colors in the picture. Similar to the vivid filter in gimp with parameter 10. There is something about the decrease (in percentages) has to sum to zero in order to keep the brightness or luminosity.

I read something about it here: http://www.reflectiveimages.com/digitalvelvia.htm

BTW I have to admit I cheated with the code i quoted in my last post. I had no spaces and no newlines. My home machine is linux, but I'm at work now and that is windows. I imagine it needs to be quoted in bash for it to work.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: channel mixer

Post by anthony »

It only needs to be quotes if spaces, newlines or other 'weird' characters like exclamation marks, stars, etc are used. I like to space out the rows anyway to make it clearer. I am glad you admitted the code shown was edited as I could not see it running as it stood on windows, unix, or linux.

You said... I think.
Do you think, or do you know?

I could guess myself, and my guess would be the top row is the coefficients for the output red pixel. You guessed the column.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
LarsBT

Re: channel mixer

Post by LarsBT »

Phew... lots of work to get IM 6.3 to compile and work on my ubuntu.

Now, I have investigated the matrix and:

1. Your guess is right. The row is the output for the channel. Top row is red channel.
So a pixel of (8,32,128) and command is

Code: Select all

convert in.png -recolor \
     ' 0, 1, 0, 0, \
       0, 0, 0, 0, \
       0, 0, 0, 0, \
       0, 0, 0, 1'  \
       out.png
Puts the green into the red and zeros the rest. Result (32,0,0)

2. It's not a "true" matrix multiplication as the result from each row carries on to the next while multiplying. Thus the red channel is dealt with first, then the green with the any changes to the red carried on. (The problem you spotted with my initial suggestion using the -fx operator.)

If a pixel has the RGB (8,32,128) and the command is

Code: Select all

convert in.png -recolor \
    '   0, 0, 1, 0, \
        1, 0, 0, 0, \
        0, 1, 0, 0, \
        0, 0, 0, 1'  \
   out.png
which one would think rolled the channels, ie. (128,8,32). But the result is (128,128,128).
So after a little experimentation I actually believe it first copies the blue into the red (128), then copies the new red into the green (128) and finally copies the new green into the blue. Result is (128,128,128)

This is with version: ImageMagick 6.3.5 08/31/07 Q16 http://www.imagemagick.org

I don't know if this is the intention (no documentation), but now I have to figure out how to deal with it.

But the speed increase in applying the operator is significant compared the -fx, so I guess it is the right one to use.

Hope this helps

/Lars
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: channel mixer

Post by anthony »

LarsBT wrote:Your guess is right. The row is the output for the channel. Top row is red channel.
That makes things easier to understand.

LarsBT wrote:It's not a "true" matrix multiplication as the result from each row carries on to the next while multiplying. Thus the red channel is dealt with first, then the green with the any changes to the red carried on. (The problem you spotted with my initial suggestion using the -fx operator.)
Then you have found a bug.. This is a matrix operator, as as such should
be performed as a whole. Not one by one. Send in a bug report.

Hmmm quick test.... The clasical 'swap red and blue channels' problem. This is typically regarded as difficult to do, and the result on the built-in rose image is a blue rose.

Code: Select all

    convert rose: -recolor ' 0 0 1
                             0 1 0
                             1 0 0 '  rose_blue.png
Well the result definatally isn't blue, as it is more like a 'black' rose!
So I can verify -recolor is broken.

Note that I only use a 3x3 matrix. That is quite acceptable when transparency is not being modified. A 4x4 is for RGBA or CMYK images
while a 5x5 is needed for CMYKA images.

This brings use to a extra idea with the -recolor matrix (when it is working).
matrix operations on a HSL image. Could produce interesting effects.

Another example of (when working) -recolor matrix would be...
Grey scale image in a 2/5/3 ration rather than the IM default of 3/4/3

Code: Select all

    convert rose: -recolor ' .2 .5 .3
                             .2 .5 .3
                             .2 .5 .3 '  rose_grey_253.png

I did say I have not tested it and added examples in IM Examples yet.
The act of creating examples generally performs a secondary function of testing and debugging IM operators, as well as providing regression tests tha locates detrimental effects from other changes in the code.

Thanks for your efforts. I have made notes for IM examples. BUt no examples yet. The 'Color Modification' Page of IM examples, will in the next couple of months undergo a ''cellular division'' so to speak. It will divide in direct color modifications, and histogram modifications.

The former includes color selection, replacement, fuzz factors, brightening, gray-scaling, recoloring, tinting, Color Lookup tables, etc etc.

The later will include things like normalization, auto-leveling, linear and gussian equalization, contrast adjustments, and so on.

Lot of work to do.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: channel mixer

Post by anthony »

anthony wrote:Hmmm quick test.... The clasical 'swap red and blue channels' problem. This is typically regarded as difficult to do, and the result on the built-in rose image is a blue rose.

Code: Select all

    convert rose: -recolor ' 0 0 1
                             0 1 0
                             1 0 0 '  rose_blue.png
Well the result definatally isn't blue, as it is more like a 'black' rose!
So I can verify -recolor is broken.
The latest IM Beta now has the above working. -recolor seems to ne simultanious equations. Please verify.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply