Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use OpenCV-Python to realize the function of identifying answer card and judging paper

Shulou Source: shulou.com Published: 2022-06-01 10:16:09 09月11日 Update

This article mainly shows you "how to use OpenCV-Python to achieve identification answer card marking function", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "how to use OpenCV-Python to achieve identification answer card marking function" this article.

Task

Identify the answer card taken with the camera and judge the final score (assuming the correct answer is B, E, A, D, B)

Main steps

Outline recognition-- Edge recognition of answer Card

Perspective transformation-- extracting the main body of the answer card

Outline recognition-identify all circle options and eliminate extraneous contours

Check which item is selected for each line, store the results, and record the correct number

Calculate the final score and mark it in the diagram

Step by step implementation

Outline recognition-- Edge recognition of answer Card

Input image

Import cv2 as cvimport numpy as np # correct answer right_key = {0: 1, 1: 4, 2: 0, 3: 3, 4: 1} # input image img = cv.imread ('. / images/test_01.jpg') img_copy = img.copy () img_gray = cv.cvtColor (img, cv.COLOR_BGR2GRAY) cvshow ('img-gray', img_gray)

Image preprocessing # Image preprocessing # Gaussian noise reduction img_gaussian = cv.GaussianBlur (img_gray, (5,5), 1) cvshow ('gaussianblur', img_gaussian) # canny Edge Detection img_canny = cv.Canny (img_gaussian, 80150) cvshow (' canny', img_canny)

Outline recognition-- Edge recognition of answer Card

# Contour recognition-answer card edge recognition 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)

Perspective transformation-- extracting the main body of the answer card

Fit each profile to change the polygon outline into a quadrilateral

DocCnt = None # ensure that if len (cnts) > 0: # sort by contour size cnts = sorted (cnts, key=cv.contourArea, reverse=True) # traverse each profile for c in cnts: # approximate peri = cv.arcLength (c, True) # arclength calculates the length of a curve or the perimeter of a closed curve # the first parameter inputs a two-dimensional vector, and the second parameter calculates whether the curve is closed approx = cv.approxPolyDP (c, 0.02 * peri, True) # approximate the curve / polygon with a curve / polygon with fewer vertices So that the distance between them = 20 and h > = 20 and ar > = 0.9 and ar 0: # sort by contour size cnts = sorted (cnts, key=cv.contourArea, reverse=True) # traverse each profile for c in cnts: # approximate peri = cv.arcLength (c, True) # arclength calculates the length of a curve or the perimeter of a closed curve # the first parameter inputs a two-dimensional vector, and the second parameter calculates whether the curve is closed approx = cv.approxPolyDP (c, 0.02 * peri, True) # approximate the curve / polygon with a curve / polygon with fewer vertices so that the distance between them = 20 and h > = 20 and ar > = 0.9 and ar

Tags: Contours answer cards curves coordinates sorting detection edges transformations input images number of polygons scores answers selection closure line body parameters results Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Docker Microsoft Shulou Information Xiaomi