网站动态页面怎么做,微信商城网站如何做,做黑彩网站图片,网页游戏排行榜2013前十名文章目录 一、案例介绍二、代码解析 一、案例介绍
下面是一张使用2B铅笔填涂选项后的答题卡 使用OpenCV 中的各种方法进行真确答案识别#xff0c;最终将正确填涂的答案用绿色圈出#xff0c;错误的答案不圈出#xff0c;用红色圈出错误题目的正确答案最终统计正确的题目数… 文章目录 一、案例介绍二、代码解析 一、案例介绍
下面是一张使用2B铅笔填涂选项后的答题卡 使用OpenCV 中的各种方法进行真确答案识别最终将正确填涂的答案用绿色圈出错误的答案不圈出用红色圈出错误题目的正确答案最终统计正确的题目数量并在答题卡的左上角写出分数最终的结果图如下
二、代码解析 先直接上完整代码 import numpy as np
import cv2 定义显示图片的函数 def cv_show(name, img):cv2.imshow(name, img)cv2.waitKey(0) 寻找透视变换时的四个近似轮廓的顶点 def order_points(pts):# 一共4个坐标rect np.zeros((4, 2), dtypefloat32)s pts.sum(axis1)rect[0] pts[np.argmin(s)]rect[2] pts[np.argmax(s)]diff np.diff(pts, axis1)rect[1] pts[np.argmin(diff)]rect[3] pts[np.argmax(diff)]return rect 图像透视变换函数 def 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]], dtypefloat32)M cv2.getPerspectiveTransform(rect, dst)warped cv2.warpPerspective(image, M, (maxWidth, maxHeight))return warped 轮廓排序函数 def sort_contours(cnts, methodleft-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),keylambda b: b[1][i], reversereverse))# zip(*...)使用星号操作符解包排序后的元组列表并将其重新组合成两个列表:一个包含所有轮廓另一个包含所有边界框。return cnts, boundingBoxesANSWER_KEY {0: 1, 1: 4, 2: 0, 3: 3, 4: 1} # 正确答案 图片预处理
image cv2.imread(images/test_01.png) # 读取答题卡图片
contours_img image.copy() # 复制一个原图为了后续的步骤在其上操作
gray cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # 转换为灰度图高斯滤波 去除图片中的噪声点
blurred cv2.GaussianBlur(gray, (5, 5), 0)
cv_show(blurred, blurred) # 提前定义好的用于显示图片的函数 cv_show边缘检测
edged cv2.Canny(blurred, 75, 200)
cv_show(edged, edged)轮廓检测
cnts cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[1]
cv2.drawContours(contours_img, cnts, -1, (0, 0, 255), 3)
cv_show(contours_img, contours_img) 图像透视变换 -- 将答题卡区域提取出来
docCnt None
# 根据轮廓大小进行排序准备透视变换
cnts sorted(cnts, keycv2.contourArea, reverseTrue) # 将检测到的答题卡轮廓进行排序其实只有一个轮廓为了提高代码的可行性
for c in cnts:peri cv2.arcLength(c, True) # 计算答题卡区域轮廓的面积approx cv2.approxPolyDP(c, 0.02 * peri, True) # 轮廓近似if len(approx) 4:docCnt approxbreak# 执行透视变换
warped_t four_point_transform(image, docCnt.reshape(4, 2)) # 提前定义好的图像透视变换函数 four_point_transform
warped_new warped_t.copy()
cv_show(warped, warped_t)
# 转换为灰度图
warped cv2.cvtColor(warped_t, cv2.COLOR_BGR2GRAY)
# 转换为二值图片非黑即白
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, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[1]
warped_Contours cv2.drawContours(warped_t, cnts, -1, (0, 255, 0), 1)
cv_show(warped_Contours, warped_Contours)questionCnts [] # 用于存储正确的答案选项的轮廓
for c in cnts: # 遍历轮廓并计算比例和大小# 计算每个轮廓的外接矩阵的左上角坐标(x, y), 以及宽w高h的大小(x, y, w, h) cv2.boundingRect(c)ar w / float(h)# 根据实际情况制定标准if w 20 and h 20 and ar 0.9 and ar 1.1:questionCnts.append(c) 将每个答案的轮廓按照答题卡上的顺序正确排序后与正确答案进行比较
# 先按照从上到下排序
questionCnts sort_contours(questionCnts, methodtop-to-bottom)[0] # 提前定义好的轮廓排序函数 sort_contourscorrect 0
# 对每题的五个选项进行循环处理并与正确答案进行比较
for (q, i) in enumerate(np.arange(0, len(questionCnts), 5)):# 在按照从左到右排序cnts sort_contours(questionCnts[i:i 5])[0]bubbled None# 遍历每一个选项for (j, c) in enumerate(cnts):# 使用mask掩膜来判断结果mask np.zeros(thresh.shape, dtypeuint8)cv2.drawContours(mask, [c], -1, 255, -1)cv_show(mask, mask)# 通过计算非零点数量来算是否选择这个答案# 利用掩膜(mask)进行”与“操作只保留mask位置中的内容thresh_mask_and cv2.bitwise_and(thresh, thresh, maskmask)cv_show(thresh_mask_and, thresh_mask_and)total cv2.countNonZero(thresh_mask_and) # 统计像素值不为0的像素数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_new, [cnts[k]], -1, color, 3)cv_show(warped_new, warped_new) 统计得分并在答题卡的左上角写上分数
score (correct / 5.0) * 100
print([INFO] score: {}分.format(score))
cv2.putText(warped_new, {}.format(score), (10, 30),cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2)
cv2.imshow(Original, image)
cv2.imshow(Exam, warped_new)
cv2.waitKey(0)步骤解析 首先将每一题的正确答案的索引号与每一题的索引号对应上 以一个字典的形式保存在一个变量中 ANSWER_KEY {0: 1, 1: 4, 2: 0, 3: 3, 4: 1}1.图片预处理 图像边缘检测的介绍https://blog.csdn.net/weixin_73504499/article/details/141832066?spm1001.2014.3001.5501图像轮廓检测的介绍https://blog.csdn.net/weixin_73504499/article/details/141873522?spm1001.2014.3001.5501 image cv2.imread(images/test_01.png) # 读取答题卡图片
contours_img image.copy() # 复制一个原图为了后续的步骤在其上操作
gray cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) # 转换为灰度图高斯滤波 去除图片中的噪声点
blurred cv2.GaussianBlur(gray, (5, 5), 0)
cv_show(blurred, blurred) # 提前定义好的用于显示图片的函数 cv_show边缘检测
edged cv2.Canny(blurred, 75, 200)
cv_show(edged, edged)轮廓检测
cnts cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[1]
cv2.drawContours(contours_img, cnts, -1, (0, 0, 255), 3)
cv_show(contours_img, contours_img)效果如下 2.将答题卡区域提取出来图像透视变换并进行处理 透视变换的两个关键函数的解析https://blog.csdn.net/weixin_73504499/article/details/142864082?spm1001.2014.3001.5501 图像透视变换 -- 将答题卡区域提取出来
docCnt None
# 根据轮廓大小进行排序准备透视变换
cnts sorted(cnts, keycv2.contourArea, reverseTrue) # 将检测到的答题卡轮廓进行排序其实只有一个轮廓为了提高代码的可行性
for c in cnts:peri cv2.arcLength(c, True) # 计算答题卡区域轮廓的面积approx cv2.approxPolyDP(c, 0.02 * peri, True) # 轮廓近似if len(approx) 4:docCnt approxbreak# 执行透视变换
warped_t four_point_transform(image, docCnt.reshape(4, 2)) # 提前定义好的图像透视变换函数 four_point_transform
warped_new warped_t.copy()
cv_show(warped, warped_t)
# 转换为灰度图
warped cv2.cvtColor(warped_t, cv2.COLOR_BGR2GRAY)
# 转换为二值图片非黑即白
thresh cv2.threshold(warped, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
cv_show(thresh, thresh)
thresh_Contours thresh.copy()结果如下 3.对答题卡中的每一个答案选项区域进行处理 # 找到每一个圆圈轮廓
cnts cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[1]
warped_Contours cv2.drawContours(warped_t, cnts, -1, (0, 255, 0), 1)
cv_show(warped_Contours, warped_Contours)questionCnts [] # 用于存储正确的答案选项的轮廓
for c in cnts: # 遍历轮廓并计算比例和大小# 计算每个轮廓的外接矩阵的左上角坐标(x, y), 以及宽w高h的大小(x, y, w, h) cv2.boundingRect(c)ar w / float(h)# 根据实际情况制定标准if w 20 and h 20 and ar 0.9 and ar 1.1:questionCnts.append(c)结果如下 4.将每个答案的轮廓按照答题卡上的顺序正确排序后与正确答案进行比较 # 先按照从上到下排序
questionCnts sort_contours(questionCnts, methodtop-to-bottom)[0] # 提前定义好的轮廓排序函数 sort_contourscorrect 0
# 对每题的五个选项进行循环处理并与正确答案进行比较
for (q, i) in enumerate(np.arange(0, len(questionCnts), 5)):# 在按照从左到右排序cnts sort_contours(questionCnts[i:i 5])[0]bubbled None# 遍历每一个选项for (j, c) in enumerate(cnts):# 使用mask掩膜来判断结果mask np.zeros(thresh.shape, dtypeuint8)cv2.drawContours(mask, [c], -1, 255, -1)cv_show(mask, mask)# 通过计算非零点数量来算是否选择这个答案# 利用掩膜(mask)进行”与“操作只保留mask位置中的内容thresh_mask_and cv2.bitwise_and(thresh, thresh, maskmask)cv_show(thresh_mask_and, thresh_mask_and)total cv2.countNonZero(thresh_mask_and) # 统计像素值不为0的像素数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_new, [cnts[k]], -1, color, 3)cv_show(warped_new, warped_new)其中部分展示效果如下 结果如下 5.统计得分并在答题卡的左上角写上分数 统计得分并在答题卡的左上角写上分数
score (correct / 5.0) * 100
print([INFO] score: {}分.format(score))
cv2.putText(warped_new, {}.format(score), (10, 30),cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2)
cv2.imshow(Original, image)
cv2.imshow(Exam, warped_new)
cv2.waitKey(0)最终结果如下