在现代教育中,自动化评分系统正变得越来越重要。本文将带您使用OpenCV实现一个答题卡自动识别与打分系统。从图像预处理到答案区域检测,再到最终得分计算,我们将一步步讲解整个流程,并附上完整的代码示例。无论您是初学者还是有一定经验的开发者,都能从中受益!
题卡识别的应用场景,例如标准化考试、在线测评等。如何通过图像识别技术提取答题卡中的答案信息?需要解决的关键问题:图像倾斜校正、答案区域定位、选项识别等。使用OpenCV进行图像处理。可结合简单的机器学习算法(如KNN)进行分类。
2.操作步骤
-
灰度化:将彩色图像转换为灰度图像。
-
高斯模糊:去除噪声。
-
边缘检测:使用Canny算法检测答题卡的轮廓。
-
透视变换:校正答题卡的倾斜角度。
-
代码示例:
import cv2
import numpy as np
# 读取图像并灰度化
image = cv2.imread('answer_sheet.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 高斯模糊
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
# 边缘检测
edged = cv2.Canny(blurred, 75, 200)
# 查找轮廓并排序
contours, _ = cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = sorted(contours, key=cv2.contourArea, reverse=True)[:5]
# 获取答题卡的四个顶点
for contour in contours:
peri = cv2.arcLength(contour, True)
approx = cv2.approxPolyDP(contour, 0.02 * peri, True)
if len(approx) == 4:
doc_contour = approx
break
# 透视变换
def four_point_transform(image, pts):
rect = order_points(pts)
(tl, tr, br, bl) = rect
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")
M = cv2.getPerspectiveTransform(rect, dst)
warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight))
return warped
warped = four_point_transform(image, doc_contour.reshape(4, 2))
-
使用形态学操作(如膨胀和腐蚀)突出答案区域。
-
使用轮廓检测提取每个答案框的位置。
-
代码示例:
# 形态学操作
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5, 5))
morph = cv2.morphologyEx(warped, cv2.MORPH_CLOSE, kernel)
# 轮廓检测
contours, _ = cv2.findContours(morph, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
for contour in contours:
x, y, w, h = cv2.boundingRect(contour)
if w > 20 and h > 20: # 过滤掉过小的区域
cv2.rectangle(warped, (x, y), (x + w, y + h), (0, 255, 0), 2)
-
将答案框分割为单个选项。
-
计算每个选项区域的像素值,判断是否被涂黑。
-
与标准答案进行比对,计算得分。
-
代码示例:
# 假设标准答案为 [1, 2, 3, 4]
correct_answers = [1, 2, 3, 4]
user_answers = []
for i, box in enumerate(answer_boxes):
roi = warped[box[1]:box[1]+box[3], box[0]:box[0]+box[2]]
mean_val = cv2.mean(roi)[0]
if mean_val < 100: # 如果平均像素值较低,则认为被涂黑
user_answers.append(i % 4 + 1)
# 计算得分
score = sum([1 for u, c in zip(user_answers, correct_answers) if u == c])
print(f"得分:{score}/{len(correct_answers)}")
3.完整代码
import numpy as np
import cv2
# 定义正确答案字典
ANSWER_KEY = {0: 1, 1: 4, 2: 0, 3: 3, 4: 1}
def order_points(pts):
"""
对四个顶点进行排序,顺序为:左上、右上、右下、左下。
:param pts: 输入的四个顶点坐标
:return: 排序后的四个顶点坐标
"""
rect = np.zeros((4, 2), dtype="float32")
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 rect
def four_point_transform(image, pts):
"""
对图像进行透视变换,将其转换为正视图。
:param image: 输入图像
:param pts: 四个顶点坐标
:return: 透视变换后的图像
"""
rect = order_points(pts)
(tl, tr, br, bl) = rect
# 计算新图像的宽度和高度
width_a = np.linalg.norm(br - bl)
width_b = np.linalg.norm(tr - tl)
max_width = max(int(width_a), int(width_b))
height_a = np.linalg.norm(tr - br)
height_b = np.linalg.norm(tl - bl)
max_height = max(int(height_a), int(height_b))
# 定义目标四边形的坐标
dst = np.array([
[0, 0],
[max_width - 1, 0],
[max_width - 1, max_height - 1],
[0, max_height - 1]], dtype="float32")
# 计算透视变换矩阵并应用变换
transform_matrix = cv2.getPerspectiveTransform(rect, dst)
warped = cv2.warpPerspective(image, transform_matrix, (max_width, max_height))
return warped
def sort_contours(cnts, method="left-to-right"):
"""
对轮廓进行排序。
:param cnts: 轮廓列表
:param method: 排序方法(left-to-right, right-to-left, top-to-bottom, bottom-to-top)
:return: 排序后的轮廓和对应的边界框
"""
reverse = False
i = 0
if method in ("right-to-left", "bottom-to-top"):
reverse = True
if method in ("top-to-bottom", "bottom-to-top"):
i = 1
bounding_boxes = [cv2.boundingRect(c) for c in cnts]
(cnts, bounding_boxes) = zip(*sorted(zip(cnts, bounding_boxes),
key=lambda b: b[1][i], reverse=reverse))
return cnts, bounding_boxes
def cv_show(name, img):
"""
显示图像。
:param name: 窗口名称
:param img: 要显示的图像
"""
cv2.imshow(name, img)
cv2.waitKey(0)
cv2.destroyAllWindows()
def main(image_path):
"""
主函数,处理图像并计算分数。
:param image_path: 输入图像路径
"""
# 读取图像并转换为灰度图
image = cv2.imread(image_path)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
edged = cv2.Canny(blurred, 75, 200)
# 显示中间结果
cv_show('Blurred', blurred)
cv_show('Edged', edged)
# 查找轮廓
cnts, _ = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours_img = image.copy()
cv2.drawContours(contours_img, cnts, -1, (0, 0, 255), 3)
cv_show('Contours', contours_img)
# 找到试卷区域的轮廓
doc_cnt = 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:
doc_cnt = approx
break
# 执行透视变换
warped = four_point_transform(gray, doc_cnt.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)
cv2.drawContours(thresh_contours, cnts, -1, (0, 0, 255), 3)
cv_show('Thresh Contours', thresh_contours)
# 过滤出符合要求的圆形轮廓
question_cnts = []
for c in cnts:
(x, y, w, h) = cv2.boundingRect(c)
ar = w / float(h)
if w >= 20 and h >= 20 and 0.9 <= ar <= 1.1:
question_cnts.append(c)
# 对轮廓按从上到下排序
question_cnts = sort_contours(question_cnts, method="top-to-bottom")[0]
# 计算分数
correct = 0
for (q, i) in enumerate(np.arange(0, len(question_cnts), 5)):
cnts = sort_contours(question_cnts[i:i + 5])[0]
bubbled = None
# 查找被填涂的答案
for (j, c) in enumerate(cnts):
mask = np.zeros(thresh.shape, dtype="uint8")
cv2.drawContours(mask, [c], -1, 255, -1)
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]
if k == bubbled[1]:
color = (0, 255, 0)
correct += 1
cv2.drawContours(warped, [cnts[k]], -1, color, 3)
# 计算并显示分数
score = (correct / 5.0) * 100
print(f"[INFO] Score: {score:.2f}%")
cv2.putText(warped, f"{score:.2f}%", (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2)
# 显示最终结果
cv2.imshow("Original", image)
cv2.imshow("Exam", warped)
cv2.waitKey(0)
if __name__ == '__main__':
# 运行主函数
main("images/test_01.png")
# 运行结果 [INFO] Score: 80.00%