How to unwarp a page gutter?

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
nicolai.rostov
Posts: 18
Joined: 2015-10-06T10:42:17-07:00
Authentication code: 1151

How to unwarp a page gutter?

Post by nicolai.rostov »

I'd like to straighten out photos taken from book pages. For example, page (image #1) is mostly, but not entirely, warped toward the book gutter. The difficulty is that each line bottom-up is more deeply warped than the line right below it (image #2), and the shrink factor increases left to right, so that a perspective correction will not do.

I was trying to distort it using the shepards method but the resulting image (#3) was coming out even more distorted than the original.

Code: Select all

convert <input> -distort Shepards '15,45 15,45 ...' <output>
I need another approach. What would be a more efficient way to achieve this?

Thank you.


image 1
Image

image 3
Image

image 3
Image
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to unwarp a page gutter?

Post by snibgo »

There is displacement in both directions. The letters in the gutter are displaced down and to the left. The right side of the page approximates to a cylinder, photographed by a camera. Fred and I have pages on transforming cylinders to planes. My page considers only parallel projection, where the camera is at infinity, so would need adjustment because your camera is clearly closer. Modulating the y-displacement by the height above the bottom would be good first-try.
snibgo's IM pages: im.snibgo.com
nicolai.rostov
Posts: 18
Joined: 2015-10-06T10:42:17-07:00
Authentication code: 1151

Re: How to unwarp a page gutter?

Post by nicolai.rostov »

Okay. I'm trying to understand how Fred's pinbarrel works but am clueless as to how to calculate the right radial distortion/correction coefficients.

There is an example on Fred's homepage to make a square into an image which is distorted in a way that looks like my page above. I'd like to build from that into a solution to my problem. Here is Fred's code:

Code: Select all

pinbarrel "0.5,0.5,0,0,0" -c 64,64 infile outfile
Image

How can I determine the argument to achive the opposite effect? How does one determine the radial coefficients and center point coordinates?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: How to unwarp a page gutter?

Post by fmw42 »

IM has this implemented, though I do not think it will be relevant to what you want. See http://www.imagemagick.org/Usage/distorts/#barrel. But there is no way to compute the exact values you would need, as far as I know.
nicolai.rostov
Posts: 18
Joined: 2015-10-06T10:42:17-07:00
Authentication code: 1151

Re: How to unwarp a page gutter?

Post by nicolai.rostov »

Thanks. Is it possible to apply an (inverted) IM's barrel distortion method diagonally? All examples on that page are distorted around a vertical or horizontal axis in the middle of the image.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: How to unwarp a page gutter?

Post by snibgo »

Barrel (and pincushion) distortion move pixels towards (or away) from a centre. You don't have that distortion. We know that because the top edge has a curve, but none of the others do.

Open a book, and look at how the paper is curved near the gutter. It is curved in one direction only, like part of a cylinder.
snibgo's IM pages: im.snibgo.com
nicolai.rostov
Posts: 18
Joined: 2015-10-06T10:42:17-07:00
Authentication code: 1151

Re: How to unwarp a page gutter?

Post by nicolai.rostov »

I see. But is there any way to apply a pincushion distortion only to the right side of an image, though? Or maybe I could rotate the original so that the current distortion axis (white-gray boundary above) goes to the center of the image, and then apply a pincushion distortion to that, crop the right-hand side off, lay it over the original rotated image, and rotate everything back to the original position.

At any rate how do people go about determining the multiple radial coefficients and center point coordinates of a pincushion distortion? It can't be by trial and error only. I think I understand how the Shepards method works but I am clueless regarding barrel and pincushion. A small change to any of those coefficients results in a completely different image.
Post Reply