Best way to apply Perspective distorted image as a layer?

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
Tagomago
Posts: 36
Joined: 2011-02-10T09:55:33-07:00
Authentication code: 8675308

Best way to apply Perspective distorted image as a layer?

Post by Tagomago »

I looked for this question in old posts but didn't come up with quite the same issue.

I know this is a common question, though - what is the best way to perspective distort (or for that matter, apply any complex distortion) an image and then add it as a layer into another image?

I've got perspective distortion working :

Code: Select all

convert trial.jpg -matte -virtual-pixel transparent -distort Perspective "0,0 335,945  0,1127 292,1499  1539,1127 1208,1229  1539,0 1208,399" trail_pers.png
But looking at the Layers explanation page http://www.imagemagick.org/Usage/layers/ I'm not sure how to first distort the image then add it into the second image all in one command.

If I distort the image as a seperate step, then I would imagine it would be difficult to grabbed only the warped portion (since parts will be blank from distort) and carry that over into a seperate image.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Best way to apply Perspective distorted image as a layer

Post by fmw42 »

try with +distort and -layers merge

convert backgroundimage \( trial.jpg -matte -virtual-pixel transparent +distort Perspective "0,0 335,945 0,1127 292,1499 1539,1127 1208,1229 1539,0 1208,399" \) -compose over -layers merge background_trial.png

this assumes you want to overlay the perspective on a background image and your control points are picked appropriately from the corresponding locations in both images.


see although a complex example http://www.imagemagick.org/Usage/distorts/#box3d


Here is example code that was used recently to create the image at http://www.imagemagick.org/script/index.php (though the image was flipped horizontally afterwards in this case)

picture="mona_lisa.png"
# get picture corners clockwise from top left
width=`convert $picture -ping -format "%[fx:w-1]" info:`
height=`convert $picture -ping -format "%[fx:h-1]" info:`
p1=0,0
p2=$width,0
p3=$width,$height
p4=0,$height
# measured desired corresponding wizard drawing points
w1=298,488
w2=431,472
w3=497,590
w4=370,616
# combine control points
points="$p1 $w1 $p2 $w2 $p3 $w3 $p4 $w4"
echo $points
# process picture in perspective and overlay on wizard
convert wizard_template.png \
\( $picture -channel rgba -alpha set -virtual-pixel transparent \
+distort perspective "$points" \) \
-compose over -layers merge wizard_mona_lisa.png


If this is not what you need, then please post links to your images and explain the result you want.
Last edited by fmw42 on 2011-03-01T10:59:40-07:00, edited 1 time in total.
Tagomago
Posts: 36
Joined: 2011-02-10T09:55:33-07:00
Authentication code: 8675308

Re: Best way to apply Perspective distorted image as a layer

Post by Tagomago »

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

Re: Best way to apply Perspective distorted image as a layer

Post by anthony »

The ability to handle virtual canvases was a major requirement for me when I designed distort. Especially when distorting images (like affine projections) can easily generate negative offsets. Without virtual canvas handling you will loose a lot of data.

Basically -distort distorts images using the original image as the 'viewport' boundaries for the distortion.
Input can be virtual, though generally isn't, as such the distorted image will be cropped but the original images bounds.

Some exceptions to this are the 'Polar' distortions as the image size really needs to change to hold any 'practical' result.
But the new bounds are chosen to be a normal non-layered (no virtual offset) image

+distort however attempts to calculate virtual canvas viewport bounds according to the distortion so as to try and hold ALL the results. That includes some of the fuzzy edge pixels caused by the filter (though that is only a rough adjustment).

This is not always easy as for some distortions as it not only needs the 'reverse map' function used for the actual distort, but alos a forward map function, to find the limits of the distortion. Because of this some distorts can calculate appropriate bounds for +distort, For example "Shepards Distortion".

However you can override BOTH techniques by specifying a expert 'viewport' setting you want to use to 'view' the distorted space.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
Post Reply