[magick-users] Stretch Quadrilateral into Square
Mike Bruno
mbruno at bete.com
Mon Jan 26 05:50:27 PST 2009
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: magick-users-bounces at imagemagick.org
[mailto:magick-users-bounces at imagemagick.org]On Behalf Of Dave Hoover
Sent: Monday, January 26, 2009 7:25 AM
To: magick-users at imagemagick.org
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
Magick-users at imagemagick.org
http://studio.imagemagick.org/mailman/listinfo/magick-users
More information about the Magick-users
mailing list