This is a simple Camscanner projecct using openCV
- Pre-processing image to enhance document.
- Corner detection to extract the corner points.
- Warper to make it flat x, y direction.
- Post-processing image (thresholding, denoising)
Pre-processing is an essential step in document preparation, and it can make all the difference in the quality of the final product. The goal of pre-processing is to clean up the document and make it more usable, and there are a variety of methods that can be used to achieve this.
In our preprocessing we following these steps:
- Convert to Grey level
- Denoise ( either using Gaussian Blur or fastNmeanDenoising )
- Convert to morph (can be skipped)
- Edge detection (using canny method)
- Extra processing for better result (can be skipped)
Convert to Grey level | Denoise using Gaussian Blur |
Edge detection using Canny | Code example for pre-processing |
Once the image is pre-processed, the next step is to detect the corner points. This will help us extract the document's shape and transform it into a flat surface.
This function takes in an image and returns an array of pixel points that correspond to the corners of the biggest contour in the image. The steps involved in this function are:
- Initialize an empty array called
corners
to store the corner pixel points of the biggest contour. - Initialize a variable
maxArea
to 0 to keep track of the maximum area encountered so far. - Use the
findContours()
method from the OpenCV library to find all the contours in the image. - Loop through each contour in the image.
- Calculate the area of the contour using the
contourArea()
method. - If the area of the contour is smaller than 6000, ignore it and move on to the next contour.
- If the area of the contour is larger than
maxArea
and the contour has 4 edges (meaning it is a square such as a paper), then save the corner pixel points of this contour in thecorners
array and update the value ofmaxArea
. - Return the
corners
array.
This function takes in an array of corner pixel points and returns a sorted array of these points in a specific order required for further processing. The steps involved in this function are:
- Reshape the
corners
array into a 4x2 matrix for easier manipulations. - Create an empty array called
sortedCorners
to store the sorted corner pixel points. - Calculate the sum of the width and height of each corner and store the results in an array called
add
. - The corner with the smallest sum corresponds to the top-left corner and is saved in
sortedCorners[0]
. The corner with the largest sum corresponds to the bottom-right corner and is saved insortedCorners[3]
. - Calculate the difference between the width and height of each corner and store the results in an array called
diff
. - The corner with the minimum difference corresponds to the top-right corner and is saved in
sortedCorners[1]
. The corner with the maximum difference corresponds to the bottom-left corner and is saved insortedCorners[2]
. - Return the
sortedCorners
array.
Corner detection figure | cornerDetector code example | cornerDetector code example |
After extracting the corner points, we can use them to transform the document into a flat surface in both the x and y directions. This will ensure that the document is easy to read and work with.
Finally, we will perform some post-processing on the transformed document. This will involve thresholding and denoising the image to make it even clearer and easier to work with.