opencv实现答题卡识别


"""
识别答题卡
"""

import cv2
import numpy as np

def showImg(img_name, img):
cv2.imshow(img_name, img)
cv2.waitKey()
cv2.destroyAllWindows()

def get_max_rect(sorted_cnts):
for cnt in sorted_cnts:
# 轮廓近似
possible_cnts = []
epsilon = 0.1 * cv2.arcLength(cnt, True)
approx = cv2.approxPolyDP(cnt, epsilon, True)
if len(approx) == 4:
possible_cnts.append(cnt)
possible_cnts = sorted(possible_cnts, key=lambda x: cv2.arcLength(x, True))
return possible_cnts

def get_max_bounding_rect(possible_cnts):
# for cnt in possible_cnts:
# x, y, w, h = cv2.boundingRect(cnt)

sorted_cnts = sorted(possible_cnts, key=lambda cnt: cv2.boundingRect(cnt)[2]*cv2.boundingRect(cnt)[3], reverse=True)
print(sorted_cnts[0])

def show_countour(img, cnt):
img_copy = img.copy()
cv2.drawContours(img_copy, cnt, -1, (0,255, 0), 3)
showImg("img_copy", img_copy)


# 读取答题卡图片,并显示
answer_sheet_img = cv2.imread("t1.jpg")
print(answer_sheet_img.shape)
showImg("answer_sheet_img", answer_sheet_img)

# 高斯滤波,去除噪音
blur = cv2.GaussianBlur(answer_sheet_img,(5,5),0)
showImg("blur", blur)

# 图像转灰度值
sheet_gray = cv2.cvtColor(blur, cv2.COLOR_BGR2GRAY)
showImg("sheet_gray", sheet_gray)

# 二值化
retval, sheet_threshold = cv2.threshold(sh
  • 1
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
好的,下面是使用OpenCV实现识别的具体代码: ```python import cv2 import numpy as np # 读取图片 img = cv2.imread('test.png') # 将图片转换为灰度图像 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # 对灰度图像进行高斯模糊处理 blurred = cv2.GaussianBlur(gray, (5, 5), 0) # 对图像进行二值化处理 thresh = cv2.threshold(blurred, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1] # 对图像进行形态学处理 kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (21, 21)) closed = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel) # 查找轮廓 cnts = cv2.findContours(closed.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if len(cnts) == 2 else cnts[1] # 初始化案列表 questionCnts = [] # 遍历轮廓 for c in cnts: # 计算轮廓的面积和周长 area = cv2.contourArea(c) perimeter = cv2.arcLength(c, True) # 过滤掉面积和周长不符合要求的轮廓 if area > 5000 and perimeter < 1000: # 计算轮廓的近似多边形 approx = cv2.approxPolyDP(c, 0.05 * perimeter, True) # 如果轮廓是一个矩形,则将其添加到案列表中 if len(approx) == 4: questionCnts.append(approx) # 对案列表进行排序 questionCnts = sorted(questionCnts, key=lambda x: x[0][1]) # 遍历案列表 for (q, i) in enumerate(np.arange(0, len(questionCnts), 5)): # 对目矩形进行排序 cnts = sorted(questionCnts[i:i + 5], key=lambda x: x[0][0]) # 初始化案字典 bubbled = None # 遍历目矩形 for (j, c) in enumerate(cnts): # 计算目矩形的中心坐标 (x, y, w, h) = cv2.boundingRect(c) centerX = x + w // 2 centerY = y + h // 2 # 查找圆形区域 roi = thresh[centerY - 10:centerY + 10, centerX - 10:centerX + 10] total = cv2.countNonZero(roi) # 如果圆形区域的像素点个数大于阈值,则认为该目被选中 if bubbled is None or total > bubbled[0]: bubbled = (total, j) # 将案添加到案列表中 print("Question {}: {}".format(q + 1, chr(bubbled[1] + 65))) ``` 上述代码实现了对识别,并输出了识别结果。需要注意的是,在实际使用时,需要根据的具体情况进行参数的调整,以达到最佳的识别效果。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值