Page 1 of 1

Help with displacement map + Perl syntax

Posted: 2007-07-09T21:57:16-07:00
by Ryland
I'm working on an application that will make an image of a soft drink can, and I want to distort the graphics to simulate 3D perspective (rounded top and bottom). I've got a displacement filter that works in Photoshop, but not in ImageMagick, and I suspect it's because I don't know the correct PerlMagick syntax. It should produce a relatively can-shaped distortion, but for some reason it's skewing on the X-axis:

Image

I know there's a way to define how much to distort on each axis on the command line, and I'm pretty sure I should be setting the X axis to zero, but I can't figure out what the Perl syntax is. Can someone give me an example? Thanks in advance.

Re: Help with displacement map + Perl syntax

Posted: 2007-07-09T22:04:18-07:00
by Ryland
Oh, I forgot: this is the Perl code I'm using.

Code: Select all

use Image::Magick;

$checker = Image::Magick->new;
$checker->Set(size=>"200x350");
$checker->Read("checker.png");

$dmap = Image::Magick->new;
$dmap->Set(size=>"200x350");
$dmap->Read("displacementmap.png");

$base = Image::Magick->new;
$base->Set(size=>"200x350");
$base->Read("xc:#FFFFFF");

$check->Composite(image=>$dmap,compose=>'Displace',gravity=>'Center');

$base->Composite(image=>$checker,compose=>'Over',gravity=>'Center');

$base->Write("png:checker_distort.png");

undef $base;
undef $checker;
undef $dmap;

Re: Help with displacement map + Perl syntax

Posted: 2007-07-10T09:57:04-07:00
by Ryland
Sorry, I just realized there's a whole other forum dedicated to PerlMagick, I'll take this over there.

Re: Help with displacement map + Perl syntax

Posted: 2007-07-11T00:34:52-07:00
by anthony
I would be interested to know how you generated that displacement map :-)

Re: Help with displacement map + Perl syntax

Posted: 2007-07-14T18:52:55-07:00
by Ryland
anthony wrote:I would be interested to know how you generated that displacement map :-)
I made it in Photoshop, but it wouldn't be difficult to do in ImageMagick. First I made a reflected horizontal gradient, because I wanted more displacement toward the vertical center of the image than at the right and left edges. Then I made a normal vertical gradient, and inverted the first gradient using the second gradient as a mask, so that the displacement would go up at the top of the image and down at the bottom.

Image

Re: Help with displacement map + Perl syntax

Posted: 2007-07-15T17:00:21-07:00
by anthony
Very tricky...

Re: Help with displacement map + Perl syntax

Posted: 2007-07-15T17:23:48-07:00
by anthony
IM solution (command line)...

Code: Select all

   convert rose: -fx 'cos(pi*(i/w-.5))' -evaluate multiply .5 \
           \( +clone -negate \) \
           \( -size 70x46 gradient: -flip \) -composite \
           displace_cylinder.jpg

   composite  displace_cylinder.jpg  rose: \
              -background black -virtual-pixel background \
              -displace 0x10  cylindrical rose:
Remember grey = no displacement, not white!

Also as the displacement values represents the 'destination' offset, rather than the source offset, (see http://www.imagemagick.org/Usage/compose/#displace ) the 'cosine()' function in the above is distorted by the linear inversion. Making the curve become rather straight toward the edges.

Re: Help with displacement map + Perl syntax

Posted: 2007-07-16T03:09:29-07:00
by Ryland
It may be worthwhile to point out that only a specific region of the displacement map is going to get used, corresponding to the label area of the can.

Image

Only the blue region will get distorted. (Actually, a separate graphic the size of the blue region will get distorted, and then composited onto the base can image.)

As I preview this, I realize it would be a better idea to invert the displacement map so that the left and right edges are distorted toward the vertical center, instead of the vertical center being curved up and down - downsampling the edges instead of upsampling the center.

Re: Help with displacement map + Perl syntax

Posted: 2007-07-16T04:06:44-07:00
by anthony
I agree. and that is what my revised map did. It made the center line grey, meaning no change.