python-3.x - 在 Python 中使用 OpenCV 扫描 OMR 表
问题描述
当我扫描 OMR 表以检测轮廓时,它没有检测到所有矩形。步骤如下:
- 加载彩色图像
- 转换为灰色并调整大小
- 使用 GaussianBlur 模糊图像
- 使用 Canny 进行边缘检测
- 寻找轮廓
- 可视化结果
但它并未显示所有矩形。谁能帮我解决这个问题。
附上 OMR 表供参考:
解决方案
我的建议如下:
- 加载图像
- img 变灰
- 模糊和精明
- 应用轮廓
- 打印轮廓并对轮廓进行排序
- 如果它们具有相同的面积,则绘制轮廓(因为您感兴趣的区域都是相同的形状)
import cv2
import numpy as np
img=cv2.imread('test.png') #read image
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) #turn image to gray
blur = cv2.GaussianBlur(gray,(3,3),0) #add blur
edges = cv2.Canny(blur,50,100) #find edges
contours, hierarchy = cv2.findContours(edges,cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) #find contours
cv2.drawContours(img,contours,-1,(0,255,0),2) #draw contours
cv2.imshow('Contours in Green',img) #show contours in green
#Now you need to sort them out.
推荐阅读
- java - 收到握手警告:unrecognized_name
- sql - PostgreSQL DATE_PART 函数
- java - 尝试解析 apache-commons-chains 的目录 XML 时出现 EmptyStackException
- r - 如何强制“pairlist”对象键入“double”(对于ggscatter cor.coef)?
- java - 使用 PDFBOX 将 PDF 文档所有页面导出为 A4 大小的图像(PDF 文档包含不同的页面大小,如 A3 和 Letter )
- r - R中的特殊/保留属性列表?
- python - PyCharm - from turtle import * -- 提供未使用的导入参考
- laravel - 通过虚拟主机和端口进行 API 访问?
- python - Python:与 MacOS 相比,Windows10 上的 Scipy 非常慢
- r - 如何将变量从一个 R 脚本重用到另一个?