reproducing Spherical Distortion Map

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
gubach
Posts: 45
Joined: 2013-12-13T11:13:29-07:00
Authentication code: 6789

reproducing Spherical Distortion Map

Post by gubach » 2019-08-20T11:21:40-07:00

I am trying to reproduce the steps for generating a Spherical Distortion Map (http://www.imagemagick.org/Usage/mapping/#spherical). I am calling IM with this string in direct analogy to the given one in the text:

Code: Select all

"C:\Program Files\ImageMagick-6.9.7-Q8\convert.exe" -size 1500x1500 xc: -channel R -fx 'yy=(j+.5)/h-.5; (i/w-.5)/(sqrt(1-4*yy^2))+.5' -separate +channel sphere_lut.png
and I am getting the following error messages:

convert.exe: unable to parse expression `yy' @ error/fx.c/FxGetSymbol/1850.
convert.exe: unable to parse expression `'yy' @ error/fx.c/FxEvaluateSubexpression/2331.
convert.exe: unable to open image `(i/w-.5)/(sqrt(1-4*yy2))+.5'': No such file or directory @ error/blob.c/OpenBlob/2701.
convert.exe: unable to open module file `C:\Program Files\ImageMagick-6.9.7-Q8\modules\coders\IM_MOD_RL_5'_.dll': No such file or directory @ warning/module.c/GetMagickModulePath/676.
convert.exe: no decode delegate for this image format `5'' @ error/constitute.c/ReadImage/504.

The result png is a white image.

User avatar
fmw42
Posts: 25657
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: reproducing Spherical Distortion Map

Post by fmw42 » 2019-08-20T11:26:26-07:00

On Windows, best to use double quotes. Try

Code: Select all

convert -size 1500x1500 xc: -channel R -fx "yy=(j+.5)/h-.5; (i/w-.5)/(sqrt(1-4*yy^2))+.5" -separate +channel sphere_lut.png
This works fine for me on Mac OSX.

gubach
Posts: 45
Joined: 2013-12-13T11:13:29-07:00
Authentication code: 6789

Re: reproducing Spherical Distortion Map

Post by gubach » 2019-08-21T00:10:23-07:00

Hello mw42,
thanks for answering. Works now fine!

gubach
Posts: 45
Joined: 2013-12-13T11:13:29-07:00
Authentication code: 6789

Re: reproducing Spherical Distortion Map

Post by gubach » 2019-08-21T01:16:35-07:00

I am now on the third step "sphere_overlay". This line

Code: Select all

"C:\Program Files\ImageMagick-6.9.7-Q8\convert.exe" sphere_mask.png ( +clone -blur 0x20 -shade 110x21.7 -contrast-stretch 0% +sigmoidal-contrast 6x50% -fill grey50 -colorize 10% ) -composite sphere_overlay.png
generates no errors but an image that looks very different from the one in the description: Image
My result is on https://www.flickr.com/photos/gbachelier/48590511801.

gubach
Posts: 45
Joined: 2013-12-13T11:13:29-07:00
Authentication code: 6789

Re: reproducing Spherical Distortion Map

Post by gubach » 2019-08-21T03:21:47-07:00

Apart from the HardLight and CopyOpacity compositing I tested the actual spherical projection (or simulation of it) with

Code: Select all

convert in.jpg sphere_lut.png -fx "p{ v*w, j }" out.jpg
and I got an interesting Glitch Art image: https://www.flickr.com/photos/gbachelier/48591195817.
A sphere'ish shape is recognizable in the north-west geometry of the result and with some crop, upscale and masking with a circle this could be further used but its not the proposed processing pipeline from http://www.imagemagick.org/Usage/mapping/#spherical.

gubach
Posts: 45
Joined: 2013-12-13T11:13:29-07:00
Authentication code: 6789

Re: reproducing Spherical Distortion Map

Post by gubach » 2019-08-21T04:05:06-07:00

I found the reason why the sphere is smaller: Because the run time of the sphere_lut generation is very high even in the 1500x1500 test case I generated a smaller version and upscaled the png later. This is obviously not compatible with the later usage of sphere_lut as a displacement map with -fx "p{ v*w, j }". If generating it with the size of the later input.jpg I got at least the proportions right: https://www.flickr.com/photos/gbachelier/48591358527. But now I see all the artifacts (aliasing zig-zag at edges) within the sphere ... result quality seems too bad.

snibgo
Posts: 12289
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: reproducing Spherical Distortion Map

Post by snibgo » 2019-08-21T06:55:25-07:00

I'm a bit lost. Do you have an unanswered question?
gubach wrote:...the run time of the sphere_lut generation is very high even in the 1500x1500 test case ...
"-fx" is slow. I find it too slow to be useful for images that have more than, say, 64000 pixels. Generally there are faster alternatives: "-evaluate", "-compose Displace", "-compose Distort", and so on.
snibgo's IM pages: im.snibgo.com

gubach
Posts: 45
Joined: 2013-12-13T11:13:29-07:00
Authentication code: 6789

Re: reproducing Spherical Distortion Map

Post by gubach » 2019-08-24T06:24:48-07:00

@snibo: in the forth post I had difficulties in reproducing sphere_overlay.png from the sphere_mask.png using the givien code; I go a very different looking grayscale image. But for test purpes I upscaled the thumbnail in the Usage-page for a HardLight compositing test.
But after I saw the very poor quality of the actual sphere mapping (https://www.flickr.com/photos/gbachelier/48591358527 : everywhere coarse zig-zags; why?) I doubt that it makes sense to investigate it further.

snibgo
Posts: 12289
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: reproducing Spherical Distortion Map

Post by snibgo » 2019-08-24T07:08:29-07:00

Displacement maps need to be high-precision. A small error (eg quantization) such as one part in 500 in an ordinary image may be invisible, but the same error in a displacement map will be far more obvious in the displaced result.

The zig-zags come from a low-precision displacement map. For images above about 1000x1000 pixels, I recommend the map should be at least 32 bits/channel/pixel. Floating point is generally better than integer.

Your maps are recorded in PNG which can only record 16 bits/channel/pixel, integer only. Downloading your https://www.flickr.com/photos/gbachelier/48590511801 , I see it is only 8 bits/channel/pixel. (Only one channel because it is gray, which is okay.)
snibgo's IM pages: im.snibgo.com

snibgo
Posts: 12289
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: reproducing Spherical Distortion Map

Post by snibgo » 2019-08-24T09:50:51-07:00

IM v6 by default doesn't process alpha. IM v7 by default does process alpha, so we can use "-alpha off" when we really don't want alpha.

"-fx" is evaluated at every pixel, so I replace that with "-evaluate Add %%[fx:...]" etc where the fx is evaluated only once per image.

I use "-compose Distort -composite" for the distorton, after setting the map green channel to a linear gradient.

I store the map in a 32-bit floating-point miff, and make a PNG but only for ease of viewing.

I've retained Anthony's separate commands. They could be combined into a single command. Windows BAT syntax:

Code: Select all

rem See https://www.imagemagick.org/Usage/mapping/#spherical

set SIZE=1000x1000

%IMG7%magick ^
  -define compose:clamp=off ^
  -size %SIZE% gradient:Black-White ^
  -evaluate Add %%[fx:QuantumRange/h/2-QuantumRange/2] ^
  -function Polynomial -4,0,1 ^
  -evaluate Pow 0.5 ^
  ( -size %SIZE% gradient: -rotate 90 ^
    -evaluate Subtract 50%% ^
  ) ^
  -compose DivideDst -composite ^
  -evaluate Add 50%% ^
  -size %SIZE% gradient:Black-White ^
  ( +clone ) ^
  -combine ^
  -alpha off ^
  -define quantum:format=floating-point ^
  -depth 32 ^
  +write sphere_lut2.miff ^
  sphere_lut2.png

%IMG7%magick ^
  -size %SIZE% xc:black ^
  -fill white ^
  -draw "circle %%[fx:w/2-1/2],%%[fx:h/2-1/2] %%[fx:w/2-1/2],0" ^
  -alpha off ^
  sphere_mask.png

%IMG7%magick ^
  sphere_mask.png ^
  ( +clone -blur 0x20 -shade 110x21.7 -contrast-stretch 0%% ^
    +sigmoidal-contrast 6x50%% ^
    -fill grey50 -colorize 10%% ^
  ) ^
  -compose Over -composite ^
  -alpha off ^
  sphere_overlay.png

%IMG7%magick ^
  lena.png -resize %SIZE% ^
  sphere_lut2.miff ^
  -compose Distort -composite ^
  sphere_overlay.png -compose HardLight  -composite ^
  sphere_mask.png -alpha off -compose CopyOpacity -composite ^
  sphere_lena3.png
snibgo's IM pages: im.snibgo.com

Post Reply