by pietrofmaggi on 8/18/16, 1:45 PM with 70 comments
by kazinator on 8/18/16, 9:38 PM
Here is my very quick and dirty manual job of the same example page:
http://www.kylheku.com/~kaz/dewarp.png
Literally less than five minutes.
First I cropped the image. Then duplicated the layer. Blurred the top layer (Gaussian, 50 radius). Then flipped to Divide mode and merged the visible layers. This leveled the lightness quite well, almost completely eliminating the shadow over he right side of the page and all other lighting differences. There is a hint of the edge of the shadow still present because it is such a sharp contrast; but that can be eliminated in an adjustment of the intensity curves. In such cases it may be helpful to experiment with smaller blur radii, too.
I then did a perspective transform in the lateral direction, squeezing the left side top-bottom and expanding the right, resulting in the warp now being approximately horizontal. (The perspective transform is not just for adding a perspective effect; it is also useful reversing perspective!)
Finally, I used the Curve Bend (with its horrible interactive interface and awful preview) to warp in a compensating way. Basically, the idea is to draw an upper and lower curve which is the opposite of the curve on the page. I made two attempts, keeping the results of the second.
If the preview of this tool wasn't a ridiculous, inscrutable thumbnail, it would be possible to do an excellent job in one attempt, probably close to perfect.
Because the page is evenly light thanks to the divide-by-blurred layer trick, it will nicely threshold to black and white, or a narrow grayscale range.
by cooper12 on 8/18/16, 5:29 PM
I wish he went into more details on the steps taken after dewarping. You can tweak the image levels to get good contrast, but surprisingly there aren't any shadows from underleveling or loss of detail from overleveling. I wonder if the author ran OCR on the scans after, and speaking of OCR, IIRC Leptonica is one of the dependencies of Tesseract so it must do some similar pre-processing.
Edit: reading more carefully, he mentions that he used adaptive thresholding from OpenCV.
by niftich on 8/18/16, 5:27 PM
This code had the same idea, and is open-source!
by troymc on 8/18/16, 7:55 PM
by peterjmag on 8/18/16, 10:00 PM
Gotta admire this guy's resourcefulness—and patience. If I were a professor, I'd probably just reject the assignment outright if a student sent me a bunch of photos from their smartphone in lieu of a PDF or a "proper" scan. :)
by mzucker on 8/18/16, 7:35 PM
by anilgulecha on 8/19/16, 5:58 AM
Most phone cameras these days have good resolutions, and you could technically take a 6x4 photo, divvy it to 3x3 grid and take close up photos, and have smart algorithms interpolate the pixels to form a single image with high res. I'd even bet you'd results equal to or better than a flat bed scanner.
For better us, just open the camera preview and slowly pan over the image.
Has someone tried something like this? With FOSS apps like mosaic, hdr tools and imagemagick, it should be possible. I'm guessing opencv would be needed for interpolation and noise removal..
by Syzygies on 8/18/16, 5:00 PM
by Ciantic on 8/18/16, 7:14 PM
by jgable on 8/18/16, 7:18 PM
by amelius on 8/19/16, 8:35 AM
by renlo on 8/19/16, 1:12 AM
by petters on 8/19/16, 8:33 AM
Those are small optmization problems. These types of problems are solved in computer vision for hundreds of thousands of variables. His problem can be solved in real-time, not tens of seconds.
by paul_milovanov on 8/19/16, 6:04 AM
by voltagex_ on 8/19/16, 3:17 AM
by saynsedit on 8/18/16, 8:00 PM
by artursapek on 8/18/16, 7:30 PM
by nullcipher on 8/18/16, 10:57 PM
by anowell on 8/18/16, 10:53 PM