Cylindrical displacement problem

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
7dgedas
Posts: 1
Joined: 2011-04-09T14:45:35-07:00
Authentication code: 8675308

Cylindrical displacement problem

Post by 7dgedas »

I found quite cool example in IM docs which i thought would perfectly fit my needs for project: "cylindrical displacement", http://www.imagemagick.org/Usage/mappin ... e_cylinder . Was particularly happy with execution speed (key factor for me). But after some thorough testing i actually discovered it was not so cool, since it produced non-realistic results.

My finding is that horizontal "compression" at the left and right edges of the cylinder is way too low and the rendered picture lacks impression of real cylinder (though vertical transformations are OK). In other words, horizontal compression is nearly linear, but it should not be.

Original picture:
Image
What i get is this:
Image
script:

Code: Select all

convert orig.png -background white -gravity south -splice 0x50 \
	          \( +clone -sparse-color barycentric '-50,0 black 600,0 white' \) \
	          \( +clone -function arcsin 0.5 \) \
	         \( -clone 1 -level 25%,75%	 \
	         -function polynomial -4,4,0 -gamma 2	+level 50%,0 \)	\
	        -delete 1 -swap 0,1  miff:- | \
	 composite - -virtual-pixel white  -displace 10x40  out.jpg
Note that rectangle width at left or right edge is almost the same as compared to rectangle width in middle. For realistic result this ratio of widths have to be more extreme. In this case ratio is about 1.08.

The result i wish is more like this:
Image
script:

Code: Select all

