我需要为我的高中假期项目使用 Python 制作一个 OMR 检测系统(如果它足够可靠,学校可能会在某种程度上使用它),我已经对其进行了大量研究,并且已经尝试过从轮廓到模板匹配的所有内容,我觉得模板匹配工作正常,但它只能检测 OMR 表中的许多圆圈中的一个,有人可以帮我弄清楚如何检测多个(所有)圆圈(无论它们是否是冒泡与否),在 omr 表及其各自的坐标中,这对我来说就足够了。
我试过的:
import numpy as np
import cv2
img = cv2.resize(cv2.imread('assets/omr_match1.jpg', 0), (0, 0), fx=0.2, fy=0.5)
template = cv2.resize(cv2.imread('assets/circle.jpg', 0), (0, 0), fx=0.2, fy=0.5)
h, w = template.shape
methods = [cv2.TM_CCOEFF, cv2.TM_CCOEFF_NORMED, cv2.TM_CCORR,
cv2.TM_CCORR_NORMED, cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]
methods=methods[0]
# This is one among the above which works perfectly
for method in methods:
img2 = img.copy()
result = cv2.matchTemplate(img2, template, method)
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(result)
if method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
location = min_loc
else:
location = max_loc
bottom_right = (location[0] + w, location[1] + h)
cv2.rectangle(img2, location,bottom_right, 0, 1)
cv2.imshow('Match', img2)
cv2.waitKey(0)
cv2.destroyAllWindows()
见上文,仅标记了一个随机圆圈,而不是所有圆圈。