gpt4 book ai didi

python - 如何使用 Python OpenCV 检测和查找表单中的复选框?

转载 作者:太空宇宙 更新时间:2023-11-03 20:49:04 24 4
gpt4 key购买 nike

我有几张图片需要通过使用计算机视觉检测复选框来进行 OMR。

我正在使用 findContours 仅在扫描文档中的复选框上绘制轮廓。但是该算法会提取文本的每一个轮廓。

from imutils.perspective import four_point_transform
from imutils import contours
import numpy as np
import argparse, imutils, cv2, matplotlib
import matplotlib.pyplot as plt
import matplotlib.image as mpimg

image = cv2.imread("1.jpg")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
edged = cv2.Canny(blurred, 75, 200)

im_test = [blurred, cv2.GaussianBlur(gray, (7, 7), 0), cv2.GaussianBlur(gray, (5, 5), 5), cv2.GaussianBlur(gray, (11, 11), 0)]
im_thresh = [ cv2.threshold(i, 127, 255, 0) for i in im_test ]
im_thresh_0 = [i[1] for i in im_thresh ]
im_cnt = [cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[0] for thresh in im_thresh_0]

im_drawn = [cv2.drawContours(image.copy(), contours, -1, (0,255,0), 1) for contours in im_cnt]

plt.imshow(im_drawn[0])
plt.show()

输入图像:

enter image description here

最佳答案

  1. 获取二进制图像。 Load the image , grayscale , Gaussian blur , 和 Otsu's threshold获得二进制黑白图像。

  2. 去除小噪声粒子。 Find contours并使用 contour area filtering 进行过滤去除噪音。

  3. 修复复选框水平和垂直墙。此步骤是可选的,但在复选框可能损坏的情况下,我们会修复墙以便于检测。这个想法是创建一个 rectangular kernel然后执行 morphological operations .

  4. 检测复选框。从这里我们找到轮廓,获得bounding rectangle coordinates ,并使用 shape approximation 进行过滤+纵横比。这个想法是复选框本质上是一个正方形,所以它的轮廓尺寸应该在一个范围内。


输入图像->二值图像

检测到的复选框以绿色突出显示

enter image description here

Checkboxes: 52

另一个输入图像->二值图像

检测到的复选框以绿色突出显示

enter image description here

Checkboxes: 2

代码

import cv2

# Load image, convert to grayscale, Gaussian blur, Otsu's threshold
image = cv2.imread('1.jpg')
original = image.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blur = cv2.GaussianBlur(gray, (3,3), 0)
thresh = cv2.threshold(blur, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

# Find contours and filter using contour area filtering to remove noise
cnts, _ = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)[-2:]
AREA_THRESHOLD = 10
for c in cnts:
area = cv2.contourArea(c)
if area < AREA_THRESHOLD:
cv2.drawContours(thresh, [c], -1, 0, -1)

# Repair checkbox horizontal and vertical walls
repair_kernel1 = cv2.getStructuringElement(cv2.MORPH_RECT, (5,1))
repair = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, repair_kernel1, iterations=1)
repair_kernel2 = cv2.getStructuringElement(cv2.MORPH_RECT, (1,5))
repair = cv2.morphologyEx(repair, cv2.MORPH_CLOSE, repair_kernel2, iterations=1)

# Detect checkboxes using shape approximation and aspect ratio filtering
checkbox_contours = []
cnts, _ = cv2.findContours(repair, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2:]
for c in cnts:
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.035 * peri, True)
x,y,w,h = cv2.boundingRect(approx)
aspect_ratio = w / float(h)
if len(approx) == 4 and (aspect_ratio >= 0.8 and aspect_ratio <= 1.2):
cv2.rectangle(original, (x, y), (x + w, y + h), (36,255,12), 3)
checkbox_contours.append(c)

print('Checkboxes:', len(checkbox_contours))
cv2.imshow('thresh', thresh)
cv2.imshow('repair', repair)
cv2.imshow('original', original)
cv2.waitKey()

关于python - 如何使用 Python OpenCV 检测和查找表单中的复选框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55763858/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com