Search⌘ K

A Review of the Document Scanner

Explore how to build a document scanner by processing images with OpenCV. Learn techniques like grayscale conversion, edge detection, contour detection, ordering contour points, and warping images to isolate documents.

Let’s look at the correct code for creating the document scanner.

First, we set the size of our document after scanning.

frameWidth = 480
frameHeight = 640

Image Processing

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 cv2.canny() function of the OpenCV library:

Python
def imageProcessing(img):
imgGray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
imgBlur = cv2.GaussianBlur(imgGray,(5,5),1)
imgCanny = cv2.Canny(imgBlur,200,200)
return imgCanny

Get image contours

Next, we define the variable that we’ll use in this function. We define a NumPy array, biggest, and an integer, bArea. We use the cv2.findcontours method to find all the contours:

C++
biggest = np.array([])
bArea = 0
contours,heirarchy = cv2.findContours(img,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_NONE)

We loop ...