欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用?OpenCV-Python?識別答題卡判卷功能

 更新時間:2021年12月21日 15:08:03   作者:糖公子沒來過  
這篇文章主要介紹了使用?OpenCV-Python?識別答題卡判卷,本文分步驟通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下

任務

識別用相機拍下來的答題卡,并判斷最終得分(假設正確答案是B, E, A, D, B)

主要步驟

  1. 輪廓識別——答題卡邊緣識別
  2. 透視變換——提取答題卡主體
  3. 輪廓識別——識別出所有圓形選項,剔除無關輪廓
  4. 檢測每一行選擇的是哪一項,并將結果儲存起來,記錄正確的個數(shù)
  5. 計算最終得分并在圖中標注

分步實現(xiàn)

輪廓識別——答題卡邊緣識別

輸入圖像

import cv2 as cv
import numpy as np
 
# 正確答案
right_key = {0: 1, 1: 4, 2: 0, 3: 3, 4: 1}
 
# 輸入圖像
img = cv.imread('./images/test_01.jpg')
img_copy = img.copy()
img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cvshow('img-gray', img_gray)

圖像預處理

# 圖像預處理
# 高斯降噪
img_gaussian = cv.GaussianBlur(img_gray, (5, 5), 1)
cvshow('gaussianblur', img_gaussian)
# canny邊緣檢測
img_canny = cv.Canny(img_gaussian, 80, 150)
cvshow('canny', img_canny)

?

?

輪廓識別——答題卡邊緣識別

# 輪廓識別——答題卡邊緣識別
cnts, hierarchy = cv.findContours(img_canny, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
cv.drawContours(img_copy, cnts, -1, (0, 0, 255), 3)
cvshow('contours-show', img_copy)

透視變換——提取答題卡主體

對每個輪廓進行擬合,將多邊形輪廓變?yōu)樗倪呅?/p>

docCnt = None
 
# 確保檢測到了
if len(cnts) > 0:
    # 根據(jù)輪廓大小進行排序
    cnts = sorted(cnts, key=cv.contourArea, reverse=True)
 
    # 遍歷每一個輪廓
    for c in cnts:
        # 近似
        peri = cv.arcLength(c, True)
        # arclength 計算一段曲線的長度或者閉合曲線的周長;
        # 第一個參數(shù)輸入一個二維向量,第二個參數(shù)表示計算曲線是否閉合
 
        approx = cv.approxPolyDP(c, 0.02 * peri, True)
        # 用一條頂點較少的曲線/多邊形來近似曲線/多邊形,以使它們之間的距離<=指定的精度;
        # c是需要近似的曲線,0.02*peri是精度的最大值,True表示曲線是閉合的
 
        # 準備做透視變換
        if len(approx) == 4:
            docCnt = approx
            break

透視變換——提取答題卡主體

