I wrote some software recently that uses OpenCV to act as a sort of “portable photocopier”. The idea is to be able to take photos on a portable camera (e.g., like the one on a mobile phone), and then have those photos automatically transformed into something that looks like a photocopy: high contrast, black and white, flat and free of perspective distortion.
The first pass at this problem is a python script on GitHub.
The strategy is pretty simple. We have some input image:
First, we take the image and convert it to greyscale. Then we try to find the position of the sheet of paper as follows. Applying an Otsu threshold to the input image, we hope that the threshold picks out the outlines of the paper – it should, provided the background is suffiently darker and the lighting is even enough.
Then we use a feature detector, and look for features that are rectangles:
Selecting the biggest one, we find what we think are the vertices of the paper. Then we do a guess to figure out which of the corners is which, and then use a perspective transform to dewarp the image and crop to the paper size.
Finally, now that we have a greyscale image that consists of just the paper, we apply an adaptive threshold filter to try to pick out the writing from the background.
The script still needs some work. It doesn’t currently try to determine the aspect ratio of the paper, and instead assumes that the paper is US Letter size. The main problem, though, is that the final step is very crude and produces rather poor-quality output.