MAKE DOCUMENT SCANNER USING PYTHON AND OPENCV

Hi This Story begins with sharing of Notes given by our Math's Professor. One of my friend took his phone, scanned the image and...


Hi

This Story begins with sharing of Notes given by our Math's Professor. One of my friend took his phone, scanned the image and shared it with everyone.
I came to know about many apps like Genius Scan, CamScanner etc which allow us to scan our documents and save them as PDF. How about having one of YOUR OWN! 

As the title suggests, we are going to use Python 2.7 and OpenCV 2.4 for this. 

Long story short, we have to do the following tasks:

  • Capture image
  • Resize it
  • Detect Edges
  • Find Contour
  • Do a Perspective Transformation
  • Threshold the image and Save as Image / PDF

To Jump right into code, CLICK HERE .
(This will redirect you to my github page)

CAPTURE IMAGE

Original Image Captured by Camera
Capture/Choose the Image that you want to scan as document. I am going to choose an Image manually for the sake of simplicity.

RESIZE IT

The image that you Shot using your camera might be of very high resolution. So, in order to make it fit in a proper size of document, we need to resize it.
You will do this with cv2.resize() function.


Resized Image

DETECT EDGES

Once you have resized the image, we are good to go with the real tasks. We will detect all the edges in our image with Canny edge detection method.
You will do this with cv2.Canny() . Visit the official documentation site to have more information.


Edge Detection

FIND CONTOURS

Now, we will try to find contour in the image. We will do this by cv2.findContours() function.
In order to get the required contour, we will choose the biggest contour from the rest of the contour array.


Selecting the Largest Contour

It is also necessary to check whether the contour that you are going to choose is of four corners or not! This will help you to remove any of the false/undesired contour present in your image.


PERSPECTIVE TRANSFORMATION

Now that you have your desired contour, it is important to make it look from the top as the image which you clicked could have been taken from a different angle. Hence, we will do a Perspective Transformation on the selected contour. This will be perform Perspective transformation byusing cv2.getPerspectiveTransform() function and then  cv2.warpPerspective() function to this 3 * 3 matrix.


Document after Perspective Transformation

THRESHOLD THE IMAGE AND SAVE

We are almost done with creating the scanned document. For a scanned touch and feel, we will threshold the image. Once you are done with it, save it as image or PDF. For saving it as an PDF, First save your final image as JPG. Then use:
im = Image.open() and im.save() function to save it as PDF.



Final Scanned Image( Left: Inverse Threshold , Right: Threshold )



















Personally, I like the Image in Right :)

Congrats!!
Now you know how you can create your own Document Scanner in just few minutes. This was as easy as a pie!

To view full source code, visit my Github page .

Tell me which one of those two you like the most in the comments. Also, I would welcome any suggestions or change that i can do in this. Feel free to comment :)


Happy Coding!

You Might Also Like

4 comments

  1. Hi. We tried your code but it gave us a white picture as an output. Please help

    ReplyDelete
    Replies
    1. It could be giving white image because the final image is an threshold image. Try adjusting those values because my script doesn't handles it automatically. I will try to add this asap. Thanks for trying it :)

      Delete

Total Pageviews

Flickr Images