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

2023-12-02

Using OpenCV (python) I am trying to remove the section of image which is above the border line (white area in this sample image where ORIGINAL is writtn) in the image shown below Invoice Image

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

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

Following is what I have been able to extract using the code below. However not able to clip the outer extra section of image due to noisy horizontal or vertical lines outside this wireframe. Also tried filling outside section with black and then detecting the contours.
Suggestions please... 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)

在这里应该使用简单的轮廓过滤方法,而不是试图找到水平/垂直线来检测文本文档。这个想法是对图像进行阈值处理以获得二值图像,然后找到轮廓并使用轮廓区域进行排序。最大的轮廓应该是文本文档。然后我们可以应用一个四点透视变换获得图像的鸟瞰图。结果如下:

输入图像:

Output:

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

Code

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()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

删除边框线上方图像的顶部部分以检测文本文档 的相关文章

随机推荐