please help with fx

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
blackswan
Posts: 4
Joined: 2012-11-03T10:49:14-07:00
Authentication code: 67789

please help with fx

Post by blackswan »

Please!!!
Help to find a faster way to do the following
-fx 'sqrt(r**2 + g**2 + b**2)'
el_supremo
Posts: 1015
Joined: 2005-03-21T21:16:57-07:00

Re: please help with fx

Post by el_supremo »

Try (r*r + g*g + b*b). A multiply is sometimes faster than using **.

Pete
Sorry, my ISP shutdown all personal webspace so my MagickWand Examples in C is offline.
See my message in this topic for a link to a zip of all the files.
blackswan
Posts: 4
Joined: 2012-11-03T10:49:14-07:00
Authentication code: 67789

Re: please help with fx

Post by blackswan »

Thank you. It can help a bit, indeed.
But fx is very slow anyway. I was hoping to find a non-fx way.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: please help with fx

Post by fmw42 »

blackswan wrote:Please!!!
Help to find a faster way to do the following
-fx 'sqrt(r**2 + g**2 + b**2)'

I presume r,g,b are the image color channels and that you really need
-fx 'sqrt((r**2 + g**2 + b**2)/3)
to avoid overflow (larger than IM quantumrange for your compile, i.e. 255 for Q8 and 65535 for Q16). If you don't divide by 3, then you get clipping of data at white as your values will be larger than white. If you really need this, then you will have to compile IM in HDRI mode.


convert zelda3.png -set colorspace RGB -separate +channel \
\( -clone 0 -clone 0 -compose multiply -composite \) \
\( -clone 1 -clone 1 -compose multiply -composite \) \
\( -clone 2 -clone 2 -compose multiply -composite \) \
-delete 0-2 -evaluate-sequence mean \
-evaluate pow 0.5 \
zelda3_result.png



see
http://www.imagemagick.org/script/comma ... p#separate
http://www.imagemagick.org/Usage/color_basics/#channels
http://www.imagemagick.org/Usage/compose/#multiply
http://www.imagemagick.org/script/comma ... p#evaluate
http://www.imagemagick.org/script/comma ... e-sequence
http://www.imagemagick.org/Usage/basics/#clone
http://www.imagemagick.org/Usage/basics/#parenthesis
viewtopic.php?f=4&t=21269
http://www.imagemagick.org/script/high- ... -range.php
Last edited by fmw42 on 2012-11-06T18:12:56-07:00, edited 1 time in total.
blackswan
Posts: 4
Joined: 2012-11-03T10:49:14-07:00
Authentication code: 67789

Re: please help with fx

Post by blackswan »

Thank you for your answer. It's not what I'm looking for, unfortunately.
I should explain why I need that operation.
It's a kind of a 3-axis distance function for a RGB difference of two images,
distance = sqrt(r**2 + g**2 +b**2)
it allows to turn RGB difference to a better grayscale one, than allowed by standard operators, I think
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: please help with fx

Post by fmw42 »

blackswan wrote:Thank you for your answer. It's not what I'm looking for, unfortunately.
I should explain why I need that operation.
It's a kind of a 3-axis distance function for a RGB difference of two images,
distance = sqrt(r**2 + g**2 +b**2)
it allows to turn RGB difference to a better grayscale one, than allowed by standard operators, I think

That is exactly what I coded above, except it is the rms distance. The m means mean. You cannot add 3 values that range 0-255 (0-65535) in IM because it will produce a value larger than 255. Thus things will be clamped at white because IM is compiled as integer unless you compile in HDRI.

so the correct rms distance is

distance = sqrt((r**2 + g**2 +b**2)/3)

If you are on Windows then the syntax is slightly different. Remove the \ and replace the \ at the end of the lines with ^.

See http://www.imagemagick.org/Usage/windows/

which works fine for my images with the code above.

The difference between your result and mine will simply be a factor of sqrt(3). But than puts your result beyond 255 by sqrt(3) and it will be clipped to 255.

Do you really mean that you want:

distance = sqrt(((r1-r2)**2 + (g1-g2)**2 + (b1-b2)**2)/3)

where you have two images: image1 and image2? If so, you can do:

convert image1 image2 -compose difference -composite \
-set colorspace RGB -separate +channel \
\( -clone 0 -clone 0 -compose multiply -composite \) \
\( -clone 1 -clone 1 -compose multiply -composite \) \
\( -clone 2 -clone 2 -compose multiply -composite \) \
-delete 0-2 -evaluate-sequence mean \
-evaluate pow 0.5 \
result1.png

convert image1 image2 -compose difference -composite \
-set colorspace RGB -separate +channel \
\( -clone 0 -clone 0 -compose multiply -composite \) \
\( -clone 1 -clone 1 -compose multiply -composite \) \
\( -clone 2 -clone 2 -compose multiply -composite \) \
-delete 0-2 -evaluate-sequence add \
-evaluate pow 0.5 \
result2.png

However, the latter may overflow white and get clipped at white


see
http://www.imagemagick.org/Usage/compare/#difference

also
http://www.imagemagick.org/Usage/compare/#compare
http://www.imagemagick.org/Usage/compare/#statistics (-metric rmse)
Post Reply