接上一篇OpenCV+Python识别机读卡-CSDN博客,既然可以识别机读卡填涂答案了,将标准答案绘制到机读卡上也就简单了。
工作原理
1.答题区域为整张图片最大轮廓,先找出答题区域。
2.答题区域分为6行,每行4组,第6行只有1组,我们暂不处理第6行,只处理前面5行。
3.给定每一行第一个选项中心点坐标,该行其余选项的中心点坐标可以推算出来。
4.有了每个选项的中心点坐标,结合标准答案,就可以将答案绘制到机读卡上了。
实现步骤
1.空白机读卡长这样:
2.标准答案(内容仅供参考,以实际为准)
answers = {1: "B", 2: "B", 3: "A", 4: "B", 5: "B", 6: "A", 7: "B", 8: "B", 9: "A", 10: "B", 11: "B", 12: "A", 13: "A", 14: "B", 15: "B", 16: "A", 17: "A", 18: "B", 19: "B", 20: "A", 21: "C", 22: "C", 23: "B", 24: "D", 25: "C", 26: "D", 27: "A", 28: "D", 29: "C", 30: "B", 31: "C", 32: "D", 33: "B", 34: "A", 35: "D", 36: "C", 37: "B", 38: "B", 39: "D", 40: "D", 41: "B",42: "C", 43: "B", 44: "D", 45: "B", 46: "A", 47: "D", 48: "D", 49: "C", 50: "D", 51: "B", 52: "D", 53: "A", 54: "A", 55: "D", 56: "D", 57: "C", 58: "D", 59: "B", 60: "D", 61: "A", 62: "B", 63: "D", 64: "B", 65: "C", 66: "D", 67: "B", 68: "C", 69: "D", 70: "A", 71: "ABCD", 72: "ABCD", 73: "AC", 74: "BCD", 75: "BC", 76: "BD", 77: "ABCD", 78: "ABCD", 79: "ACD", 80: "ABD"}
有单选题和多选题。需要实现将答案绘制到空白机读卡上。
直接贴源码,详细说明请参考代码注释。
import cv2
# 1.读取图片并缩放
orginImg = cv2.imread("01.jpg")
size = ((int)(650*1.8), (int)(930*1.8)) # 尽可能将图片弄大一点,下面好处理
img = cv2.resize(orginImg, size)
# 显示图像
def imshow(name, image):
scale_percent = 50 # 缩放比例
width = int(image.shape[1] * scale_percent / 100)
height = int(image.shape[0] * scale_percent / 100)
dim = (width, height)
resized_image = cv2.resize(image, dim, interpolation=cv2.INTER_AREA)
cv2.imshow(name, resized_image)
imshow("1.orgin", img)
# 2.转灰度图
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
imshow("2.gray", gray)
# 3.黑帽运算:移除干扰项
cvblackhat = cv2.morphologyEx(
gray, cv2.MORPH_BLACKHAT, cv2.getStructuringElement(cv2.MORPH_RECT, (15, 15)))
imshow("3.black", cvblackhat)
# 4.二值化突出轮廓,自动阈值范围 cv2.THRESH_BINARY|cv2.THRESH_OTSU
thresh = cv2.threshold(
cvblackhat, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
imshow("4.thresh", thresh)
# 5.提取轮廓,并在图上标记轮廓
cnts, hierarchy = cv2.findContours(
thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
mark = img.copy()
cv2.drawContours(mark, cnts, -1, (0, 0, 255), 2)
imshow("5.contours", mark)
# 6.提取我们感兴趣的部分(这里我们只需要答题部分) img[y:y+h, x:x+w]
_top = 0
_left = 0
roi = None
for (i, c) in enumerate(cnts):
(x, y, w, h) = cv2.boundingRect(c)
ar = w/float(h)
if w > 500 and h > 500 and ar > 0.9 and ar < 1.1:
roi = img[y:y+h, x:x+w]
_top = y
_left = x
break
imshow("5.roi", roi)
# 7.查找每个选项的中心点坐标
# 思路:
# 通过分析:
# 1.答题区域分为6行,每行4组,第6行只有1组,我们暂不处理第6行,只处理前面5行。
# 2.只要给定每一行第一个选项中心坐标,该行其余选项的中心坐标可以推算出来。
# 3.通过找到每个选项中心点坐标,再加上选项宽高,就可以在答题区域绘出每个选项的范围。
# 4.通过计算每个选项范围图像里非0像素点个数,结合阈值判断该选项是否选中。
# 5.结合题目个数,遍历每个选项,构造出最终答案。
item = [34, 20] # 每个选项宽度(跟图形缩放有关系)
x_step = 44 # x方向行距(两个选项水平方向距离)
y_step = 28 # y方向行距(两个选项垂直方向距离)
blank = 92 # 每组间距(5个一组)水平方向距离
centers = [] # 每个选项的中心点坐标,用来框选选项
# 答题区域有5行多1组,这里只处理前面5行,最后一组暂不处理
startPonits = [(25, 44), (25, 216), (26, 392), (26, 566), (28, 744)]
for (i, p) in enumerate(startPonits):
temp = [] # 暂存该组选项坐标
start = list(p) # 该行起始点坐标
for g in range(0, 4, 1): # 每行有4组
if len(temp) > 0:
startx = temp[len(temp)-1][0] + blank # 最后一个选项的x坐标+每组间距
else:
startx = start[0]
start[0] = startx
for i in range(start[0], start[0]+5*x_step, x_step): # 水平5个选项
for j in range(start[1], start[1]+4*y_step, y_step): # 垂直4个选项
temp.append((i, j))
for (i, c) in enumerate(temp):
centers.append(c)
questions = [] # 二维数组:保存每个题目ABCD4个选项对应的中心点坐标
group = [] # 将点分组,每4个1组,对应每题的4个选项
for (i, (x, y)) in enumerate(centers):
group.append((x, y))
if (i+1) % 4 == 0:
questions.append(group)
group = []
def find_key_by_value(dictionary, value):
for key, val in dictionary.items():
if val == value:
return key
return None
# 标准答案
answers = {1: "B", 2: "B", 3: "A", 4: "B", 5: "B", 6: "A", 7: "B", 8: "B", 9: "A", 10: "B", 11: "B", 12: "A", 13: "A", 14: "B", 15: "B", 16: "A", 17: "A", 18: "B", 19: "B", 20: "A", 21: "C", 22: "C", 23: "B", 24: "D", 25: "C", 26: "D", 27: "A", 28: "D", 29: "C", 30: "B", 31: "C", 32: "D", 33: "B", 34: "A", 35: "D", 36: "C", 37: "B", 38: "B", 39: "D", 40: "D", 41: "B",
42: "C", 43: "B", 44: "D", 45: "B", 46: "A", 47: "D", 48: "D", 49: "C", 50: "D", 51: "B", 52: "D", 53: "A", 54: "A", 55: "D", 56: "D", 57: "C", 58: "D", 59: "B", 60: "D", 61: "A", 62: "B", 63: "D", 64: "B", 65: "C", 66: "D", 67: "B", 68: "C", 69: "D", 70: "A", 71: "ABCD", 72: "ABCD", 73: "AC", 74: "BCD", 75: "BC", 76: "BD", 77: "ABCD", 78: "ABCD", 79: "ACD", 80: "ABD"}
# 8.将答案绘制到答题卡上
option = {0: "A", 1: "B", 2: "C", 3: "D"}
show = img.copy()
for (i, q) in enumerate(questions):
if i < len(answers):
an = answers.get(i+1) # 根据题目号获取答案
if an is None:
continue
characters = [char for char in an] # 将答案拆分成字符列表,适配多选题
for char in characters:
index = find_key_by_value(option, char) # 根据答案查找对应选项的索引,标记对应选项
for (j, (x, y)) in enumerate(q):
if j == index:
left = _left + x-(int)(item[0]/2) # 中心点坐标-1/2*选项宽度+左侧距离
top = _top + y-(int)(item[1]/2) # 中心点坐标-1/2*选项宽度+顶部距离
# 绘制黑块遮挡选项
cv2.rectangle(show, (left, top), (left +
item[0], top + item[1]), (0, 0, 0), -1)
imshow("5.show", show)
cv2.waitKey(0)
cv2.destroyAllWindows()
运行效果
存在的缺陷:
1.这里每行起始点坐标和每个选项宽高,都是写的固定值(手动量出来的)。
2.不同机读卡需调整对应参数。