python - 删除边界线上方的图像顶部以检测文本文档

标签 python opencv image-processing ocr tesseract

使用 OpenCV (python) 我正在尝试删除下图中边界线上方的图像部分(此示例图像中的白色区域,其中 ORIGINAL 是 writtn)
Invoice Image

使用水平和垂直内核我能够绘制线框,但是这不能多次工作,因为由于扫描质量,很多时候很少水平或垂直线出现在线框之外,这会导致错误的轮廓检测。在这张图片中,您还可以看到右上角有噪音,我将其检测为最上面的水平线。

我想要的是,一旦我得到了实际的盒子,我就可以简单地使用 x、y 坐标对所需字段(如引用号、发布日期等)进行 OCR 扫描。

以下是我能够使用下面的代码提取的内容。但是,由于此线框外的水平或垂直线嘈杂,无法裁剪图像的外部额外部分。还尝试用黑色填充外部部分,然后检测轮廓。
建议请...
Wireframe

    kernel_length = np.array(image).shape[1]//40 
# A verticle kernel of (1 X kernel_length), which will detect all the verticle lines from the image.
verticle_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (1, kernel_length))
# A horizontal kernel of (kernel_length X 1), which will help to detect all the horizontal line from the image.
hori_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (kernel_length, 1))
# A kernel of (3 X 3) ones.
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
# Morphological operation to detect verticle lines from an image
img_temp1 = cv2.erode(gray, verticle_kernel, iterations=3)
verticle_lines_img = cv2.dilate(img_temp1, verticle_kernel, iterations=3)

最佳答案

与其尝试寻找水平/垂直线来检测文本文档,不如在这里使用简单的轮廓过滤方法。这个想法是对图像进行阈值化以获得二值图像,然后找到轮廓并使用轮廓区域进行排序。最大的轮廓应该是文本文档。然后我们可以申请 four point perspective transform以获得图像的鸟瞰图。结果如下:

输入图像:



输出:



请注意输出图像如何仅具有所需的文本文档并且没有倾斜角度对齐。

代码

from imutils.perspective import four_point_transform
import cv2
import numpy

# Load image, grayscale, Gaussian blur, Otsu's threshold
image = cv2.imread("1.jpg")
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 sort for largest contour
cnts = cv2.findContours(thresh, cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cnts = cnts[0] if len(cnts) == 2 else cnts[1]
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)
displayCnt = None

for c in cnts:
    # Perform contour approximation
    peri = cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, 0.02 * peri, True)
    if len(approx) == 4:
        displayCnt = approx
        break

# Obtain birds' eye view of image
warped = four_point_transform(image, displayCnt.reshape(4, 2))

cv2.imshow("thresh", thresh)
cv2.imshow("warped", warped)
cv2.imshow("image", image)
cv2.waitKey()

关于python - 删除边界线上方的图像顶部以检测文本文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60018244/

相关文章:

python - 使用 PyArray_NewFromDescr 存储数据

python - 我如何 "wake up"事件循环来通知它另一个线程已完成 Future ?

visual-c++ - openCV 中的物体检测

ruby - Imagemagick 的 -lat '50x50' 在 minimagick 中不工作

php - 与 Postgres 一起工作的图像/媒体服务器

python - 检查 numpy 数组中步幅的非歧义性

python - 如果系列内单词的长度 > 3,则过滤数据帧

opencv - 当我们运行密集光流 (farnnback) 时,输出到底是什么?

python - 属性错误 : module 'cv2' has no attribute 'imread'

opencv - 使用 Azure 机器学习检测图像中的符号