首页 > 解决方案 > 在 Python 中使用 OpenCV 扫描 OMR 表

问题描述

当我扫描 OMR 表以检测轮廓时,它没有检测到所有矩形。步骤如下:

  1. 加载彩色图像
  2. 转换为灰色并调整大小
  3. 使用 GaussianBlur 模糊图像
  4. 使用 Canny 进行边缘检测
  5. 寻找轮廓
  6. 可视化结果

但它并未显示所有矩形。谁能帮我解决这个问题。

附上 OMR 表供参考:

输入 OMR 表

标签: python-3.xopencv3.0

解决方案


我的建议如下:

  1. 加载图像
  2. img 变灰
  3. 模糊和精明
  4. 应用轮廓
  5. 打印轮廓并对轮廓进行排序
  6. 如果它们具有相同的面积,则绘制轮廓(因为您感兴趣的区域都是相同的形状)
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.

所有轮廓的输出


推荐阅读