Explore Enterprise Education Gitee Premium Gitee AI AI teammates
Fetch the repository succeeded.
Donate
Please sign in before you donate.
Scan WeChat QR to Pay
Cancel
Complete
Prompt
Switch to Alipay.
OK
Cancel
1 Star 2 Fork 2.5K

程序源代码/从零学Python

Create your Gitee Account
Explore and code with more than 14 million developers,Free private repositories !:)
Sign up
Already have an account? Sign in
文件
master
Branches (1)
master
master
Branches (1)
master
Clone or Download
Clone/Download
Prompt
To download the code, please copy the following command and execute it in the terminal
To ensure that your submitted code identity is correctly recognized by Gitee, please execute the following command.
When using the SSH protocol for the first time to clone or push code, follow the prompts below to complete the SSH configuration.
1 Generate RSA keys.
2 Obtain the content of the RSA public key and configure it in SSH Public Keys
To use SVN on Gitee, please visit the usage guide
When using the HTTPS protocol, the command line will prompt for account and password verification as follows. For security reasons, Gitee recommends configure and use personal access tokens instead of login passwords for cloning, pushing, and other operations.
Username for 'https://gitee.com': userName
Password for 'https://userName@gitee.com': # Private Token
master
Branches (1)
master
Python
/
Day12
/
sheet_scan_example02.py
Python
/
Day12
/
sheet_scan_example02.py
sheet_scan_example02.py 4.41 KB
Copy Edit Raw Blame History
小柒2012商城 authored 2018年11月23日 17:53 +08:00 . OpenCV识别答题卡
# -*- coding:utf-8 -*-
from imutils.perspective import four_point_transform
from imutils import contours
import numpy as np
import cv2 as cv
# 加载原图,可在项目imgs/example02目录下找到
img = cv.imread("test01.jpg")
# cv.resizeWindow("enhanced", 240, 280);
# 打印原图
cv.imshow("orgin", img)
# 灰度化
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
# 打印灰度图
cv.imshow("gray", gray)
# 高斯滤波,清除一些杂点
blur = cv.GaussianBlur(gray, (3, 3), 0)
# 自适应二值化算法
thresh2 = cv.adaptiveThreshold(blur, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY_INV, 131, 4)
# 打印二值化后的图
cv.imshow("thresh2", thresh2)
# 寻找轮廓
image, cts, hierarchy = cv.findContours(thresh2, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
# 打印找到的轮廓
print("轮廓数:", len(cts))
# 对拷贝的原图进行轮廓标记
contour_flagged = cv.drawContours(img.copy(), cts, -1, (0, 0, 255), 3)
# 打印轮廓图
cv.imshow("contours_flagged", contour_flagged)
# 按像素面积降序排序
list = sorted(cts, key=cv.contourArea, reverse=True)
# 遍历轮廓
for ct in list:
# 周长,第1个参数是轮廓,第二个参数代表是否是闭环的图形
peri = 0.01 * cv.arcLength(ct, True)
# 获取多边形的所有定点,如果是四个定点,就代表是矩形
approx = cv.approxPolyDP(ct, peri, True)
# 只考虑矩形
if len(approx) == 4:
# 从原图中提取所需的矫正图片
ox = four_point_transform(img, approx.reshape(4, 2))
# 从原图中提取所需的矫正图片
tx = four_point_transform(gray, approx.reshape(4, 2))
# 打印矫正后的灰度图
cv.imshow("tx", tx)
# 对矫正图进行高斯模糊
blur = cv.GaussianBlur(tx, (3, 3), 0)
# 对矫正图做自适应二值化
thresh2 = cv.adaptiveThreshold(blur, 255, cv.ADAPTIVE_THRESH_GAUSSIAN_C, cv.THRESH_BINARY_INV, 131, 4)
# 打印矫正后的二值化图
cv.imshow("tx_thresh2", thresh2)
# 获取轮廓
r_image, r_cts, r_hierarchy = cv.findContours(thresh2, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
# 打印得到轮廓数量
print("第二层轮廓数:", len(r_cts))
# 用于存储答案的python list变量
question_list = []
for r_ct in r_cts:
# 转为矩形,分别获取 x,y坐标,及矩形的宽和高
x, y, w, h = cv.boundingRect(r_ct)
# 过滤掉不符合答案坐标和长宽的选项
if x > 2 and y > 2 and w > 20 and h > 20:
# cv.drawContours(ox, r_ct, -1, (0, 0, 255), 1)
question_list.append(r_ct)
print("答案总数:", len(question_list))
# 按坐标从上到下排序
questionCnts = contours.sort_contours(question_list, method="top-to-bottom")[0]
# 使用np函数,按5个元素,生成一个集合
for (q, i) in enumerate(np.arange(0, len(questionCnts), 5)):
# 每一个行5个答案,从左到右排序
cnts = contours.sort_contours(questionCnts[i:i + 5])[0]
# 存储一行题里面的每个答案
ans_list = []
for (j, cc) in enumerate(cnts):
# 生成全黑画布
mask = np.zeros(thresh2.shape, dtype="uint8")
# 将每一个答案按轮廓写上去,并将填充颜色设置成白色
tpp = cv.drawContours(mask, [cc], -1, 255, -1)
# 两个图片做位运算
mask = cv.bitwise_and(thresh2, thresh2, mask=mask)
# 统计每个答案的像素
total = cv.countNonZero(mask)
# 添加到集合里面
ans_list.append((total, j))
# 按像素大小排序
ans_list = sorted(ans_list, key=lambda x: x[0], reverse=True)
max_ans_num = ans_list[0][1]
max_ans_size = ans_list[0][0]
print("答案序号:", max_ans_num, "列表:", ans_list)
# 给选中答案,标记成红色
cv.drawContours(ox, cnts[max_ans_num], -1, (0, 0, 255), 2)
cv.imshow("answer_flagged", ox)
# 最大的轮廓就是我们想要的,之后的就可以结束循环了
break
# 阻塞等待窗体关闭
cv.waitKey(0)
Loading...
Report
Report success
We will send you the feedback within 2 working days through the letter!
Please fill in the reason for the report carefully. Provide as detailed a description as possible.
Please select a report type
Cancel
Send
误判申诉

此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。

如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。

取消
提交

About

从零学Python,各种开发案例,不定期更新。
Cancel

Releases

No release

Contributors

All

Activities

can not load any more
Edit
About
Homepage
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Python
1
https://gitee.com/itcode-itcode/Python.git
git@gitee.com:itcode-itcode/Python.git
itcode-itcode
Python
从零学Python
master
Going to Help Center

Search

Comment
Repository Report
Back to the top
Login prompt
This operation requires login to the code cloud account. Please log in before operating.
Go to login
No account. Register

AltStyle によって変換されたページ (->オリジナル) /