./cylinderize -m vertical -p 20 -w 80 orig.png out2.jpg
In this case widths ratio is about 1.85 and rises to infinity at the very edge. This picture is rendered by Fred's Cylinderize script. It is realistic, universal and produce correct results, but is about 7x slower than "cylindrical displacement" script, so i can't use it to achieve my goals :( . I guess i could increase it's performanceby by about 33% removing unnecessary for me shell scripting statements, but that's not engough.

My question is, is there a simple way to modify this "cylindrical displacement" script to apply more realistic horizontal compression? As we can see compression overall exists, but way too low, and i can't find a way to adjust it. Or does this script in essence lacks functionality to achieve this goal?

I am trying to change various params of the script, but with no success (i treat the script as black box and change params intuitevily only, since i do not understand fully what transformations are made). I figured out, that if i change 'arcsin 0.5' to, say, 'arcsin 0.1' i get impact to horizontal compression, but the opposite way i want! I get this:
Image

Code: Select all

convert orig.png -background white -gravity south -splice 0x50 \
	          \( +clone -sparse-color barycentric '-50,0 black 600,0 white' \) \
	          \( +clone -function arcsin 0.1 \) \
	         \( -clone 1 -level 25%,75%	 \
	         -function polynomial -4,4,0 -gamma 2	+level 50%,0 \)	\
	        -delete 1 -swap 0,1  miff:- | \
	 composite - -virtual-pixel white  -displace 10x40  out.jpg
Hor. compression degree is higher in the middle of the cylinder thatn at edges now.


I looked for other ways to solve problem, like adjusting horizontal compression in extra transformation step, like here:
http://www.imagemagick.org/Usage/mappin ... rtion_maps
Image

But performance was even poorer than that of Cylinderize script.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Cylindrical displacement problem

Post by anthony »

There is certainly some invalid stretching of the results. The horizontal displacement should not be stretching the image, but compressing it toward the edges. Probably goes to show that it pays to use a checker-board pattern for part of the testing.

I'll wait for Fred to comment however before looking at this more closely, seeing as he developed it.

NOTE: this technique was developed to use displacement (composition) mapping (relative), however it is possible to use a distortion mapping (-compose Distort) (using absolute Lookup coordinates) instead. That may make it easier to work things out.

I just never did get round to the re-write of the "Mapping' Examples page to make use of the new composition, as well as write up the other displacement mappings (lens effects). I have the main example saved, just not written up.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Cylindrical displacement problem

Post by fmw42 »

In my script, cylinderize, at the link below, the perspective effect is only a crude approximation, as this code is not a real 3D system. Both Anythony and I contributed to getting this to work. However, I am not sure what type of example Anthony has posted on his Examples. But I simply was not trying to make a real 3D perspective effect. The horizontal distortion is, I believe, a true vertically oriented cylinder distortion and is controlled in my script by the radius parameter. It is the vertical "tilt" distortion making the top and bottom curve that is only an approximation of a tilted perspective. The sides of the cylinder never change from true vertical.

If this is clear and you still feel there is an error in my script, perhaps you could explain the issue again for me. It is possible that something in IM has changed since the script was created that is causing some error.

Fred

By the way, this was one of the hardest script to write to make it efficient. Anthony had to create a whole new type of distortion displacement function. Otherwise, I would have had to use -fx (as I did in the beginning) and that was very slow. It is only thanks to Anthony for allowing the dramatic speed up.
pushparaj
Posts: 1
Joined: 2013-06-26T06:51:21-07:00
Authentication code: 6789

Re: Cylindrical displacement problem

Post by pushparaj »

Hi,
I have used this code,
convert orig.png -background white -gravity south -splice 0x50 \ \( +clone -sparse-color barycentric '-50,0 black 600,0 white' \) \ \( +clone -function arcsin 0.1 \) \ \( -clone 1 -level 25%,75% \ -function polynomial -4,4,0 -gamma 2 +level 50%,0 \) \ -delete 1 -swap 0,1 miff:- | \ composite - -virtual-pixel white -displace 10x40 out.jpg

But its not working.
so i have just test with code
convert orig.png -background white -gravity south -splice 0x50 \ \( +clone -sparse-color barycentric '-50,0 black 600,0 white' \) \ \( +clone -function arcsin 0.1 \) \ \( -clone 1 -level 25%,75% \ -function polynomial -4,4,0 -gamma 2 +level 50%,0 \) out.jpg

just removed the code \ -delete 1 -swap 0,1 miff:- | \ composite - -virtual-pixel white -displace 10x40
it's run and creating 4 images out-0.jpg,out-1.jpg,out-2.jpg,out-3.jpg

help me why its not running my side.

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

Re: Cylindrical displacement problem

Post by anthony »

For reference see
http://www.imagemagick.org/Usage/mappin ... e_cylinder

It is a annotated example.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Cylindrical displacement problem

Post by snibgo »

It's worth noting that Anthony's example can be combined into a single command. Windows 7 script:

Code: Select all

convert ^
  rose: -background black -gravity south -splice 0x8 ^
  ( +clone -sparse-color barycentric "0,0 black 69,0 white" ) ^
  ( +clone -function arcsin 0.5 ) ^
  ( -clone 1 -level 25%%,75%% ^
    -function polynomial -4,4,0 -gamma 2 ^
    +level 50%%,0 ) ^
  -delete 1 -swap 0,1  miff:- | ^
composite - -virtual-pixel black  -displace 17x7 rose_cylinder.png

Code: Select all

convert ^
  rose: -background black -gravity south -splice 0x8 ^
  ( +clone -sparse-color barycentric "0,0 black 69,0 white" ) ^
  ( +clone -function arcsin 0.5 ) ^
  ( -clone 1 -level 25%%,75%% ^
    -function polynomial -4,4,0 -gamma 2 ^
    +level 50%%,0 ) ^
  -delete 1 ^
  -virtual-pixel black  -compose displace -define compose:args=17x7 -composite ^
  rose_cylinder2.png
The results are visually identical, although a few pixels have slightly different values. I don't know why.
snibgo's IM pages: im.snibgo.com
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Cylindrical displacement problem

Post by snibgo »

I am two years too late, but the main problem for the OP is that his image orig.png is surrounded by a large transparent border. This (Windows 7 script) works fine:

Code: Select all

%IM%convert orig.png -trim +repage orig3.png
%IM%identify orig3.png
rem orig3.png is 347x195

%IM%convert orig3.png -background white -gravity south -splice 0x50 ^
  -write x.png ^
  -set colorspace RGB ^
  ( +clone -sparse-color barycentric "0,0 black 346,0 white" ) ^
  ( +clone -function arcsin 0.5 ) ^
  ( -clone 1 -level 25%%,75%% ^
    -function polynomial -4,4,0 -gamma 2 ^
    +level 50%%,0 ) ^
    -delete 1 -swap 0,1  miff:- | ^
%IM%composite - -virtual-pixel white  -displace 86.75x49  out.jpg
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Cylindrical displacement problem

Post by fmw42 »

I am two years too late, but the main problem for the OP is that his image orig.png is surrounded by a large transparent border. This (Windows 7 script) works fine:
I suspect that Anthony's code does not expect the large transparent area (nor does my cylinderize script). If the transparent border is trimmed off first, does it work to give the proper effect along the sides?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Cylindrical displacement problem

Post by snibgo »

Yes, the result reduces the width of each rectangle the further it is from the centre.

The trimmed image is 347x195, so I manually set the sparse-color 2nd coord to 346, and the final x-displacement to 347/4 = 86.75.

Image

Compared to the source image, the centre portion has become squeezed. The rectangles have become squares. Doubtless this is easily fixable.
snibgo's IM pages: im.snibgo.com
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Cylindrical displacement problem

Post by fmw42 »

Not sure why you are getting squares and not rectangles. Try increasing the radius and see if that converts the squares to rectangles. Also you might scale the width of the cropped image to achieve the same results for a given radius.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: Cylindrical displacement problem

Post by anthony »

Just remember this is a isometric transformation. Cylinders look very different when viewed in perspective, due to the different 'depths' which can be very hard to calculate with 2-Dementional mathematical algorithms.

If you want true cylindrical (3-D) perspective views of objects, rather thane fast 2-D transforms, you will need to go to Ray Tracing, However then you also need to deal with backgrounds, lighting, object intersections, etc, etc, etc. Even just the object defining can be quite a problem when raytracers are used.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
vinod
Posts: 20
Joined: 2016-03-10T13:50:05-07:00
Authentication code: 1151

Re: Cylindrical displacement problem

Post by vinod »

hi i have windows i have xamp to run php script. that i work in php
i have imagemagic 6.9

i run following command

$cmd1 =
"convert orig.png -background white -gravity south -splice 0x50 ( +clone -sparse-color barycentric '-50,0 black 600,0 white' ) ( +clone -function arcsin 0.5 ) ( -clone 1 -level 25%,75% -function polynomial -4,4,0 -gamma 2 +level 50%,0 ) -delete 1 -swap 0,1 miff:- | composite - -virtual-pixel white -displace 10x40 out.jpg ";
shell_exec($cmd1);

and i get very strange output i need o/p like above or cylinder shape please help me...
Post Reply