This is a simple utility to transform an area on a flat plane to a bird's eye perspective. For something automatic see https://github.com/mzucker/page_dewarp. This just removes the perspective distortion and displays the warped results based on the 4 quadrants chosen.
Might want to look at what this is doing as well: http://tpgit.github.io/Leptonica/dewarp_8c.html or https://github.com/scantailor/scantailor