【从小项目学图片处理】#1 答题卡识别

2023-11-19

说明:项目皆参考于网上,代码也有大部分参考原文。仅用于学习和练习图像处理操作。
项目原文: Bubble sheet multiple choice scanner and test grader using OMR, Python and OpenCV

问题描述:
给定下面一张答题卡,识别并依次输出被涂黑的字母 BEACB。
在这里插入图片描述
解决思路/步骤:

  • 1# 识别图像中答题卡部分,通过透视变换,将图像摆正。
  • 2# 识别并提取答题卡中圆形部分。
  • 3# 判断被涂黑的部分,输出被涂黑的字母。

代码实现:

  • 用c++实现
#include <opencv2/opencv.hpp>
#include <iostream>
using namespace cv;
using namespace std;

bool cmp1(Rect a, Rect b){
	return (a.y<=b.y);
}
bool cmp2(Rect a, Rect b){
	return (a.x<=b.x);
}

int main(int argc, char** argv){
	if(argc==1) {
		cout << "Usage: ProgramName PicturnFile" << endl;
		return -1;
	}
	Mat imgSrc = imread(argv[1]);	
	if(imgSrc.empty()) {
		cout << "Failed to load image!" << endl;
		return -1;
	}

	Mat img = Mat::zeros(imgSrc.size(),imgSrc.type());
	cvtColor(imgSrc, img, COLOR_BGR2GRAY);
	Mat gray = img.clone();
	GaussianBlur(img, img, Size(5,5), 0);
	Canny(img, img, 75, 200);

	vector<vector<Point>> contours;
	vector<Vec4i> hierarchy;
	findContours(img, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);

//	Mat test = Mat::zeros(img.size(), img.type());
//	drawContours(test, contours, -1, Scalar(255), 2);
	int k = 0;
	float area = 0;
	for(int i=0; i<contours.size(); i++){
		float Area = contourArea(contours[i]);
		if(area <= Area) {
			area = Area;
			k = i;
		}
	}
	
	vector<Point2f> p;
	double len;
	len = arcLength(contours[k], true);
	approxPolyDP(contours[k], p, len * 0.02, true);
	
	int height = max((int)sqrt((p[0].y-p[1].y)*(p[0].y-p[1].y)),(int)sqrt((p[2].y-p[3].y)*(p[2].y-p[3].y)));
	int width = max((int)sqrt((p[0].x-p[3].x)*(p[0].x-p[3].x)),(int)sqrt((p[2].x-p[1].x)*(p[2].x-p[1].x)));
	
	vector<Point2f> pdst = {Point2f(0,0), Point2f(0,width-1), Point2f(height-1,width-1), Point2f(height-1,0)};

	Mat m(3, 3, CV_32F);
	m = getPerspectiveTransform(p, pdst);
	warpPerspective(gray, img, m, Size(height, width));

	threshold(img, img, 0, 255, THRESH_BINARY_INV | THRESH_OTSU);
	contours.clear();
	findContours(img, contours, hierarchy, RETR_EXTERNAL, CHAIN_APPROX_SIMPLE);

	vector<Rect> rec;
	for(int i=0; i<contours.size(); i++){
		Rect rect = boundingRect(contours[i]);
		float ar = rect.width/(float)rect.height;
		if(rect.width >= 20 && rect.height >= 20 ){
			rec.push_back(rect);
		}
	}

	sort(rec.begin(), rec.end(), cmp1);
	for(vector<Rect>::iterator it=rec.begin(); it!=rec.end(); it+=5){
		sort(it, it+5, cmp2);
	}
	
	vector<int> ans(5,0);
	for(int i=0; i<5; i++){
		int maxcount = 0;
		for(int j=0; j<5; j++){
			Mat mask = Mat::zeros(img.size(),img.type());
			rectangle(mask, rec[i*5+j], Scalar(255), -1);
			bitwise_and(mask, img, mask);
			int count = countNonZero(mask);
			if(count >= maxcount){
				maxcount = count;
				ans[i] = j;	
			}
		}
	}
	imshow("img",img);
	waitKey(0);
	for(int i=0; i<ans.size(); i++){
		char out= 'A'+ans[i];
		cout << out;
	}

	return 0;
}
  • 用python实现(源项目代码)
