如何使用opencv python在各种彩色背景中查找文档边缘? [各种背景下的文档扫描]

时间:2019-04-04 10:19:01

标签: python-3.x transform background-color opencv3.0 perspective

我目前有一个需要智能扫描的文档。

为此,我需要在任何背景下找到合适的文档轮廓,以便可以对该图像进行变形的透视投影和检测。

执行此操作时面临的主要问题是文档边缘检测到任何类型的背景。

我一直尝试使用HoughLineP函数,并尝试在到目前为止通过Canny边缘检测的灰度模糊图像上找到轮廓。

<input type="hidden">

Sample Image of Document 如何通过OpenCV代码在文档周围找到合适的边框。 任何帮助都感激不尽。 (文档是从相机以任何角度和彩色背景拍摄的。)

2 个答案:

答案 0 :(得分:1)

还有一个类似的问题,称为正交投影。

Orthographic approaches

不是进行高斯模糊+形态学运算来获取文档的边缘,而是先进行正投影,然后再通过您的方法找到轮廓。

要精确定义边界框,请尝试使用一些预设值或参考字母,然后使用正交投影可以计算高度,从而计算边界框的尺寸。

答案 1 :(得分:1)

以下代码可以帮助您检测/细分图像中的页面...

import cv2
import matplotlib.pyplot as plt
import numpy as np
image = cv2.imread('test_p.jpg')
image = cv2.imread('test_p.jpg')
print(image.shape)
ori = image.copy()
image = cv2.resize(image, (image.shape[1]//10,image.shape[0]//10))

调整图像大小以使操作更快,以便我们可以实时工作。

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (11,11), 0)
edged = cv2.Canny(gray, 75, 200)
print("STEP 1: Edge Detection")
plt.imshow(edged)
plt.show()
cnts = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts = sorted(cnts[1], key = cv2.contourArea, reverse = True)[:5]

在这里,我们将仅考虑基于面积的排序列表中的前5个轮廓 高斯模糊的大小在这里是位敏感的,因此请根据图像大小选择相应的大小。 经过上述操作,图像可能看起来像..

Image with Edges

for c in cnts:
    ### Approximating the contour
    #Calculates a contour perimeter or a curve length
    peri = cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, 0.01 * peri, True)
    # if our approximated contour has four points, then we
    # can assume that we have found our screen
    screenCnt = approx
    if len(approx) == 4:
        screenCnt = approx
        break
    # show the contour (outline) 
    print("STEP 2: Finding Boundary")
cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 2)
image_e = cv2.resize(image,(image.shape[1],image.shape[0]))
cv2.imwrite('image_edge.jpg',image_e)
plt.imshow(image_e)
plt.show()

最终图像可能看起来像...

获得最终图像后,其他事情可能会得到处理...

代码参考:-Git Repository

我想这个答案会有所帮助...