删除边框线上方的图像顶部以检测文本文档 [英] Remove top section of image above border line to detect text document

查看:50
本文介绍了删除边框线上方的图像顶部以检测文本文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用OpenCV(python),我试图在下图所示的图像中删除边界线上方的图像部分(此示例图像中的白色区域,原始位置被扭曲).

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

使用水平和垂直内核,我可以绘制线框,但是由于扫描质量很多次,线框外部很少出现水平线或垂直线,这会导致错误的轮廓检测,因此该线框无法正常工作.在此图像中,您还可以在右上角看到我正在检测到的噪声为最上方的水平线.

Using horizontal and vertical kernels I am able to draw the wireframe, however that does not work many times because many times due to scanning quality few horizontal or vertical lines appear outside the wireframe which causes wrong contour detection. In this image also you can see on top right there is noise which I am detecting as topmost horizontal line.

我想要的是,一旦获得实际的框,我就可以简单地使用x,y坐标对所需字段(例如参考号,颁发机构等)进行OCR扫描.

What I want is, once I get the actual box then I can simply use x, y coordinates for OCR scanning of needed fields (like reference number, Issued In etc).

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

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...

    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)

推荐答案

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

Instead of trying to find horizontal/vertical lines to detect the text document, a simple contour filtering approach should work here. The idea is to threshold the image to obtain a binary image then find contours and sort using contour area. The largest contour should be the text document. We can then apply a four point perspective transform to obtain a birds eye view of the image. Here's the results:

输入图片:

输出:

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

Notice how the output image only has the desired text document and is aligned without a skewed angle.

代码

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()

这篇关于删除边框线上方的图像顶部以检测文本文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