Create Your Own CamScanner Using Python and OpenCV
Create Your Own CamScanner Using Python and OpenCV
1
Speaker
SHIRISH GUPTA
Topic
Create your own 'CamScanner' using
Python & OpenCV
2
Have you ever wondered how a 'CamScanner' converts your mobile camera's fuzzy
document picture into a defined, properly lit and scanned image..? I have and until recently
I thought it was a very difficult task. But it's not and we can make our own 'CamScanner'
with relatively few lines of code
3
• What are Computer Vision ?
• Why the recent Buzz?
• What are we gonna achieve today?
• What is OpenCV?
Agenda for Today • Architecture
• Preprocess the image using different concepts
such as blurring, denoising (Non-Local Means)
and thresholding
• Canny Edge detection & Extraction of biggest
contour
• Sharpening & Brightness correction
• Questions?
4
What is Computer Vision
5
Why the recent buzz?
One modern company who has pioneered the
technology of Computer Vision
• Advancement in AI and Machine Learning has
accelerated the developments in computer vision.
Earlier these were two separate fields and there were
different techniques, coding languages & academic
researchers in both
6
What is OpenCV ?
7
ARCHITECTURE
8
PREPROCESS THE IMAGE
9
BLURRING DENOISING
The goal of blurring is to reduce the noise in the image. It There is another kind of de-noising that we conduct - Non-
removes high frequency content (e.g: noise, edges) from the Local Means Denoising
image - resulting in blurred edges
• Averaging - It simply takes the average of all the pixels under • The principle of the initial denoising methods were to
kernel area and replaces the central element with this replace the colour of a pixel with an average of the colours
average | cv2.blur() of nearby pixels
• Gaussian Filter - Instead of a box filter consisting of equal • But what if there is edge or elongated pattern where
filter coefficients, a Gaussian kernel is used | cv2.GaussianBlur() denoising by averaging wont work and we need to scan a
vast portion of the image in search of all the pixels to
• Median Filter - Computes the median of all the pixels under
denoise.
the kernel window and the central pixel is replaced with this
median value | cv2.medianBlur()) • Denoising is then done by computing the average colour of
these most resembling pixels | cv2.fastNlMeansDenoising()
Kernel Sharpening - A kernel, convolution matrix, or mask is a small matrix. It is
used for blurring, sharpening, embossing, edge detection, and more.
10
Original Picture Blurred (Using Gaussian Blur) Denoised (Non-Local Means Denoising)
11
THRESHOLDING
Simplest method of segmenting images into binary ones. This is
generally done so as to clearly differentiate between shades of
pixel intensities
12
Adaptive Threshold on Gray Scale
CANNY EDGE DETECTION &
EXTRACTION OF BIGGEST
CONTOUR
13
CANNY EDGE DETECTION EXTRACT BIGGEST
Canny edge detection & extraction is a multi-step algorithm CONTOUR
We still need to find the corners so as to find the exact co-
that can detect edges and find the ‘biggest’ contour ordinates to crop the image
• First - Send a de-noised image to this algorithm so that it is • Order Point Transformation - Apply order points
able to detect ‘relevant’ edges | cv2.Canny() transformation to get exact (x,y) coordinates of the biggest
joining all the continuous points (along the edges), having • Four Point Transformation — Using the above (x,y)
same colour or intensity. Resultant — rectangles, spheres, etc coordinates, calculate the width and height of the contour
| cv2.findcontours() and pass it through the cv2.warpPerspective() to crop the
14
Original Picture Biggest contour extracted using Canny Cropped Image
Edge Detection
15
FINALLY — SHARPENESS &
BRIGHTNESS CORRECTION
16
BRIGHTEN AND
SHARPENING
Last step is to sharpen the image to get well illuminated and
readable document
cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
17
Full Article
https://levelup.gitconnected.com/create-your-own-camscanner-
using-python-opencv-66251212270
Full Code
https://gist.github.com/shirish201/959edf669c43fd5e596bc311e
b396710
18
You can contact me via
19
Thankyou!
20