...

/

Solution: Document Scanner

Solution: Document Scanner

Review the steps involved in creating a document scanner

Let’s look at the solution for creating the document scanner.

Get image edges

We change the image to grayscale and then make it blurry. This helps us get the edges properly. To get the edges, we use the Canny() function of the OpenCV library:

// Including header files
#include <opencv2/opencv.hpp>
#include <iostream>

// Declaring variables
cv::Mat origImg, grayImg, blurImg, cannnyImg, threImg;

// Image processing
cv::Mat preProcessing(cv::Mat img)
{
    cv::cvtColor(img, grayImg, cv::COLOR_BGR2GRAY);
    cv::GaussianBlur(grayImg, blurImg, cv::Size(3, 3), 3, 0);
    cv::Canny(blurImg, cannnyImg, 25, 75);
    return cannnyImg;
}

int main() {

    std::string path = "/usercode/docs.jpg";
    origImg = cv::imread(path);
    threImg = preProcessing(origImg);
    cv::imshow("Image", origImg);
    cv::imshow("Image Crop", threImg);
    cv::waitKey(0);
    return 0;
}
Getting image edges

Get image contours

We loop through all those contours. We use a new function, contourArea(), and give the contours as the parameter to calculate its area. ...