# import the necessary packages
from imutils.perspective import four_point_transform
from imutils import contours
import numpy as np
import argparse
import imutils
import cv2
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
	help="path to the input image")
args = vars(ap.parse_args())
# define the answer key which maps the question number
# to the correct answer
ANSWER_KEY = {0: 1, 1: 4, 2: 0, 3: 3, 4: 1}
# load the image, convert it to grayscale, blur it
# slightly, then find edges
image = cv2.imread(args["image"])
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
edged = cv2.Canny(blurred, 75, 200)
# find contours in the edge map, then initialize
# the contour that corresponds to the document
cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL,
	cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
docCnt = None
# ensure that at least one contour was found
if len(cnts) > 0:
	# sort the contours according to their size in
	# descending order
	cnts = sorted(cnts, key=cv2.contourArea, reverse=True)
	# loop over the sorted contours
	for c in cnts:
		# approximate the contour
		peri = cv2.arcLength(c, True)
		approx = cv2.approxPolyDP(c, 0.02 * peri, True)
		# if our approximated contour has four points,
		# then we can assume we have found the paper
		if len(approx) == 4:
			docCnt = approx
			break
# apply a four point perspective transform to both the
# original image and grayscale image to obtain a top-down
# birds eye view of the paper
paper = four_point_transform(image, docCnt.reshape(4, 2))
warped = four_point_transform(gray, docCnt.reshape(4, 2))
# apply Otsu's thresholding method to binarize the warped
# piece of paper
thresh = cv2.threshold(warped, 0, 255,
	cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
# find contours in the thresholded image, then initialize
# the list of contours that correspond to questions
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
	cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
questionCnts = []
# loop over the contours
for c in cnts:
	# compute the bounding box of the contour, then use the
	# bounding box to derive the aspect ratio
	(x, y, w, h) = cv2.boundingRect(c)
	ar = w / float(h)
	# in order to label the contour as a question, region
	# should be sufficiently wide, sufficiently tall, and
	# have an aspect ratio approximately equal to 1
	if w >= 20 and h >= 20 and ar >= 0.9 and ar <= 1.1:
		questionCnts.append(c)
# sort the question contours top-to-bottom, then initialize
# the total number of correct answers
questionCnts = contours.sort_contours(questionCnts,
	method="top-to-bottom")[0]
correct = 0
# each question has 5 possible answers, to loop over the
# question in batches of 5
for (q, i) in enumerate(np.arange(0, len(questionCnts), 5)):
	# sort the contours for the current question from
	# left to right, then initialize the index of the
	# bubbled answer
	cnts = contours.sort_contours(questionCnts[i:i + 5])[0]
	bubbled = None
	# loop over the sorted contours
	for (j, c) in enumerate(cnts):
		# construct a mask that reveals only the current
		# "bubble" for the question
		mask = np.zeros(thresh.shape, dtype="uint8")
		cv2.drawContours(mask, [c], -1, 255, -1)
		# apply the mask to the thresholded image, then
		# count the number of non-zero pixels in the
		# bubble area
		mask = cv2.bitwise_and(thresh, thresh, mask=mask)
		total = cv2.countNonZero(mask)
		# if the current total has a larger number of total
		# non-zero pixels, then we are examining the currently
		# bubbled-in answer
		if bubbled is None or total > bubbled[0]:
			bubbled = (total, j)
	# initialize the contour color and the index of the
	# *correct* answer
	color = (0, 0, 255)
	k = ANSWER_KEY[q]
	# check to see if the bubbled answer is correct
	if k == bubbled[1]:
		color = (0, 255, 0)
		correct += 1
	# draw the outline of the correct answer on the test
	cv2.drawContours(paper, [cnts[k]], -1, color, 3)
# grab the test taker
score = (correct / 5.0) * 100
print("[INFO] score: {:.2f}%".format(score))
cv2.putText(paper, "{:.2f}%".format(score), (10, 30),
	cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 0, 255), 2)
