【进阶OpenCV】 (9)--摄像头操作--->答题卡识别改分项目

发布于:2024-10-13 ⋅ 阅读:(7) ⋅ 点赞:(0)

项目:答题卡识别改分

本篇我们来介绍,如何识别一张答题卡,并为其答案验证对错,进行打分。

在这里插入图片描述

思路

  1. 图片预处理
  2. 边缘检测
  3. 描绘轮廓
  4. 找到每一个圆圈轮廓
  5. 比对答案
  6. 计算正确率

1. 图片预处理

  1. 对识别的图片进行灰度处理
  2. 使用高斯滤波cv2.GaussianBlur()函数平滑处理去噪
  3. 接着使用cv2.Canny()函数再进行边缘检测,检测图片中的边缘。
def cv_show(name,img):
    cv2.imshow(name,img)
    cv2.waitKey(0)
"""-----预处理-----"""
image = cv2.imread(r'./images/test_01.png')
contours_img = image.copy()
gray = cv2.cvtColor(image,cv2.COLOR_RGB2GRAY)
blurred = cv2.GaussianBlur(gray,(5,5),0)
cv_show('blurred',blurred)
edged = cv2.Canny(blurred,75,200)
cv_show('edged',edged)

在这里插入图片描述

2. 描绘轮廓

使用cv2.findContours()函数查找图像的轮廓,再通过cv2.drawContours()函数将其绘制出来:

"""-----轮廓检测-----"""
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)

在这里插入图片描述

3. 轮廓近似

通过轮廓近似找到答题卡,为接下来的透视变换做准备:

判断条件:当近似轮廓可以用四个点绘制出时(因为答题卡是长方形的),将其保留。

"""-----根据轮廓大小进行排序,准备透视变换-----"""
cnts = sorted(cnts,key=cv2.contourArea,reverse=True)
docCnt = None
for c in cnts: # 遍历每一个轮廓
    peri = cv2.arcLength(c,True)
    approx = cv2.approxPolyDP(c,0.02 * peri,True) #轮廓近似

    if len(approx) == 4:
        docCnt = approx
        break

通过以上代码,得到答题卡的四个角点坐标docCnt

4. 透视变换

定义两个函数:

  • order_point(pts):将给定的四个点(通常是从图像中检测到的轮廓点或角点)按照特定的顺序排列:左上角(tl)、右上角(tr)、右下角(br)和左下角(bl)。
  • four_point_transform(image,pts):这个函数使用 order_point 函数得到的点来对输入图像进行透视变换,使得这四个点映射到一个矩形上。
