Image processing challenge: automatic perspective correction

I have a challenge to propose for the skilled programmers out there: implement an automatic perspective correction tool.

Actually I am in the process of porting Darktable’s perspective correction tool into PhotoFlow, and I thought it would be great to add some sort of auto-guessing of the optimal perspective correction transform, based on the pattern of straight lines found in the image.

Now I’m perfectly aware that this might be much easier to say than to implement, but who knows? Maybe some skilled and experienced programmers in this community could come up with a neat solution…

Darktable’s perspective correction works by defining the four corners of a quadrilater that should be transformed into a rectangle in the corrected image. Therefore the goal would be to automatically “guess” the most appropriate corners for a given image.

Of course this could also be backported into Darktable, if the developers are interested!

2 Likes

AFAIK huggin already contains something like that. Might be a fun hack, but I do somewhat doubt that it will be reliable enough to actually be useful given that the manual correction is not a lot of work either.

1 Like

That’s right if you do it for one image. But if you have to do it for 100 images it could be a big improvement.
A similar tool would be cool to rotate an image to make the horizon straight.

1 Like

I googled a little bit and found:

  1. Automatic perspective correction with OpenCV on Stackoverflow:
    http://stackoverflow.com/questions/22519545/automatic-perspective-correction-opencv
  2. Hugin has a straighten button to optimises the roll and pitch of the input images:
    Hugin Fast Preview window - PanoTools.org Wiki
2 Likes

Thanks, I will look into the OpenCV option in more detail…

Concerning hugin, it adjusts roll and pitch, but I think that for a complete perspective correction one also needs to correct the yaw, right?
This is why one needs to start from a generic quadrangle, and transform it into a rectangle (if I understood the underlying math well enough ;-)).

I started another search I found two nice documents about horizon detection:

http://www.researchgate.net/publication/253820856_Horizon_detection_based_on_sky-color_and_edge_features

I think the second one looks better.

Ever so slightly off topic: I have actually done something like this for cropping: GitHub - jwagner/smartcrop.js: Content aware image cropping and the algorithm is really simple too. :smile:

1 Like

@Jonas_Wagner Very interesting tool!

Based on all the suggestions and examples I’ve received so far, I’ve started to figure out a possible scheme:

  • Detect edges (with sobel, difference of gaussians, g’mic “gradient norm” or whatever works best)
  • Split the edge image into tiles, and for each tile search for straight lines
  • Merge straight lines across tiles, and rank lines in terms of number of crossed tiles
  • Build all possible quadrilateral regions with the detected lines, and rank the regions in terms of surface, number of edge pixels along the borders of the region, similarity with a rectangle (assuming the distortion we want to correct is not “too extreme”), and maybe something more

Assuming that the object we want to straighten occupies a significant fraction of the frame, and that it is not extremely distorted, we are looking for a quadrilateral region with one corner in each image quadrant and with sides that are approximately perpendicular.

Still rather vague, but maybe a good starting point for further discussions.

http://www.fmwconcepts.com/imagemagick/unperspective/index.php

Just in case anyone didn’t see it yet: A new module for automatic perspective correction | darktable

1 Like

Indeed that matches perfectly what I was proposing… really impressive work!