0% found this document useful (0 votes)
147 views

Create Your Own CamScanner Using Python and OpenCV

Almost all of us have used CamScanner (or a similar app) in our lives. It is a very effective app which allows users to scan documents from your mobile and share it as an image. The biggest advantage of the application is that it ‘cleans’ (denoising, rotation, sharpened, etc) up a camera-clicked image into a very refined output. But do you know that Computer Vision is the science behind it, and we can create our own CamScanner using the basics of OpenCV in Python. But before that, what is OpenCV

Uploaded by

Shirish Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
147 views

Create Your Own CamScanner Using Python and OpenCV

Almost all of us have used CamScanner (or a similar app) in our lives. It is a very effective app which allows users to scan documents from your mobile and share it as an image. The biggest advantage of the application is that it ‘cleans’ (denoising, rotation, sharpened, etc) up a camera-clicked image into a very refined output. But do you know that Computer Vision is the science behind it, and we can create our own CamScanner using the basics of OpenCV in Python. But before that, what is OpenCV

Uploaded by

Shirish Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 20

Create your own CamScanner

using Python and OpenCV

1
Speaker
SHIRISH GUPTA

HEAD – DATA SCIENCE & PARTNERSHIPS AT


LOAN2GROW

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

“Compared to what we have in mind”

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

• Computer vision is an interdisciplinary scientific


field that deals with how computers can gain high-
level understanding from digital images or videos.
From the perspective of engineering, it seeks to
understand and automate tasks that the human
visual system can do.

• Basically, it's a scientific field to make the


computers understand a photo/video similar to how
it will be interpreted by a human being.

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

• But now, the gap has been reduced significantly and


more and more data scientists are working in the
field of computer vision and vice-a-versa.

• The reason is the simple common denominator in

both the fields - DATA

6
What is OpenCV ?

• OpenCV is a library of programming functions


mainly aimed at real-time computer vision. Originally
developed by Intel, it was later supported by Willow
Garage and then Itseez.

• Initially developed in C++ but now it's available


across multiple languages such Python, Java, etc.

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

• Simple Thresholding — If pixel value is greater than a


threshold value, it is assigned one value (may be white), else
Gray Scaled
it is assigned another value (may be black) | cv2.threshold()

• Adaptive Thresholding — Algorithm calculates the


threshold for a small regions of the image. So we get
different thresholds for different regions of the same image
and it gives us better results for images with varying
illumination (No ‘single’ threshold for the whole image) |
cv2.adaptiveThreshold()

Note: Remember to convert the images to grayscale before thresholding

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

• Second – Search all ‘contours’ from the above images by contour.

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

• Finally extract the biggest rectangular (approx.) contour from contour

the above | cv2.convexHull() & cv2.approxPolyDP()

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

• Brightening - Use hue, saturation, value (h,s,v) concept


where ‘v’-value represents the brightness. Play around with
this value to increase the brightness of the documents |

cv2.cvtColor(img, cv2.COLOR_BGR2HSV)

• Kernel Sharpening - A kernel,


convolution matrix, or mask is a small
matrix. It is used for blurring,
sharpening, embossing, edge
detection, and more. Play around with
different kernels to sharpen the image Kernel used in the code

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

[email protected] https://www.facebook.com/shirish https://www.linkedin.com/in/shirish


.gupta.180/ -gupta-ba003742/

19
Thankyou!

20

You might also like