Correcting for perspective distortion

General discussion about software packages and releases, new software you've found, and threads by programmers and script writers.

Moderator: peterZ

Post Reply
tkr
Posts: 35
Joined: 29 Jan 2012, 21:53
Number of books owned: 0

Correcting for perspective distortion

Post by tkr »

For no particular reason, I've become interested in the underlying computer vision concepts that are used in book scanning.
Using the code that OG200 posted, as a starting point, I've actually managed to get a working setup of Python with OpenCV2.
(had to jump through a few hoops to do that, and intend to post what I found).

I can do things like calibration using a chessboard to find intrinsic parameters, manipulate images etc. The one area where I'm having difficulty is
with correcting for perspective distortion.

The process as I roughly understand it:
InQuad : Find the coordinates of the 4 outer corners of the chessboard being used for calibration
OutQuad : Supply the coordinates of where you think the 4 outer corners of the chessboard should be mapped to (to show up without perspective distortion)
transform <== getPerspectiveTransform(inQuad, outQuad)
transformed_Image <== warpPerspective(transform, image-to-work-on)

I'm having trouble understanding how to calculate the OutQuad coordinates and would appreciate some help/an example in this area.

Thanks
TKR
spamsickle
Posts: 596
Joined: 06 Jun 2009, 23:57

Re: Correcting for perspective distortion

Post by spamsickle »

Coincidentally, I also started playing with OpenCV in the past couple of weeks, though with C++ rather than Python. I hope you will post the details of how you overcame the problems you encountered, and I'll try to find that OG200 post you referenced as well.

Unless I'm missing something, if you have a square checkerboard for calibration, your output quad can be any square. Since all squares are alike except for scaling, I'd just pick one that has the rough dimensions you want -- if your input images are 2000x3000 pixels, and your calibration checkerboard covers 3/4 of the smaller dimension, an output quad of {(0,0) (0,1500) (1500, 1500) and (1500, 0)} should work, or you might blow it up by dimensioning it as 2000 pixels on a side instead.

Translating this into a perspective correction for a rectangular page rather than a square calibration checkerboard is a little trickier, but not much.
tkr
Posts: 35
Joined: 29 Jan 2012, 21:53
Number of books owned: 0

Re: Correcting for perspective distortion

Post by tkr »

I too originally started with C/C++ - I was able to access my webcam, and get some simple image processing programs to run, but when I started to do things like 'findHomography' or other slightly more complicated programs and they crashed without warning, it proved to be too frustrating to debug. Making the switch to Python was a good step for me.

Here is what I had to do:
a) http://www.enthought.com/, go here and download the latest version of EPDfree - includes Python and all the necessary libs like numpy scipy etc required to get opencv running.
b) Install opencv from here http://sourceforge.net/projects/opencvlibrary/files/
c) Find the following files cv.py and cv2.pyd and copy them to your working folder
d) Should be able to run any python script that uses OpenCV library.
tkr
Posts: 35
Joined: 29 Jan 2012, 21:53
Number of books owned: 0

Re: Correcting for perspective distortion

Post by tkr »

Spamsickle,
Thanks for the response re. outQuad.

Just need confirmation -
- If I have a rectangular chessboard (instead of square) then just need to make sure that the aspect ratio is preserved, whatever the actual L and W dimensions.
- If I have to compensate for aspect ratio, then would elongate the side that has the larger DPI (so as to avoid losing detail.

TKR
spamsickle
Posts: 596
Joined: 06 Jun 2009, 23:57

Re: Correcting for perspective distortion

Post by spamsickle »

- If I have a rectangular chessboard (instead of square) then just need to make sure that the aspect ratio is preserved, whatever the actual L and W dimensions.
I believe that's correct.
- If I have to compensate for aspect ratio, then would elongate the side that has the larger DPI (so as to avoid losing detail.
To me DPI is only a concern when one is outputing to print, and I do all my image processing in image space. I would expect for perspective correction calculations, the largest edge of the quadrilateral I'm converting to a rectangle would have preserved the most information when going from real world to image. Since the image plane of your camera is probably very close to parallel to the plane of the page, I would probably begin by using the size of that side in pixels as the L dimension, and I'd stop there if the output quality was acceptable. 99 times out of 100, I expect it would be. If not, I would gradually reduce the L dimension (preserving the original L and W ratio) until the quality was acceptable.
Post Reply