Automatically correcting trapezoidal distortion

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
aporthog
Posts: 37
Joined: 2012-05-30T08:24:46-07:00
Authentication code: 13

Automatically correcting trapezoidal distortion

Post by aporthog »

I am about to write a program to automatically detect and correct trapezoidal distortion of book pages and thought I'd run it by you guys to see if you had any suggestions to make the task easier or faster. I've had good luck in a test program I made using certain assumptions that i can't make for more generic cases. Now i want to make it more robust and generic so my plan is to detect the 4 white corners of the page before any deskew, and then undistort them to make the left and right pages edges equal sizes. I know how to do this. So my questions are:

1. I will use txt:- info to loop through 4 approximate parts of the image until I locate the 4 white corners. The cropping you see in the example and the width of the black areas are approximately the same for every single image so no need to go hunting through the entire image for the corners, only need to root around four general areas. This is a time consuming operation and rather complicated to write. I can make certain shortcuts by reducing the image dimensions significantly but can't reduce too far or risk losing precision. Is there a clever or faster way to locate these corners? I'm tempted to use morphology somehow but I'm not sure how the slight distortion of the corners could throw that off - they're not exactly right angles.

2. Do you think it's better to lengthen the shorter side to match the longer and introduce virtual pixels or reduce the longer side and eliminate pixels?

3. Is there an automatic detrapezoidify command in ImageMagick? Hey, it can't hurt to ask! :)

Image

Full-size image: http://ucblibrary4.berkeley.edu/~apollo ... torted.jpg

Thanks all!
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Automatically correcting trapezoidal distortion

Post by fmw42 »

If on Linux or Mac (or Windows with Cygwin), try my script, unperspective, at the link below. Otherwise, you can use -distort perspective but you need to pick control points manually.
aporthog
Posts: 37
Joined: 2012-05-30T08:24:46-07:00
Authentication code: 13

Re: Automatically correcting trapezoidal distortion

Post by aporthog »

I will try and sort through that. We're on Windows and Solaris but the Solaris IM is too old and we haven't been able to compile a new version. I'll try and find the salient points and convert them to perl and add them to my module. I'm trying to understand the idea of finding the peaks and the whole conversion between cartesian and polar. If I can get a handle on what's going on there I should be able to handle the rest myself.

Thanks!
Post Reply