tags:

views:

66

answers:

1

If I want to detect perspective distorted objects (e.g rectangles) and calculate the correction transformation, what would be a good method?

For example, I have a lot of photos of papers lying on a flat surface (the photos are shot from an angle), and I want to correct the perspective and crop them.

I am thinking of using the OpenCv Python bindings to whip something up. I thought it would be a "standard problem" with lots of examples, but I failed to find any relevant info. Clues?

EDIT: I should add that I know it can be done by using some Web services, but I see this as a chance of doing something useful while learning OpenCv :)

A: 

You have to specify exactly what you mean with detecting the distortion. I assume you want to detect the corners of such a paper (or your table/flat surface) and make it axis-parallel to your display. This can be done using cvFindHomography and cvWarpPerspective. I have written some example code a while ago for python, note that those are still the old bindings (shouldn't take long to update to opencv 2.0 though): http://ioctl.eu/blog/2009/05/13/opencv_homography

The other thing you obviously have to do is detecting the corners. In my sample code, the user is required to click four corners using the mouse. In your case, it is hard to tell because I need an example. OpenCV offers several means to detect corners (see Harris), also in the examples there should be code on how to find rectangles. If you could be more specific, I might be of greater assistence.

UPDATE This might be interesting for you as well: I've just heard a talk on manipulating perspective distortion in images (to e.g. increase the foreshortening). They require heavy user interaction, but optimize some grid with some constrains to maintain a smooth warping. Here is the acm link: http://portal.acm.org/citation.cfm?id=1778765.1778864 Maybe you can draw some ideas from that paper and the mentioned references, however I haven't read it myself (yet).

zerm
Thank you for your answer. I did not want to specify exactly how, because did not want to narrow myself, but wanted some input from others :) Detecting corners is one way I thought about, but I worry that it is not robust enough. For example, a lot of pictures have one ore more of the corner outside the bounds of the picture.
Krumelur
I think that zerm's answer covers a good starting point so I will not put another answer but to expand on his approach.The correct approach (most naturally) would be to find the corner. To find corners, you can use available corner detectors. However, you can also find the vanishing point of your image.Your question is ambiguous in what type of shape are these papers. Or what type of background or what type of colors. This is a computer vision problem and it is best that you post some images.
Dat Chu
@Dat Chu: Good point about posting samples. Anyway this was more of a "for fun" exercise, and I need some starters. I think I will go for detecting vanishing points. Actually, the text is probably a lot more detectable than the edges themselves.
Krumelur