def order_point(pts):
    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):
    # 获取输入坐标点
    rect = order_point(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")

    M = cv2.getPerspectiveTransform(rect,dst)
    warped = cv2.warpPerspective(image,M,(maxwidth,maxheight))
    # 返回变化后结果
    return warped
"""-----执行透视变换-----"""
warped_t = four_point_transform(image,docCnt.reshape(4,2))
warped_new = warped_t.copy()
cv_show('warped',warped_t)
warped = cv2.cvtColor(warped_t,cv2.COLOR_BGR2GRAY)

在这里插入图片描述

这样就将答题卡完整的取出来了。

5. 阈值处理

非黑即白,使得到的答题卡更清晰。

"""-----阈值处理-----"""
thresh = cv2.threshold(warped,0,255,
                       cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
cv_show('thresh',thresh)
thresh_Contours = thresh.copy()

在这里插入图片描述

6. 找每一个圆圈轮廓

  1. 通过cv2.findContours()函数,查找轮廓,并将其绘制出来。
  2. 遍历每一个轮廓,将符合条件的圆圈轮廓存放进questionCnts列表中。
"""-----找到每一个圆圈轮廓-----"""
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) = cv2.boundingRect(c)
    ar = w/float(h)
    # 根据实际情况指定标准
    if w >= 20 and h >= 20 and ar >= 0.9 and ar <= 1.1:
        questionCnts.append(c)

在这里插入图片描述

7. 将每一个圆圈轮廓排序

定义了一个名为 sort_contours的函数,用于根据指定的方法对轮廓(contours)进行排序:

功能:这个函数可以用于在图像处理中,根据对象的水平或垂直位置对检测到的对象进行排序,这在处理图像中的多个对象时非常有用。

def sort_contours(cnts ,method='left-to-right'):
    reverse=False
    i=0
    if method=='right-to-left' or method=='bottom-to-top':
        reverse=True

    if method=='top-to-bottom' or method=='bottom-to-top':
        i=1
    boundingBoxes=[cv2.boundingRect(c) for c in cnts]
    (cnts,boundingBoxes)=zip(*sorted(zip(cnts,boundingBoxes),
                                     key=lambda b:b[1][i],reverse=reverse))#zip(*...)使用星号操作符解包排序后的元组列表,并将其重新组合成两个列表:一个包含所有轮廓,另一个包含所有边界框。
    return cnts,boundingBoxes
questionCnts = sort_contours(questionCnts,method = "top-to-bottom")[0]

8. 找寻所填答案,比对正确答案

8.1 思路

  • 思路:我们发现,答题卡中,每一行共五个圆圈代表一道题的答案,我们要找到填涂的圆圈,然后再找到正确答案的圆圈,比对二者是不是同一个。

    所以我们要从左到右每五个圆圈轮廓寻找一次填涂答案的位置,然后比较一次。那么,我们怎么从五个轮廓中找到填涂的那个呢?

    通过cv2.countNonZero()函数方法,作用是计算数组(通常是图像或图像的一部分)中非零元素(像素点)的数量。填涂后的轮廓位置轮廓内都是有颜色的,通过掩膜(对每一个轮廓进行掩膜)方法与每个轮廓做”与“运算,然后计算非零元素的数量,五个中,谁最多,谁就是填涂的。

    接着通过它在五个中的位置,与正确答案的位置作比较,看看一不一致,判断对错。

8.2 图解

  • 图解
  1. 第一个轮廓的掩膜”与“运算

在这里插入图片描述

得到一个清晰的轮廓,计算它的非零像素点的数量。

  1. 填涂点的掩膜”与”运算

在这里插入图片描述

对比上下两个轮廓的非零像素点的数量就可以明显锁定填涂位置。

8.3 代码体现

"""-----比对答案-----"""
ANSWER_KEY = {0:1,1:4,2:0,3:3,4:1} # 正确答案
correct = 0
# 每持有5个选项
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,dtype="uint8")
        cv2.drawContours(mask,[c],-1,255,-1)
        cv_show('mask',mask)
        # 通过计算非零点数量来算是否选择这个答案
        # 利用掩膜进行‘与’操作,只保留mask位置中的内容
        thresh_mask_and = cv2.bitwise_and(thresh,thresh,mask=mask)
        cv_show('thresh_mask_and',thresh_mask_and)

        total = cv2.countNonZero(thresh_mask_and)
        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("warpeding",warped_new)

判断结果:

在这里插入图片描述

9. 计算正确率

"""-----计算分数-----"""
score = (correct / 5.0) * 100
print("[INFO] score:{:.2f}%".format(score))
cv2.putText(warped_new,"{:.2f}%".format(score),(10,30),
            cv2.FONT_HERSHEY_SIMPLEX,0.9,(0,0,255),2)
cv2.imshow("origimal",image)
cv2.imshow('Exam',warped_new)
cv2.waitKey(0)
----------------
[INFO] score:80.00%

在这里插入图片描述

总结

本篇介绍了:如何对答题卡进行识别并计算准确率。

要点知识:边缘检测、轮廓近似、透视变换以及掩膜。

过程:1. 图片预处理 -----> 2. 描绘轮廓 -----> 3. 轮廓近似 -----> 4. 透视变换 -----> 5. 阈值处理 -----> 6. 找每一个圆圈轮廓 -----> 7. 将每一个圆圈轮廓排序 -----> 8. 比对正确答案 -----> 9. 计算正确率.