# 透視變換——提取答題卡主體
docCnt = docCnt.reshape(4, 2)
warped = four_point_transform(img_gray, docCnt)
cvshow('warped', warped)
def four_point_transform(img, four_points):
    rect = order_points(four_points)
    (tl, tr, br, bl) = rect
 
    # 計算輸入的w和h的值
    widthA = np.sqrt((tr[0] - tl[0]) ** 2 + (tr[1] - tl[1]) ** 2)
    widthB = np.sqrt((br[0] - bl[0]) ** 2 + (br[1] - bl[1]) ** 2)
    maxWidth = max(int(widthA), int(widthB))
 
    heightA = np.sqrt((tl[0] - bl[0]) ** 2 + (tl[1] - bl[1]) ** 2)
    heightB = np.sqrt((tr[0] - br[0]) ** 2 + (tr[1] - br[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')
 
    # 最主要的函數(shù)就是 cv2.getPerspectiveTransform(rect, dst) 和 cv2.warpPerspective(image, M, (maxWidth, maxHeight))
    M = cv.getPerspectiveTransform(rect, dst)
    warped = cv.warpPerspective(img, M, (maxWidth, maxHeight))
    return warped
 
 
def order_points(points):
    res = np.zeros((4, 2), dtype='float32')
    # 按照從前往后0,1,2,3分別表示左上、右上、右下、左下的順序將points中的數(shù)填入res中
 
    # 將四個坐標x與y相加,和最大的那個是右下角的坐標,最小的那個是左上角的坐標
    sum_hang = points.sum(axis=1)
    res[0] = points[np.argmin(sum_hang)]
    res[2] = points[np.argmax(sum_hang)]
 
    # 計算坐標x與y的離散插值np.diff()
    diff = np.diff(points, axis=1)
    res[1] = points[np.argmin(diff)]
    res[3] = points[np.argmax(diff)]
 
    # 返回result
    return res

輪廓識別——識別出選項

# 輪廓識別——識別出選項
thresh = cv.threshold(warped, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)[1]
cvshow('thresh', thresh)
thresh_cnts, _ = cv.findContours(thresh, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
w_copy = warped.copy()
cv.drawContours(w_copy, thresh_cnts, -1, (0, 0, 255), 2)
cvshow('warped_contours', w_copy)
 
questionCnts = []
# 遍歷,挑出選項的cnts
for c in thresh_cnts:
    (x, y, w, h) = cv.boundingRect(c)
    ar = w / float(h)
    # 根據(jù)實際情況指定標準
    if w >= 20 and h >= 20 and ar >= 0.9 and ar <= 1.1:
        questionCnts.append(c)
 
# 檢查是否挑出了選項
w_copy2 = warped.copy()
cv.drawContours(w_copy2, questionCnts, -1, (0, 0, 255), 2)
cvshow('questionCnts', w_copy2)

成功將無關輪廓剔除

檢測每一行選擇的是哪一項,并將結果儲存起來,記錄正確的個數(shù)

# 檢測每一行選擇的是哪一項,并將結果儲存在元組bubble中,記錄正確的個數(shù)correct
# 按照從上到下t2b對輪廓進行排序
questionCnts = sort_contours(questionCnts, method="t2b")[0]
correct = 0
# 每行有5個選項
for (i, q) in enumerate(np.arange(0, len(questionCnts), 5)):
    # 排序
    cnts = sort_contours(questionCnts[q:q+5])[0]
 
    bubble = None
    # 得到每一個選項的mask并填充,與正確答案進行按位與操作獲得重合點數(shù)
    for (j, c) in enumerate(cnts):
        mask = np.zeros(thresh.shape, dtype='uint8')
        cv.drawContours(mask, [c], -1, 255, -1)
        # cvshow('mask', mask)
 
        # 通過按位與操作得到thresh與mask重合部分的像素數(shù)量
        bitand = cv.bitwise_and(thresh, thresh, mask=mask)
        totalPixel = cv.countNonZero(bitand)
 
        if bubble is None or bubble[0] < totalPixel:
            bubble = (totalPixel, j)
 
    k = bubble[1]
    color = (0, 0, 255)
    if k == right_key[i]:
        correct += 1
        color = (0, 255, 0)
 
    # 繪圖
    cv.drawContours(warped, [cnts[right_key[i]]], -1, color, 3)
    cvshow('final', warped)
def sort_contours(contours, method="l2r"):
    # 用于給輪廓排序,l2r, r2l, t2b, b2t
    reverse = False
    i = 0
    if method == "r2l" or method == "b2t":
        reverse = True
    if method == "t2b" or method == "b2t":
        i = 1
 
    boundingBoxes = [cv.boundingRect(c) for c in contours]
    (contours, boundingBoxes) = zip(*sorted(zip(contours, boundingBoxes), key=lambda a: a[1][i], reverse=reverse))
    return contours, boundingBoxes

?

用透過mask的像素的個數(shù)來判斷考生選擇的是哪個選項

計算最終得分并在圖中標注

# 計算最終得分并在圖中標注
score = (correct / 5.0) * 100
print(f"Score: {score}%")
cv.putText(warped, f"Score: {score}%", (10, 30), cv.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2)
cv.imshow("Original", img)
cv.imshow("Exam", warped)
cv.waitKey(0)

完整代碼

import cv2 as cv
import numpy as np
 
 
def cvshow(name, img):
    cv.imshow(name, img)
    cv.waitKey(0)
    cv.destroyAllWindows()
 
def four_point_transform(img, four_points):
    rect = order_points(four_points)
    (tl, tr, br, bl) = rect
 
    # 計算輸入的w和h的值
    widthA = np.sqrt((tr[0] - tl[0]) ** 2 + (tr[1] - tl[1]) ** 2)
    widthB = np.sqrt((br[0] - bl[0]) ** 2 + (br[1] - bl[1]) ** 2)
    maxWidth = max(int(widthA), int(widthB))
 
    heightA = np.sqrt((tl[0] - bl[0]) ** 2 + (tl[1] - bl[1]) ** 2)
    heightB = np.sqrt((tr[0] - br[0]) ** 2 + (tr[1] - br[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')
 
    # 最主要的函數(shù)就是 cv2.getPerspectiveTransform(rect, dst) 和 cv2.warpPerspective(image, M, (maxWidth, maxHeight))
    M = cv.getPerspectiveTransform(rect, dst)
    warped = cv.warpPerspective(img, M, (maxWidth, maxHeight))
    return warped
 
 
def order_points(points):
    res = np.zeros((4, 2), dtype='float32')
    # 按照從前往后0,1,2,3分別表示左上、右上、右下、左下的順序將points中的數(shù)填入res中
 
    # 將四個坐標x與y相加,和最大的那個是右下角的坐標,最小的那個是左上角的坐標
    sum_hang = points.sum(axis=1)
    res[0] = points[np.argmin(sum_hang)]
    res[2] = points[np.argmax(sum_hang)]
 
    # 計算坐標x與y的離散插值np.diff()
    diff = np.diff(points, axis=1)
    res[1] = points[np.argmin(diff)]
    res[3] = points[np.argmax(diff)]
 
    # 返回result
    return res
 
 
def sort_contours(contours, method="l2r"):
    # 用于給輪廓排序,l2r, r2l, t2b, b2t
    reverse = False
    i = 0
    if method == "r2l" or method == "b2t":
        reverse = True
    if method == "t2b" or method == "b2t":
        i = 1
 
    boundingBoxes = [cv.boundingRect(c) for c in contours]
    (contours, boundingBoxes) = zip(*sorted(zip(contours, boundingBoxes), key=lambda a: a[1][i], reverse=reverse))
    return contours, boundingBoxes
 
# 正確答案
right_key = {0: 1, 1: 4, 2: 0, 3: 3, 4: 1}
 
# 輸入圖像
img = cv.imread('./images/test_01.jpg')
img_copy = img.copy()
img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cvshow('img-gray', img_gray)
 
# 圖像預處理
# 高斯降噪
img_gaussian = cv.GaussianBlur(img_gray, (5, 5), 1)
cvshow('gaussianblur', img_gaussian)
# canny邊緣檢測
img_canny = cv.Canny(img_gaussian, 80, 150)
cvshow('canny', img_canny)
 
# 輪廓識別——答題卡邊緣識別
cnts, hierarchy = cv.findContours(img_canny, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
cv.drawContours(img_copy, cnts, -1, (0, 0, 255), 3)
cvshow('contours-show', img_copy)
 
docCnt = None
 
# 確保檢測到了
if len(cnts) > 0:
    # 根據(jù)輪廓大小進行排序
    cnts = sorted(cnts, key=cv.contourArea, reverse=True)
 
    # 遍歷每一個輪廓
    for c in cnts:
        # 近似
        peri = cv.arcLength(c, True)  # arclength 計算一段曲線的長度或者閉合曲線的周長;
        # 第一個參數(shù)輸入一個二維向量,第二個參數(shù)表示計算曲線是否閉合
 
        approx = cv.approxPolyDP(c, 0.02 * peri, True)
        # 用一條頂點較少的曲線/多邊形來近似曲線/多邊形,以使它們之間的距離<=指定的精度;
        # c是需要近似的曲線,0.02*peri是精度的最大值,True表示曲線是閉合的
 
        # 準備做透視變換
        if len(approx) == 4:
            docCnt = approx
            break
 
 
# 透視變換——提取答題卡主體
docCnt = docCnt.reshape(4, 2)
warped = four_point_transform(img_gray, docCnt)
cvshow('warped', warped)
 
 
# 輪廓識別——識別出選項
thresh = cv.threshold(warped, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)[1]
cvshow('thresh', thresh)
thresh_cnts, _ = cv.findContours(thresh, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
w_copy = warped.copy()
cv.drawContours(w_copy, thresh_cnts, -1, (0, 0, 255), 2)
cvshow('warped_contours', w_copy)
 
questionCnts = []
# 遍歷,挑出選項的cnts
for c in thresh_cnts:
    (x, y, w, h) = cv.boundingRect(c)
    ar = w / float(h)
    # 根據(jù)實際情況指定標準
    if w >= 20 and h >= 20 and ar >= 0.9 and ar <= 1.1:
        questionCnts.append(c)
 
# 檢查是否挑出了選項
w_copy2 = warped.copy()
cv.drawContours(w_copy2, questionCnts, -1, (0, 0, 255), 2)
cvshow('questionCnts', w_copy2)
 
 
# 檢測每一行選擇的是哪一項,并將結果儲存在元組bubble中,記錄正確的個數(shù)correct
# 按照從上到下t2b對輪廓進行排序
questionCnts = sort_contours(questionCnts, method="t2b")[0]
correct = 0
# 每行有5個選項
for (i, q) in enumerate(np.arange(0, len(questionCnts), 5)):
    # 排序
    cnts = sort_contours(questionCnts[q:q+5])[0]
 
    bubble = None
    # 得到每一個選項的mask并填充,與正確答案進行按位與操作獲得重合點數(shù)
    for (j, c) in enumerate(cnts):
        mask = np.zeros(thresh.shape, dtype='uint8')
        cv.drawContours(mask, [c], -1, 255, -1)
        cvshow('mask', mask)
 
        # 通過按位與操作得到thresh與mask重合部分的像素數(shù)量
        bitand = cv.bitwise_and(thresh, thresh, mask=mask)
        totalPixel = cv.countNonZero(bitand)
 
        if bubble is None or bubble[0] < totalPixel:
            bubble = (totalPixel, j)
 
    k = bubble[1]
    color = (0, 0, 255)
    if k == right_key[i]:
        correct += 1
        color = (0, 255, 0)
 
    # 繪圖
    cv.drawContours(warped, [cnts[right_key[i]]], -1, color, 3)
    cvshow('final', warped)
 
 
# 計算最終得分并在圖中標注
score = (correct / 5.0) * 100
print(f"Score: {score}%")
cv.putText(warped, f"Score: {score}%", (10, 30), cv.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2)
cv.imshow("Original", img)
cv.imshow("Exam", warped)
cv.waitKey(0)
 
 

到此這篇關于使用?OpenCV-Python?識別答題卡判卷的文章就介紹到這了,更多相關OpenCV?Python?識別答題卡判卷內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • Python 50行爬蟲抓取并處理圖靈書目過程詳解

    Python 50行爬蟲抓取并處理圖靈書目過程詳解

    這篇文章主要介紹了Python 50行爬蟲抓取并處理圖靈書目過程詳解,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-09-09
  • 詳解python的幾種標準輸出重定向方式

    詳解python的幾種標準輸出重定向方式

    這篇文章是基于Python2.7版本,介紹常見的幾種標準輸出(stdout)重定向方式。顯然,這些方式也適用于標準錯誤重定向。學習python的小伙伴們可以參考借鑒。
    2016-08-08
  • Python中的異常類型及處理方式示例詳解

    Python中的異常類型及處理方式示例詳解

    今天我們主要來了解一下 Python 中的異常類型以及它們的處理方式。說到異常處理,我們首先要知道什么是異常。其實,異常就是一類事件,當它們發(fā)生時,會影響到程序的正常執(zhí)行,具體內(nèi)容跟隨小編一起看看吧
    2021-08-08
  • 如何使用Python腳本實現(xiàn)文件拷貝

    如何使用Python腳本實現(xiàn)文件拷貝

    這篇文章主要介紹了如何使用Python腳本實現(xiàn)文件拷貝,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-11-11
  • Windows下python3.7安裝教程

    Windows下python3.7安裝教程

    這篇文章主要為大家詳細介紹了Windows下python3.7安裝教程,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-07-07
  • Python字典“鍵”和“值”的排序5種方法

    Python字典“鍵”和“值”的排序5種方法

    這篇文章主要介紹了5種Python字典“鍵”和“值”的排序方法,本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-03-03
  • Python實現(xiàn)的二維碼生成小軟件

    Python實現(xiàn)的二維碼生成小軟件

    這篇文章主要介紹了Python實現(xiàn)的二維碼生成小軟件,使用wxPython、python-qrcode、pyqrcode、pyqrnative等技術和開源類庫實現(xiàn),需要的朋友可以參考下
    2014-07-07
  • Python常用爬蟲代碼總結方便查詢

    Python常用爬蟲代碼總結方便查詢

    今天小編就為大家分享一篇關于Python常用爬蟲代碼總結方便查詢,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-02-02
  • django如何自己創(chuàng)建一個中間件

    django如何自己創(chuàng)建一個中間件

    這篇文章主要介紹了django如何自己創(chuàng)建一個中間件,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-07-07
  • Pandas缺失值填充 df.fillna()的實現(xiàn)

    Pandas缺失值填充 df.fillna()的實現(xiàn)

    本文主要介紹了Pandas缺失值填充 df.fillna()的實現(xiàn),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-07-07

最新評論