Home » Source Code » An image into a document scanner using python

## Description

You see, scanning a document using your smartphone can be broken down into three simple steps:

• Step 1: Detect edges.
• Step 2: Use the edges in the image to find the contour (outline) representing the piece of paper being scanned.
• Step 3: Apply a perspective transform to obtain the top-down view of the document.
• Lines 2-7 handle importing the necessary Python packages that we’ll need.

We’ll start by importing our four_point_transform  function which I discussed last week.

We’ll also be using the imutils  module, which contains convenience functions for resizing, rotating, and cropping images. You can read more about imutils  in my basic image manipulations post.

Next up, let’s import the threshold_adaptive  function from scikit-image. This function will help us obtain the “black and white” feel to our scanned image.

Lastly, we’ll use NumPy for numerical processing, argparse  for parsing command line arguments, and cv2  for our OpenCV bindings.

Lines 10-13 handle parsing our command line arguments. We’ll need only a single switch image, --image , which is the path to the image that contains the document we want to scan.

Now that we have the path to our image, we can move on to Step 1: Edge Detection.

Line 61 performs the warping transformation. In fact, all the heavy lifting is handled by the four_point_transform  function. Again, you can read more about this function in last week’s post.

We’ll pass two arguments into four_point_transform : the first is our original image we loaded off disk (not the resized one), and the second argument is the contour representing the document, multiplied by the resized ratio.

So, you may be wondering, why are we multiplying by the resized ratio?

We multiply by the resized ratio because we performed edge detection and found contours on the resized image of height=500 pixels.

However, we want to perform the scan on the original image, not the resized image, thus we multiply the contour points by the resized ratio.

To obtain the black and white feel to the image, we then take the warped image, convert it to grayscale and apply adaptive thresholding on Lines 65-67.

Finally, we display our output on Lines 70-73.

And speaking of output, take a look at our example document:

## File list

Tips: You can preview the content of files by clicking file names^_^
Name Size Date
01.97 kB
.DS_Store12.00 kB2014-08-23 15:40
01.97 kB
01.97 kB
._.DS_Store82.00 B2014-08-23 15:40
01.97 kB
.DS_Store6.00 kB2014-08-23 13:58
01.97 kB
._.DS_Store82.00 B2014-08-23 13:58
page.jpg2.01 MB2014-06-04 19:25
._page.jpg111.77 kB2014-06-04 19:25
receipt.jpg2.03 MB2014-08-23 13:59
01.97 kB
.DS_Store6.00 kB2014-08-23 12:19
01.97 kB
._.DS_Store82.00 B2014-08-23 12:19
<__init__.py>0.00 B2014-06-04 19:00
__init__.pyc159.00 B2014-06-04 19:00
imutils.py1.46 kB2014-02-02 08:01
imutils.pyc1.46 kB2014-06-04 19:00
transform.py2.27 kB2014-08-22 13:04
transform.pyc1.56 kB2014-08-23 12:20
scan.py2.42 kB2014-08-23 14:00
...

Minimum:15 words, Maximum:160 words
• 1
• Sent successfully!
• document-scanner.zip
• 1 Point(s)

### An image into a document scanner using python(4.11 MB)

Need 1 Point(s)

Get 22 Point immediately by PayPal

More(Debit card / Credit card / PayPal Credit / Online Banking)

Submit your source codes. Get more Points

Don't have an account？ Register now
Need any help?
Mail to: support@codeforge.com

### 切换到中文版？

CodeForge Chinese Version
CodeForge English Version

### ^_^"Oops ...

Sorry!This guy is mysterious, its blog hasn't been opened, try another, please!

### Warm tip!

Favorite by Ctrl+D