Document Scanner using OpenCV – with source code – easiest way – 2024

So guys, in today’s blog we will see that how we can build a very simple yet powerful Document scanner using OpenCV. This is one of my favorite projects because of its simplicity and its power. So without any further due, Let’s do it…

Code for Document Scanner using OpenCV…

import cv2
import imutils
from skimage.filters import threshold_local
from pyimagesearch.transform import four_point_transform
import numpy as np

img_path = 'b.jpg'
big_img = cv2.imread(img_path)
cv2.imshow('org img',big_img)

ratio = big_img.shape[0] / 500.0
org = big_img.copy()
img = imutils.resize(big_img, height = 500)

gray_img = cv2.cvtColor(img.copy(),cv2.COLOR_BGR2GRAY)
blur_img = cv2.GaussianBlur(gray_img,(5,5),0)
edged_img = cv2.Canny(blur_img,75,200)

cnts,_ = cv2.findContours(edged_img.copy(),cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
cnts = sorted(cnts,key=cv2.contourArea,reverse=True)[:5]
for c in cnts:
    peri = cv2.arcLength(c,True)
    approx = cv2.approxPolyDP(c,0.02*peri,True)
    if len(approx)==4:
        doc = approx
for d in doc:
    tuple_point = tuple(d[0]),tuple_point,3,(0,0,255),4)
cv2.imshow('Corner points detected',img)

warped = four_point_transform(org, doc.reshape(4, 2) * ratio)
warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY)
cv2.imshow("Warped", imutils.resize(warped, height = 650))

T = threshold_local(warped, 11, offset = 10, method = "gaussian")
warped = (warped > T).astype("uint8") * 255
cv2.imshow("Scanned", imutils.resize(warped, height = 650))
  • Line 1-5 – Import the required libraries.
  • Line 7-10 – Read and show the input image.
Document Scanner using OpenCV
  • Line 13 – Finding the ratio by just dividing our image height by 500. We will use this ratio further in the process.
  • Line 14 – Making a copy of the original image.
  • Line 15-17 – Resizing the image and making the height to 500 keeping the aspect ratio constant.
Document Scanner using OpenCV
  • Line 20 – Here we are converting our BGR image to a grayscale image.
  • Line 21 – Here we are using Gaussian Blur to remove the Gaussian Noise from the image.
  • Line 22 – In this step, we are simply finding the edges in the image using Canny Edge Detection.
  • Line 23 -24 – Showing the image.
Document Scanner using OpenCV
  • Line 27 – Finding all the contours in the image
  • Line 28 – Sorting the contours in descending order based on their contour area and just taking the first 5.
  • Line 29-34 – Traversing in contours and finding the contour with 4 sides using cv2.approxPolyDP().
  • Line 37-43 – We are simply drawing points/circles around the corners of the document. These are the points that we got above in the contour detection step using the CHAIN_APPROX_SIMPLE method.
Document Scanner using OpenCV
  • Line 46-49 – We are applying the four-point transformation to the image. It means that we will only extract the document from the image. Or we can also say that we just want to extract the rectangle formed by the four points.
Document Scanner using OpenCV
  • Line 52-55 – Threshold the image to give it a black-and-white feel.
  • Line 56 – Destroy all open windows.

Final Result…

Document Scanner using OpenCV

Download Source Code for Document Scanner using OpenCV…

Do let me know if there’s any query regarding Document Scanner using OpenCV by contacting me on email or LinkedIn.

So this is all for this blog folks, thanks for reading it and I hope you are taking something with you after reading this and till the next time ?…


Check out my other machine learning projectsdeep learning projectscomputer vision projectsNLP projectsFlask projects at

Leave a Reply

Your email address will not be published. Required fields are marked *