cv2.imshow("Original", image)
cv2.imshow("Exam", paper)
cv2.waitKey(0)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

【从小项目学图片处理】#1 答题卡识别 的相关文章

  • Python3 在 DirectX 游戏中移动鼠标

    我正在尝试构建一个在 DirectX 游戏中执行一些操作的脚本 除了移动鼠标之外 我一切都正常 是否有任何可用的模块可以移动鼠标 适用于 Windows python 3 Thanks I used pynput https pypi or
  • 仅第一个加载的 Django 站点有效

    我最近向 stackoverflow 提交了一个问题 标题为使用mod wsgi在apache上多次请求后Django无限加载 https stackoverflow com questions 71705909 django infini
  • Github Action 在运行可执行文件时卡住

    我正在尝试设置运行google tests on a C repository using Github Actions正在运行的Windows Latest 构建过程完成 但是当运行测试时 它被卡住并且不执行从生成的可执行文件Visual
  • Pandas 将多行列数据帧转换为单行多列数据帧

    我的数据框如下 code df Car measurements Before After amb temp 30 268212 26 627491 engine temp 41 812730 39 254255 engine eff 15
  • 为什么 Pickle 协议 4 中的 Pickle 文件是协议 3 中的两倍,而速度却没有任何提升?

    我正在测试 Python 3 4 我注意到 pickle 模块有一个新协议 因此 我对 2 个协议进行了基准测试 def test1 pickle3 open pickle3 wb for i in range 1000000 pickle
  • Python:XML 内所有标签名称中的字符串替换(将连字符替换为下划线)

    我有一个格式不太好的 XML 标签名称内有连字符 我想用下划线替换它 以便能够与 lxml objectify 一起使用 我想替换所有标签名称 包括嵌套的子标签 示例 XML
  • 从库中捕获主线程 SynchronizationContext 或 Dispatcher

    我有一个 C 库 希望能够将工作发送 发布到 主 ui 线程 如果存在 该库可供以下人员使用 一个winforms应用程序 本机应用程序 带 UI 控制台应用程序 没有 UI 在库中 我想在初始化期间捕获一些东西 Synchronizati
  • 如何解决 PDFBox 没有 unicode 映射错误?

    我有一个现有的 PDF 文件 我想使用 python 脚本将其转换为 Excel 文件 目前正在使用PDFBox 但是存在多个类似以下错误 org apache pdfbox pdmodel font PDType0Font toUnico
  • 当操作繁忙时,表单不执行任何操作(冻结)

    我有一个使用 C 的 WinForms 应用程序 我尝试从文件中读取一些数据并将其插入数据表中 当此操作很忙时 我的表单冻结并且无法移动它 有谁知道我该如何解决这个问题 这可能是因为您在 UI 线程上执行了操作 将文件和数据库操作移至另一个
  • 在本地网络上运行 Bokeh 服务器

    我有一个简单的 Bokeh 应用程序 名为app py如下 contents of app py from bokeh client import push session from bokeh embed import server do
  • python import inside函数隐藏现有变量

    我在我正在处理的多子模块项目中遇到了一个奇怪的 UnboundLocalError 分配之前引用的局部变量 问题 并将其精简为这个片段 使用标准库中的日志记录模块 import logging def foo logging info fo
  • Python ImportError:无法导入名称 __init__.py

    我收到此错误 ImportError cannot import name life table from cdc life tables C Users tony OneDrive Documents Retirement retirem
  • 实体框架 4 DB 优先依赖注入?

    我更喜欢创建自己的数据库 设置索引 唯一约束等 使用 edmx 实体框架设计器 从数据库生成域模型是轻而易举的事 现在我有兴趣使用依赖注入来设置一些存储库 我查看了 StackOverflow 上的一些文章和帖子 似乎重点关注代码优先方法
  • 使用for循环时如何获取前一个元素? [复制]

    这个问题在这里已经有答案了 可能的重复 Python 循环内的上一个和下一个值 https stackoverflow com questions 1011938 python previous and next values inside
  • 如何应用一个函数 n 次? [关闭]

    Closed 这个问题需要细节或清晰度 help closed questions 目前不接受答案 假设我有一个函数 它接受一个参数并返回相同类型的结果 def increment x return x 1 如何制作高阶函数repeat可以
  • 如何计算Python中字典中最常见的前10个值

    我对 python 和一般编程都很陌生 所以请友善 我正在尝试分析包含音乐信息的 csv 文件并返回最常听的前 n 个乐队 从下面的代码中 每听一首歌曲都是一个列表中的字典条目 格式如下 album Exile on Main Street
  • C - 直接从键盘缓冲区读取

    这是C语言中的一个问题 如何直接读取键盘缓冲区中的数据 我想直接访问数据并将其存储在变量中 变量应该是什么数据类型 我需要它用于我们研究所目前正在开发的操作系统 它被称为 ICS OS 我不太清楚具体细节 它在 x86 32 位机器上运行
  • 32 位到 64 位内联汇编移植

    我有一段 C 代码 在 GNU Linux 环境下用 g 编译 它加载一个函数指针 它如何执行并不重要 使用一些内联汇编将一些参数推送到堆栈上 然后调用该函数 代码如下 unsigned long stack 1 23 33 43 save
  • 如何在 C++ BOOST 中像图形一样加载 TIFF 图像

    我想要加载一个 tiff 图像 带有带有浮点值的像素的 GEOTIFF 例如 boost C 中的图形 我是 C 的新手 我的目标是使用从源 A 到目标 B 的双向 Dijkstra 来获得更高的性能 Boost GIL load tiif
  • Kivy - 单击按钮时编辑标签

    我希望 Button1 在单击时编辑标签 etykietka 但我不知道如何操作 你有什么想法吗 class Zastepstwa App def build self lista WebOps getList layout BoxLayo

