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

Remove top section of image above border line to detect text document

使用 OpenCV (python) 我正在尝试删除下图中边界线上方的图像部分(此示例图像中写入原始图像的白色区域)

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

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

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

    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)

与其试图找到 horizontal/vertical 行来检测文本文档,不如在这里使用简单的轮廓过滤方法。这个想法是对图像进行阈值处理以获得二值图像,然后找到轮廓并使用轮廓区域进行排序。最大的轮廓应该是文本文档。然后我们可以应用 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()