.需要矫正的图片1
矫正后的结果:
需要矫正的图片2
# import the necessary packages
from imutils.perspectiveimport four_point_transform
from imutilsimport contours
import numpyas np
import argparse
import imutils
import cv2
# construct the argument parse and parse the arguments
# ap = argparse.ArgumentParser()
# ap.add_argument("-i", "--image", required=True,
# help="path to the input image")
# args = vars(ap.parse_args())
# define the answer key which maps the question number
# to the correct answer
ANSWER_KEY = {0:1,1:4,2:0,3:3,4:1}
# load the image, convert it to grayscale, blur it
# slightly, then find edges
image = cv2.imread("changeRect.png")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5,5),0)
edged = cv2.Canny(blurred,75,200)
#find contours in edge map ,then initialize the contour corresponds to the document
cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if imutils.is_cv2() else cnts[1]
docCnt =None
# ensure that at least one contour was found
if len(cnts) > 0:
# sort the contours according to their size in
# descending order
cnts =sorted(cnts,key=cv2.contourArea,reverse=True)
# loop over the sorted contours
for cin 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 we have found the paper
if len(approx) ==4:
docCnt = approx
break
# apply a four point perspective transform to both the
# original image and grayscale image to obtain a top-down
# birds eye view of the paper
paper = four_point_transform(image, docCnt.reshape(4,2))
warped = four_point_transform(gray, docCnt.reshape(4,2))
cv2.imshow("original", image)
cv2.imshow("Exam", paper)
cv2.waitKey(0)