python - 完成opencv表中缺失的行

标签 python opencv image-processing ocr python-tesseract

我正在尝试检测账单图像中的单元格:

我有这张图片enter image description here

使用以下代码删除标记:

import cv2
import numpy as np

# read image
img = cv2.imread('dummy1.PNG')

# threshold on yellow
lower = (0, 200, 200)
upper = (100, 255, 255)
thresh = cv2.inRange(img, lower, upper)

# apply dilate morphology
kernel = np.ones((9, 9), np.uint8)
mask = cv2.morphologyEx(thresh, cv2.MORPH_DILATE, kernel)

# get largest contour
contours = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
big_contour = max(contours, key=cv2.contourArea)
x, y, w, h = cv2.boundingRect(big_contour)

# draw filled white contour on input
result = img.copy()
cv2.drawContours(result, [big_contour], 0, (255, 255, 255), -1)


cv2.imwrite('removed.png', result)

# show the images
cv2.imshow("RESULT", result)
cv2.waitKey(0)
cv2.destroyAllWindows()

并获得了这张图片: enter image description here

然后应用灰度、反转、检测垂直和水平内核并通过此 main.py 合并:

# Imports
import cv2
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import csv

try:
    from PIL import Image
except ImportError:
    import Image
import pytesseract

pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
#################################################################################################


# Read your file
file = 'removed.png'
img = cv2.imread(file, 0)
img.shape


# thresholding the image to a binary image
thresh, img_bin = cv2.threshold(img, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)

# inverting the image
img_bin = 255 - img_bin
cv2.imwrite(r'C:\Users\marou\Desktop\cv_inverted.png', img_bin)

# Plotting the image to see the output
plotting = plt.imshow(img_bin, cmap='gray')
plt.show()

# Define a kernel to detect rectangular boxes

# Length(width) of kernel as 100th of total width
kernel_len = np.array(img).shape[1] // 100
# Defining a vertical kernel to detect all vertical lines of image
ver_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, kernel_len))
# Defining a horizontal kernel to detect all horizontal lines of image
hor_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (kernel_len, 1))
# A kernel of 2x2
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (2, 2))

#### Vertical LINES ####
# Use vertical kernel to detect and save the vertical lines in a jpg
image_1 = cv2.erode(img_bin, ver_kernel, iterations=5)
vertical_lines = cv2.dilate(image_1, ver_kernel, iterations=5)
cv2.imwrite(r'C:\Users\marou\Desktop\vertical.jpg', vertical_lines)
# Plot the generated image
plotting = plt.imshow(image_1, cmap='gray')
plt.show()

#### HORTIZONAL LINES ####
# Use horizontal kernel to detect and save the horizontal lines in a jpg
image_2 = cv2.erode(img_bin, hor_kernel, iterations=5)
horizontal_lines = cv2.dilate(image_2, hor_kernel, iterations=5)
cv2.imwrite(r'C:\Users\marou\Desktop\horizontal.jpg', horizontal_lines)
# Plot the generated image
plotting = plt.imshow(image_2, cmap='gray')
plt.show()



# Combining both H and V
# Combine horizontal and vertical lines in a new third image, with both having same weight.
img_vh = cv2.addWeighted(vertical_lines, 0.5, horizontal_lines, 0.5, 0.0)
# Eroding and thesholding the image
img_vh = cv2.erode(~img_vh, kernel, iterations=2)
thresh, img_vh = cv2.threshold(img_vh, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
cv2.imwrite(r'C:\Users\marou\Desktop\img_vh.jpg', img_vh)
plotting = plt.imshow(img_vh, cmap='gray')
plt.show()

要得到这个: enter image description here

现在我正在尝试填补由于水印删除而导致的行中的空白,以便能够应用正确的 OCR。 我尝试按照此 thread 中的步骤操作但我似乎无法做对。 当我尝试填充网格孔时:

# Fill individual grid holes
cnts = cv2.findContours(result, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
for c in cnts:
    x,y,w,h = cv2.boundingRect(c)
    cv2.rectangle(result, (x, y), (x + w, y + h), 255, -1)
cv2.imshow('result', result)
cv2.waitKey()

我得到空白图像: enter image description here

最佳答案

我概述了一种使用第二张图像作为输入来填充表中缺失行的方法。

image = cv2.imread(image_path)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)[1]

enter image description here

现在为水平线创建一个单独的蒙版:

h_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (50,1))
# contains only the horizontal lines
h_mask = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, h_kernel, iterations=1)

# performing repeated iterations to join lines
h_mask = cv2.dilate(h_mask, h_kernel, iterations=7)

enter image description here

还有一个单独的垂直线 mask :

v_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1,50))
v_mask = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, v_kernel, iterations=1)

enter image description here

结合以上结果,我们得到以下结果:

joined_lines = cv2.bitwise_or(v_mask, h_mask)

enter image description here

上面的结果不是你所期望的,线条已经超出了表格的边界。为了避免这种情况,我创建了一个单独的 mask 来限制表格区域。

kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5,5))
dilate = cv2.dilate(thresh, kernel, iterations=1)

enter image description here

现在找到上图中最大的轮廓并将其绘制在另一个二值图像上以创建蒙版。

contours, hierarchy = cv2.findContours(dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
c = max(contours, key = cv2.contourArea)           # contour with largest area
black = np.zeros((image.shape[0], image.shape[1]), np.uint8)
mask = cv2.drawContours(black, [c], 0, 255, -1)    # --> -1 to fill the contour

enter image description here

使用上面的图像作为上面创建的 join_lines 的 mask

fin = cv2.bitwise_and(joined_lines, joined_lines, mask = mask)

enter image description here

注意:

您可以对形态操作执行更多迭代,以更好地连接不连续的线条

关于python - 完成opencv表中缺失的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71902322/

相关文章:

python - 在 PyKD 中获取可执行文件的模块

python - 为什么 while 循环停留在 raw_input? (Python)

python - 有效地序列化时间戳

python - 漂亮的汤检查标签中的标签

c# - 使用 OpenCV 检测一幅图像中的对象是否在另一幅图像中

c++ - 这个函数有什么作用?与钳位值有关吗?

python - OpenCV(4.0.0)Python错误: (-215:Assertion failed) (mtype == CV_8U || mtype == CV_8S) && _mask.函数 'cv::binary_op'中的sameSize(*psrc1)

c++ - opencv:来自矩形的 Mat 构造函数

c++ - 如何在 openni 和 opencv 中使用 Kinect

c++ - 在opencv中聚类图像片段