一、思路分析
首先拿到答題卡照片的時候,需要對照片進行一系列預處理操作,通過透視變換將圖像擺正方便后續的操作。每一道題五個選項,有五道題,通過字典存放準確答案。沒有依次對答題卡進行輪廓檢測,這里采用的是正方形,寬高比是1:1,當然也可以是矩形,也可以通過指定其他的篩選進行進行過濾篩選。最后通過掩膜操作,因為用戶所選擇的答案都是被涂過的,也就是通過判斷黑色和白色來進行區分是否是用戶選擇的答案。一行一行的存儲,因為一道題是五個選項,每一行是一道題,這里采用從上到下從左到右分別依次存放1-5題的A-E選項。通過與標準答案字典所存放的索引進行對比,從而給出用戶得分。
二、導包及其相關函數
#導入工具包
import numpy as np
import argparse
import imutils
import cv2
# 設置參數
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,help="path to the input image")
args = vars(ap.parse_args())# 正確答案
ANSWER_KEY = {0: 1, 1: 4, 2: 0, 3: 3, 4: 1}#存放正確答案BEADB
def order_points(pts):# 一共4個坐標點rect = np.zeros((4, 2), dtype = "float32")# 按順序找到對應坐標0123分別是 左上,右上,右下,左下# 計算左上,右下s = pts.sum(axis = 1)rect[0] = pts[np.argmin(s)]rect[2] = pts[np.argmax(s)]# 計算右上和左下diff = np.diff(pts, axis = 1)rect[1] = pts[np.argmin(diff)]rect[3] = pts[np.argmax(diff)]return rectdef four_point_transform(image, pts):#透視變換# 獲取輸入坐標點rect = order_points(pts)(tl, tr, br, bl) = rect#拿到答題卡四個頂點坐標# 計算輸入的w和h值widthA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2))#因為拿到的答題卡的不一定是正兒八經的矩形,需要對四條邊進行計算長度,選出最長的作為最后矩形的長度widthB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2))maxWidth = max(int(widthA), int(widthB))heightA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2))heightB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2))maxHeight = max(int(heightA), int(heightB))# 變換后對應坐標位置dst = np.array([[0, 0],[maxWidth - 1, 0],[maxWidth - 1, maxHeight - 1],[0, maxHeight - 1]], dtype = "float32")#看個人需求而定,這里將圖像左上角規定為(0,0)位置# 計算變換矩陣M = cv2.getPerspectiveTransform(rect, dst)warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight))# 返回變換后結果return warped
def sort_contours(cnts, method="left-to-right"):#從上到下進行排序,因為題目就是一行一行的reverse = Falsei = 0if method == "right-to-left" or method == "bottom-to-top":reverse = Trueif method == "top-to-bottom" or method == "bottom-to-top":i = 1boundingBoxes = [cv2.boundingRect(c) for c in cnts](cnts, boundingBoxes) = zip(*sorted(zip(cnts, boundingBoxes), key=lambda b: b[1][i], reverse=reverse))#排完序之后,前五個是第一題的,之后每五個依次為下一題的return cnts, boundingBoxes
def cv_show(name,img):cv2.imshow(name, img)cv2.waitKey(0)cv2.destroyAllWindows()
三、對答題卡進行預處理及透視變換擺正
# 預處理
image = cv2.imread(args["image"])#讀取圖像
contours_img = image.copy()#為了不改動原始圖像,copy一下圖像,因為后續需要進行一系列輪廓檢測
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)#灰度圖
blurred = cv2.GaussianBlur(gray, (5, 5), 0)#高斯濾波,去除一些噪音點
cv_show('blurred',blurred)
edged = cv2.Canny(blurred, 75, 200)#Canny邊緣檢測
cv_show('edged',edged)
# 輪廓檢測
cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[1]#輪廓檢測完之后會得到三個返回值,這里的[1]存放的是輪廓信息
cv2.drawContours(contours_img,cnts,-1,(0,0,255),3) #將圖像通過透視變換進行擺正,繪制出答題卡的大致輪廓,拿到的答題卡圖像也不一定是正兒八經的矩形
cv_show('contours_img',contours_img)
docCnt = None
# 確保檢測到了
if len(cnts) > 0:#因為可能會檢測到其他干擾影響,但是答題卡的輪廓是最大的# 根據輪廓大小進行排序cnts = sorted(cnts, key=cv2.contourArea, reverse=True)#把檢測到的所有輪廓按面積進行排序# 遍歷每一個輪廓for c in cnts:# 近似peri = cv2.arcLength(c, True)#計算一下輪廓周長approx = cv2.approxPolyDP(c, 0.02 * peri, True)#對輪廓進行近似# 準備做透視變換if len(approx) == 4:#多邊形頂點有四個也就是矩形,這個就是我們的答題卡輪廓docCnt = approxbreak
# 執行透視變換
warped = four_point_transform(gray, docCnt.reshape(4, 2))
cv_show('warped',warped)
四、對每一道題均進行輪廓檢測,遍歷篩選
# 自適應閾值處理
thresh = cv2.threshold(warped, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
cv_show('thresh',thresh)
thresh_Contours = thresh.copy()
# 找到每一道題輪廓
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[1]#這里再次進行輪廓檢測,之所以不用霍夫圓檢測是因為有可能答題卡會被全部涂滿甚至越界
cv2.drawContours(thresh_Contours,cnts,-1,(0,0,255),3)
cv_show('thresh_Contours',thresh_Contours)
questionCnts = []
# 遍歷,對所有的輪廓進行篩選
for c in cnts:# 計算比例和大小(x, y, w, h) = cv2.boundingRect(c)ar = w / float(h)#因為是圓形,這里是寬高比,外接矩形差不多寬高比是1:1# 根據實際情況指定標準if w >= 20 and h >= 20 and ar >= 0.9 and ar <= 1.1:questionCnts.append(c)
# 按照從上到下進行排序
questionCnts = sort_contours(questionCnts, method="top-to-bottom")[0]
correct = 0
五、對比答案,評分
# 每排有5個選項
for (q, i) in enumerate(np.arange(0, len(questionCnts), 5)):#因為每一題都有五個選項,q為第幾行# 排序cnts = sort_contours(questionCnts[i:i + 5])[0]#第i題的五個結果bubbled = None# 遍歷每一個結果for (j, c) in enumerate(cnts):#j為第i道題的第j個選項# 使用mask來判斷結果mask = np.zeros(thresh.shape, dtype="uint8")cv2.drawContours(mask, [c], -1, 255, -1) #-1表示填充cv_show('mask',mask)# 通過計算非零點數量來算是否選擇這個答案mask = cv2.bitwise_and(thresh, thresh, mask=mask)total = cv2.countNonZero(mask)#看下框出來的選項中非零的個數有多少個# 通過閾值判斷if bubbled is None or total > bubbled[0]:bubbled = (total, j)# 對比正確答案color = (0, 0, 255)k = ANSWER_KEY[q]#第q道題的答案# 判斷正確if k == bubbled[1]:color = (0, 255, 0)correct += 1# 繪圖cv2.drawContours(warped, [cnts[k]], -1, color, 3)
score = (correct / 5.0) * 100
print("[INFO] score: {:.2f}%".format(score))
cv2.putText(warped, "{:.2f}%".format(score), (10, 30),cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2)
cv2.imshow("Original", image)
cv2.imshow("Exam", warped)
cv2.waitKey(0)
六、Pycharm參數設定
設置參數指定圖像路徑
找到Edit Configurations
將image參數改成自己測試圖像路徑--image images\test11.png
,其中images\test11.png為答題卡路徑
七、完整代碼
#導入工具包
import numpy as np
import argparse
import imutils
import cv2# 設置參數
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,help="path to the input image")
args = vars(ap.parse_args())# 正確答案
ANSWER_KEY = {0: 1, 1: 4, 2: 0, 3: 3, 4: 1}#存放正確答案BEADBdef order_points(pts):# 一共4個坐標點rect = np.zeros((4, 2), dtype = "float32")# 按順序找到對應坐標0123分別是 左上,右上,右下,左下# 計算左上,右下s = pts.sum(axis = 1)rect[0] = pts[np.argmin(s)]rect[2] = pts[np.argmax(s)]# 計算右上和左下diff = np.diff(pts, axis = 1)rect[1] = pts[np.argmin(diff)]rect[3] = pts[np.argmax(diff)]return rectdef four_point_transform(image, pts):#透視變換# 獲取輸入坐標點rect = order_points(pts)(tl, tr, br, bl) = rect#拿到答題卡四個頂點坐標# 計算輸入的w和h值widthA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2))#因為拿到的答題卡的不一定是正兒八經的矩形,需要對四條邊進行計算長度,選出最長的作為最后矩形的長度widthB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2))maxWidth = max(int(widthA), int(widthB))heightA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2))heightB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2))maxHeight = max(int(heightA), int(heightB))# 變換后對應坐標位置dst = np.array([[0, 0],[maxWidth - 1, 0],[maxWidth - 1, maxHeight - 1],[0, maxHeight - 1]], dtype = "float32")#看個人需求而定,這里將圖像左上角規定為(0,0)位置# 計算變換矩陣M = cv2.getPerspectiveTransform(rect, dst)warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight))# 返回變換后結果return warped
def sort_contours(cnts, method="left-to-right"):#從上到下進行排序,因為題目就是一行一行的reverse = Falsei = 0if method == "right-to-left" or method == "bottom-to-top":reverse = Trueif method == "top-to-bottom" or method == "bottom-to-top":i = 1boundingBoxes = [cv2.boundingRect(c) for c in cnts](cnts, boundingBoxes) = zip(*sorted(zip(cnts, boundingBoxes), key=lambda b: b[1][i], reverse=reverse))#排完序之后,前五個是第一題的,之后每五個依次為下一題的return cnts, boundingBoxes
def cv_show(name,img):cv2.imshow(name, img)cv2.waitKey(0)cv2.destroyAllWindows() # 預處理
image = cv2.imread(args["image"])#讀取圖像
contours_img = image.copy()#為了不改動原始圖像,copy一下圖像,因為后續需要進行一系列輪廓檢測
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)#灰度圖
blurred = cv2.GaussianBlur(gray, (5, 5), 0)#高斯濾波,去除一些噪音點
cv_show('blurred',blurred)
edged = cv2.Canny(blurred, 75, 200)#Canny邊緣檢測
cv_show('edged',edged)# 輪廓檢測
cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[1]#輪廓檢測完之后會得到三個返回值,這里的[1]存放的是輪廓信息
cv2.drawContours(contours_img,cnts,-1,(0,0,255),3) #將圖像通過透視變換進行擺正,繪制出答題卡的大致輪廓,拿到的答題卡圖像也不一定是正兒八經的矩形
cv_show('contours_img',contours_img)
docCnt = None# 確保檢測到了
if len(cnts) > 0:#因為可能會檢測到其他干擾影響,但是答題卡的輪廓是最大的# 根據輪廓大小進行排序cnts = sorted(cnts, key=cv2.contourArea, reverse=True)#把檢測到的所有輪廓按面積進行排序# 遍歷每一個輪廓for c in cnts:# 近似peri = cv2.arcLength(c, True)#計算一下輪廓周長approx = cv2.approxPolyDP(c, 0.02 * peri, True)#對輪廓進行近似# 準備做透視變換if len(approx) == 4:#多邊形頂點有四個也就是矩形,這個就是我們的答題卡輪廓docCnt = approxbreak# 執行透視變換
warped = four_point_transform(gray, docCnt.reshape(4, 2))
cv_show('warped',warped)# 自適應閾值處理
thresh = cv2.threshold(warped, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
cv_show('thresh',thresh)
thresh_Contours = thresh.copy()# 找到每一道題輪廓
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[1]#這里再次進行輪廓檢測,之所以不用霍夫圓檢測是因為有可能答題卡會被全部涂滿甚至越界
cv2.drawContours(thresh_Contours,cnts,-1,(0,0,255),3)
cv_show('thresh_Contours',thresh_Contours)
questionCnts = []# 遍歷,對所有的輪廓進行篩選
for c in cnts:# 計算比例和大小(x, y, w, h) = cv2.boundingRect(c)ar = w / float(h)#因為是圓形,這里是寬高比,外接矩形差不多寬高比是1:1# 根據實際情況指定標準if w >= 20 and h >= 20 and ar >= 0.9 and ar <= 1.1:questionCnts.append(c)# 按照從上到下進行排序
questionCnts = sort_contours(questionCnts, method="top-to-bottom")[0]
correct = 0# 每排有5個選項
for (q, i) in enumerate(np.arange(0, len(questionCnts), 5)):#因為每一題都有五個選項,q為第幾行# 排序cnts = sort_contours(questionCnts[i:i + 5])[0]#第i題的五個結果bubbled = None# 遍歷每一個結果for (j, c) in enumerate(cnts):#j為第i道題的第j個選項# 使用mask來判斷結果mask = np.zeros(thresh.shape, dtype="uint8")cv2.drawContours(mask, [c], -1, 255, -1) #-1表示填充cv_show('mask',mask)# 通過計算非零點數量來算是否選擇這個答案mask = cv2.bitwise_and(thresh, thresh, mask=mask)total = cv2.countNonZero(mask)#看下框出來的選項中非零的個數有多少個# 通過閾值判斷if bubbled is None or total > bubbled[0]:bubbled = (total, j)# 對比正確答案color = (0, 0, 255)k = ANSWER_KEY[q]#第q道題的答案# 判斷正確if k == bubbled[1]:color = (0, 255, 0)correct += 1# 繪圖cv2.drawContours(warped, [cnts[k]], -1, color, 3)score = (correct / 5.0) * 100
print("[INFO] score: {:.2f}%".format(score))
cv2.putText(warped, "{:.2f}%".format(score), (10, 30),cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2)
cv2.imshow("Original", image)
cv2.imshow("Exam", warped)
cv2.waitKey(0)
答題卡原題:
這里展示的太大,我就截取了其中一小部分進行展示
接下來就是依次對每道題進行遍歷找到掩膜,一共25次,這里就不一一展示了
最后根據設定的字典里面的正確答案給出評分