
- Step 1: Detect edges.
- Step 2: Use the edges in the image to find the contour (outline) representing the piece of paper being scanned.
- Step 3: Apply a perspective transform to obtain the top-down view of the document.
Perspective Transform

Edge Detection


Finding Contours

Perspective Transform & Threshold

# import the necessary packagesfrom transform import four_point_transformfrom skimage.filters import threshold_localimport numpy as npimport argparseimport cv2import imutils# construct the argument parser and parse the argumentsap = argparse.ArgumentParser()ap.add_argument("-i", "--image", required = False, default='test.jpg', help = "Path to the image to be scanned")args = vars(ap.parse_args())# load the image and compute the ratio of the old height# to the new height, clone it, and resize itimage = cv2.imread(args["image"])ratio = image.shape[0] / 1024.0orig = image.copy()image = imutils.resize(image, height = 1024)# convert the image to grayscale, blur it, and find edges# in the imagegray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)gray = cv2.GaussianBlur(gray, (5, 5), 0)edged = cv2.Canny(gray, 75, 200)# show the original image and the edge detected imageprint("STEP 1: Edge Detection")cv2.imshow("Image", image)cv2.waitKey(0)cv2.imshow("Edged", edged)cv2.waitKey(0)#cv2.destroyAllWindows()# find the contours in the edged image, keeping only the# largest ones, and initialize the screen contourcnts = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)cnts = imutils.grab_contours(cnts)cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:5]# loop over the contoursfor c in cnts: # approximate the contour peri = cv2.arcLength(c, True) approx = cv2.approxPolyDP(c, 0.02 * peri, True) # if our approximated contour has four points, then we # can assume that we have found our screen if len(approx) == 4: screenCnt = approx break pass# show the contour (outline) of the piece of paperprint("STEP 2: Find contours of paper")cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 2)cv2.imshow("Outline", image)cv2.waitKey(0)# apply the four point transform to obtain a top-down# view of the original imagewarped = four_point_transform(orig, screenCnt.reshape(4, 2) * ratio)# convert the warped image to grayscale, then threshold it# to give it that 'black and white' paper effectwarped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY)T = threshold_local(warped, 11, offset = 10, method = "gaussian")warped = (warped > T).astype("uint8") * 255# show the original and scanned imagesprint("STEP 3: Apply perspective transform")cv2.imshow("Original", imutils.resize(orig, height = 1024))cv2.waitKey(0)cv2.imshow("Scanned", imutils.resize(warped, width = 360))cv2.waitKey(0)cv2.destroyAllWindows()