opencv实现答题卡识别

 更新时间:2022年01月23日 17:49:23   作者:qq_36008031  
这篇文章主要为大家详细介绍了opencv实现答题卡识别,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了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(sheet_gray,177, 255, cv2.THRESH_BINARY)
# print(type(sheet_threshold), sheet_threshold)
showImg("sheet_threshold", sheet_threshold)
 
# 边界检测
edges = cv2.Canny(sheet_threshold, 100, 200)
showImg("edges", edges)
# print(type(edges))
 
# 寻找轮廓
copy_edges = edges.copy()
img_copy = answer_sheet_img.copy()
img, cnts, hierarchy = cv2.findContours(copy_edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(img_copy, cnts, -1, (0,0,255), 1)
showImg("img_copy", img_copy)
 
# 对所有轮廓加一个外接矩形,找最大的外接矩形
max_area_index = None
area = 0
for index, cnt in enumerate(cnts):
x, y, w, h = cv2.boundingRect(cnt)
if w*h > area:
max_area_index = index
show_countour(answer_sheet_img, cnts[max_area_index])
 
# 仿射,拿到答题卡主要部位
x, y, w, h = cv2.boundingRect(cnts[max_area_index]) # 最大的边界
cv2.rectangle(answer_sheet_img, (x, y),(x+w, y+h), (0,0,255), 2)
showImg("answer_sheet_img", answer_sheet_img)
pts1 = np.float32([[x,y], [x+w, y], [x+w, y+h]])
pts2 = np.float32([[0,0], [w, 0], [w, h]])
 
M = cv2.getAffineTransform(pts1, pts2)
sheet_threshold_copy = sheet_threshold.copy()
dst = cv2.warpAffine(sheet_threshold_copy, M, (w, h))
showImg("dst", dst)
print(answer_sheet_img.shape)
part_sheet_img = answer_sheet_img[y:y+h, x:x+w]
showImg("part_sheet_img", part_sheet_img)
 
# 对答案区域灰度,二值,找轮廓
part_answer_gray = cv2.cvtColor(part_sheet_img, cv2.COLOR_BGR2GRAY) # 灰度
ret, threshold_answer = cv2.threshold(part_answer_gray, 175, 255, cv2.THRESH_BINARY)
showImg("threshold_answer", threshold_answer)
 
img, answer_cnts, x = cv2.findContours(threshold_answer, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
part_sheet_img_copy = part_sheet_img.copy()
cv2.drawContours(part_sheet_img_copy, answer_cnts, -1, (0, 0, 255), 1)
showImg("dst_copy", part_sheet_img_copy)
 
# 对所有轮廓找外接矩形,想过滤掉不合适的矩形
print("画矩形")
answer_filter_cnts = []
answer_circles = []
img_ = part_sheet_img.copy()
for cnt in answer_cnts:
x, y, w, h = cv2.boundingRect(cnt)
if 30<w<40 and 30<h<40:
print(x, y, w, h)
circle_x = int(x + w/2)
circle_y = int(y+h/2)
r = int((w+h)/4)
answer_circles.append((circle_x, circle_y, r))
answer_filter_cnts.append(cnt)
 
answer_filter_cnts = np.array(answer_filter_cnts)
cv2.drawContours(img_, answer_filter_cnts, -1, (0, 0, 255), 1)
# cv2.rectangle(img, (x, y), (x+w, y+h), (0,255,0), 2)
showImg("img_", img_)
print("geshu", len(answer_circles))
 
 
# 从answer_circles中取25个
mask_dict = {1:[],2:[], 3:[], 4:[],5:[]} # 一共不一定是25个圆,将圆按照题目行分类,
sorted_y_answer_circles = sorted(answer_circles, key=lambda circle: circle[1])
print("sorted_y_answer_circles", sorted_y_answer_circles)
set_num = 1
for index, circle in enumerate(sorted_y_answer_circles):
if index == 0:
mask_dict[1].append(circle)
else:
if circle[1] - sorted_y_answer_circles[index-1][1] > 30:
set_num += 1
mask_dict[set_num].append(circle)
else:
mask_dict[set_num].append(circle)
 
print("mask_dict", mask_dict)
 
for k, mask_circle_list in mask_dict.items(): # 对每一个题目,保留五个答案,多余的舍去
if len(mask_circle_list) == 5:
sorted_x_mask_circle_list = sorted(mask_circle_list, key=lambda x:x[0])
mask_dict[k]=sorted_x_mask_circle_list
else:
sorted_x_mask_circle_list = sorted(mask_circle_list, key=lambda x: x[0])
sorted_x_mask_circle_list_5 = []
for i, c in enumerate(sorted_x_mask_circle_list):
if i == 0:
sorted_x_mask_circle_list_5.append(c)
else:
if abs(c[0] - sorted_x_mask_circle_list[i-1][0]) < 10:
pass
else:
sorted_x_mask_circle_list_5.append(c)
mask_dict[k] = sorted_x_mask_circle_list_5
 
print("mask_dict", mask_dict)
 
# mask_dict 分好组的按照顺序的圈圈
 
# 做掩码
mask_img = np.zeros_like(part_sheet_img, dtype='uint8') # 全黑图
showImg("threshold_answer", threshold_answer)
threshold_answer = np.array(threshold_answer)
# mask_dict = sorted(mask_dict, key=lambda x: mask_dict.keys())
all_scores = [] # 所有答案处的评分
for exercise_num, circle_mask_list in mask_dict.items():
# 对于每一题
score_list = [] # 每一题的每个选项的评分,涂黑的为选择的,值越接近0, 评分较低
for circle_mask in circle_mask_list:
mask_img_copy = cv2.cvtColor(mask_img, cv2.COLOR_BGR2GRAY)
# 做一个当前圆的掩码:
cv2.circle(mask_img_copy, (circle_mask[0], circle_mask[1]), circle_mask[2], (255, 255, 255), -1)
print(threshold_answer.shape, mask_img_copy.shape)
mask_img_ = cv2.bitwise_and(threshold_answer, threshold_answer, mask=mask_img_copy)
score = mask_img_.sum()
score_list.append(score)
# showImg("mask_img_", mask_img_)
all_scores.append(score_list)
 
 
all_score_np = np.array(all_scores)
s = np.argmin(all_score_np, axis=1) # 找评分最低处即为选择项
 
answer_dict = {
0: "A",
1: "B",
2: "C",
3: "D",
4: "E"
}
 
for index, v in enumerate(s):
print("第%s题的答案是%s" %(index+1, answer_dict[v]))

效果图:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • numpy中meshgrid和mgrid的区别和使用详解

    numpy中meshgrid和mgrid的区别和使用详解

    本文主要介绍了numpy中meshgrid和mgrid的区别和使用详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-02-02
  • 深入理解pytorch库的dockerfile

    深入理解pytorch库的dockerfile

    这篇文章主要介绍了pytorch库的dockerfile,主要包括dockerfile命令,使用指令的注意点及存在的一些问题,本文给大家介绍的非常详细,需要的朋友可以参考下
    2022-06-06
  • 如何利用Python动态展示排序算法

    如何利用Python动态展示排序算法

    Python是一种简单易学,功能强大的编程语言,它有高效率的高层数据结构,能够简单、有效地实现面向对象编程,下面这篇文章主要给大家介绍了关于如何利用Python动态展示排序算法的相关资料,需要的朋友可以参考下
    2021-10-10
  • python实现Excel多行多列的转换的示例

    python实现Excel多行多列的转换的示例

    本文主要介绍了python实现Excel多行多列的转换的示例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2023-03-03
  • python统计RGB图片某像素的个数案例

    python统计RGB图片某像素的个数案例

    这篇文章主要介绍了python统计RGB图片某像素的个数案例,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2021-03-03
  • python合并文本文件示例

    python合并文本文件示例

    这篇文章主要介绍了python合并文本文件示例,需要的朋友可以参考下
    2014-02-02
  • 理解Python中的With语句

    理解Python中的With语句

    这篇文章主要介绍了理解Python中的With语句,本文讲解了With语句是什么、with如何工作等内容,并给出了代码实例,需要的朋友可以参考下
    2015-02-02
  • Python flask-script 模块详解

    Python flask-script 模块详解

    Flask Script扩展提供向Flask插入外部脚本的功能,这篇文章主要介绍了Flask之flask-script模块使用,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2021-11-11
  • 利用Python实现自动工作汇报的脚本分享

    利用Python实现自动工作汇报的脚本分享

    这篇文章主要为大家详细介绍了如何利用Python实现一个自动工作汇报的脚本,文中的示例代码讲解详细,对我们学习Python有一定帮助,需要的可以参考一下
    2022-08-08
  • 在Python 3中缓存Exception对象会造成什么后果?

    在Python 3中缓存Exception对象会造成什么后果?

    这篇文章主要介绍了在Python 3中缓存Exception对象到底会造成什么后果?下面带着这个问题一起看看文章的解析,需要的朋友可以参考一下
    2021-12-12

最新评论