I had that same problem and came up with this quick solution.  It uses the 
command line tools - I've had the intent to convert it to function calls using 
the dll's, just not the time.  It should be (he says confidently) 
straightforward to take the command lines and transform them into function 
calls once you know what you want to do.

The general gist is to first extend the original image, if necessary.  
Sometimes if the quadrilateral is very skewed and fills most of the original 
image, the extents of the original image will not be large enough to accomodate 
the resulting transformation and you will loose some of the picture.  After 
that, apply the '-distort Perspective' option of convert to the image.  Then 
crop the resulting square.

The command lines look something like:

convert orig_filename -extent  newwidth x newheight  filename_extended

convert filename_extended -matte -virtual-pixel transparent -distort 
Perspective  x1_old,y1_old x1_new,y1_new  x2_old,y2_old x2_new,y2 _new  
x3_old,y3_old x3_new,y3_new  x4_old,y4_old x4_new,y4_new filename_stretched

convert filename_stretched -crop cropwidth x cropheight +xoffset +yoffset 
final_output

A good primer on perspective distortions is at
http://www.imagemagick.org/Usage/distorts/  

With this method you don't actually need to draw the polygon; all you need is 
to know the coordinates.

Mike


-----Original Message-----
From: [email protected]
[mailto:[email protected]]on Behalf Of Dave Hoover
Sent: Monday, January 26, 2009 7:25 AM
To: [email protected]
Subject: [magick-users] Stretch Quadrilateral into Square


I have an photo that has a square in it, though the square is skewed
because the photo was taken at a slight angle.  I can get the
coordinates of the corners of the square.  I'm wondering if there is a
way with ImageMagick to clip the quadrilateral, extract it from the
original image, and stretch it into a square.

I've been playing with RMagick and used Magick::RVG::ClipPath to draw
the skewed square (using polygon()) on the image.  But I can't figure
out how to take the next step and actually "crop" it and stretch it.

Thanks,
Dave Hoover
_______________________________________________
Magick-users mailing list
[email protected]
http://studio.imagemagick.org/mailman/listinfo/magick-users

_______________________________________________
Magick-users mailing list
[email protected]
http://studio.imagemagick.org/mailman/listinfo/magick-users

Reply via email to