Page 1 of 1

SOLVED: Understanding an fx operator?

Posted: 2009-05-10T19:40:35-07:00
by evank
I have a convert command with a special effects image operator:

Code: Select all

convert input.png gradient.png -fx "v.p{0,u*v.h}" output.png
On a high level, I understand what this command is accomplishing: it takes the colors from a gradient image (gradient.png) and applies them as the color palette of the input image (input.png), writing to an output image (output.png).

From what I've figured out, u is the input image, v is the gradient, and it is going through each left-most pixel in the gradient from top to bottom, somehow applying its colors to the input image. But I can't wrap my head around how it's accomplishing that last part, or how to do the same thing in a procedural manner.

I have a piece of software using this command that I'd like to replicate with other image manipulation libraries (to also support anyone in an environment without ImageMagick bindings available). So my question is, can anyone break down exactly what this fx operator is doing in some kind of psuedocode?

Update: Wow, thanks. That pretty much explains it to a tee!

Re: Understanding an fx operator?

Posted: 2009-05-10T21:23:02-07:00
by anthony
That is (almost) equivelent to a -clut operator ;-)
For more details See IM Examples, Color Modifications, CLUT
http://www.imagemagick.org/Usage/color/#clut

Lets break down the formula...

First FX loops through each pixel of the input image (no control at this time though -geagion has been proposed to limit the area of the input image looped through), and also through each of the three color channels (controled by the -channel value that defaults to just RGB channels (just the color channesl not alpha).
that means the FX formula will be exected width * height * 3 times! that is a lot!

Now as you guessed...
u = color (value) of the current pixel (and channel) in input image
the input image (u) is presumably a greyscale image so each channel value is the same, unless you are doing a histogram adjustment basied on a greyscale gradient in the second 'v' image.

v.h = is the height of the second image.

u*v.h = Multiply the color value (range 0 to 1) with the heigh of the second 'v' image.
that is convert the color into a height index position in the second image.

v.p{} = look up the color from 'v' at position given

v.p{0, u*v.h} get the color from the first column of 'v' at a distance u*v.h from the top.

In other word replace each color value in the input 'u' image, with the color in the second 'v' image indexed by the original color value.

Or A Color Lookup using the second image as the Lookup table.

As mentioned this is (almost) exactly the same as the newer -clut operator that was developed from that specific -fx formula. The only difference is instead of v.p{0,u*v.h} it is actually using v.p{u*v.w,u*v.h} or an indexed lookup along a diagonal line though the second lookup-table-image. this means that the lookup table image can be either single row, or a single column image, and things will still come out correct.

Actually the final formula is a little more complex than that to take into account the current -interpolation setting. It also is effected at extreme values by the -virtual-pixel setting, so it is a good idea to ensure the defualt 'edge' VP setting is in use.

For more details See IM Examples, Color Modifications, CLUT
http://www.imagemagick.org/Usage/color/#clut