随机推荐

  • ESP32学习笔记05-串口事件方式读取数据

    串口中断方式处理数据 事件机构体 typedef struct uart event type t type lt UART event type size t size lt UART data size for UART DATA ev
  • leetcode 142.环形链表2

    leetcode 142 环形链表2 给定一个链表的头节点 head 返回链表开始入环的第一个节点 如果链表无环 则返回 null 如果链表中有某个节点 可以通过连续跟踪 next 指针再次到达 则链表中存在环 为了表示给定链表中的环 评测
  • VASP - Bader Charge Analysis

    Bader电荷分析 用于分析原子周围的电荷密度 从而得到原子价电子数 下载 Bader Charge Analysis 工具包 下载地址为 http theory cm utexas edu henkelman code bader 下载
  • C++中的friend详细解析

    https blog csdn net u012861978 article details 52095607
  • 奇偶数排序

    题目描述 给定一个整数数组 请调整数组的顺序 使得所有奇数位于数组前半部分 所有偶数位于数组后半部分 要求时间复杂度为O n 分析与解法 最容易想到的办法是从头到尾扫描这个数组 每遇到一个偶数 就把他单独取出来 然后把该偶数后面的所有数往前
  • vulnhub之vegeta

    目录 一 主机发现 二 端口扫描 四 信息收集 五 cyberchef解密 1 两道base64解密 2 save保存 3 qrcode module 4 二维码提取工具zbaring 六 大字典跑目录 七 bulma 八 音频分析软件au
  • spark-2.2.2-bin-hadoop2.7 安装

    1 上传spark 2 2 2 bin hadoop2 7 tgz 2 解压文件 tar zxvf spark 2 2 2 bin hadoop2 7 tgz C usr local 3 进入conf 下把spark env sh temp
  • 『NLP经典项目集』05:新年到,飞桨带你对对联

    基于seq2seq的对联生成 对联 是汉族传统文化之一 是写在纸 布上或刻在竹子 木头 柱子上的对偶语句 对联对仗工整 平仄协调 是一字一音的汉语独特的艺术形式 是中国传统文化瑰宝 这里 我们将根据上联 自动写下联 这是一个典型的序列到序列
  • Mysql 检查表是否存在并创建表,检查列是否存在并新增、修改、删除列

    判断表是否存在 不存在就可新增 CREATE TABLE IF NOT EXISTS example ENGINE InnoDB AUTO INCREMENT 1 DEFAULT CHARSET utf8 判断表字段是否存在 不存在就可新增
  • springboot好在哪

    springboot好在哪 欢迎观看 第一篇 欢迎观看 你好 这是本人第一次使用 新手发文 多多包涵 第一篇 以往的ssm框架整合通常有两种形式 一种是xml形式 一种是注解形式 不管是xml还是注解 基本都会有一大堆xml标签配置 其中有
  • C++学习之list的实现

    在了解学习list实现之前我们首先了解一下关于迭代器的分类 按功能分类 正向迭代器 反向迭代器 const正向迭代器 const反向迭代器 按性质分类 单向迭代器 只能 例如单链表 双向迭代器 可 也可 例如双链表 map和set 随机迭代
  • Unity3D 监控面板显示数据(Inspector)

    using System Collections using System Collections Generic using UnityEngine using UnityEngine Serialization AddComponent
  • 机器学习十大算法之四:SVM(支持向量机)

    SVM 支持向量机 支持向量机 Support Vector Machine 是一种十分常见的分类器 曾经火爆十余年 分类能力强于NN 整体实力比肩LR与RF 核心思路是通过构造分割面将数据进行分离 寻找到一个超平面使样本分成两类 并且间隔
  • IC卡和ID卡

    定义 最常用的感应卡分为ID卡和IC卡两种 IC卡全称集成电路卡 Integrated Circuit Card 又称智能卡 Smart Card 可读写 容量大 有加密功能 数据记录可靠 使用更方便 如一卡通系统 消费系统等 ID卡全称身
  • java.sql.SQLException: Statement.executeQuery() cannot issue statements that do not produce result

    java sql SQLException Statement executeQuery cannot issue statements that do not produce result sets 解决 看看自己的java代码里的 sq
  • Linux内核内存检测工具KASAN

    KASAN k z n KASAN 是 Kernel Address Sanitizer 的缩写 它是一个动态检测内存错误的工具 主要功能是检查内存越界访问和使用已释放的内存等问题 KASAN 集成在 Linux 内核中 随 Linux 内
  • 项目管理 附下载地址

    本书对现代项目管理的基本管理过程 知识模块 工具和方法等进行了全面的介绍 全书共有十二章 每部分都力求深入浅出 站在项目经理的角度 考虑其责任大于权力的现实 并结合时代特征 学习项目管理的软技能和硬技能 通过实际案例 高效的工具和模板 使读
  • 作为字典数据获取枚举值

    RequestMapping value getAmmeterType method RequestMethod GET ResponseBody ApiOperation notes 获取电表类型 value 获取电表类型 public
  • Python程序异常处理

    一 什么是异常 异常就是程序运行时发生错误的信号 在程序由于某些原因出现错误的时候 若程序没有处理它 则会抛出异常 程序也的运行也会随之终止 程序异常带来的问题 1 程序终止 无法运行下去 2 如果程序是面向客户 那么会使客户的体验感很差
  • 【从小项目学图片处理】#1 答题卡识别

    说明 项目皆参考于网上 代码也有大部分参考原文 仅用于学习和练习图像处理操作 项目原文 Bubble sheet multiple choice scanner and test grader using OMR Python and Op