我有由表格图像组成的 PDF。我想在此图像上应用 OCR 和 OMR 来提取所需的数据。我想检测选中的复选框并从这些图像中将关联数据提取到这些复选框。但是,当前代码无法检测选中复选框的轮廓。
我正在使用
findcontours
来查找轮廓并在复选框周围创建边界框。这段代码的灵感来自这篇post。但是,它无法检测选中复选框的轮廓。这是代码和我应用代码的表单中区域的图像。
gray = x_gray.copy() # Copy of the Grayscale image shared in this post
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 = 100
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(gray, (x, y), (x + w, y + h), (36,255,12), 3) #original
checkbox_contours.append(c)
print('Checkboxes:', len(checkbox_contours))
我尝试了参数
AREA_THRESHOLD
的各种值,范围从10到200。但是,我仍然没有得到预期的结果。
如果有人能帮助我解决这个问题,那将非常有帮助。
您找到解决方案了吗?关注一下,我也有同样的问题