【问题标题】:How to find the document edges in various coloured backgrounds using opencv python? [Document Scanning in various backgrounds]如何使用opencv python在各种彩色背景中找到文档边缘? [各种背景下的文档扫描]
【发布时间】:2019-08-26 01:32:21
【问题描述】:

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

为此,我需要在任何背景中找到文档的适当轮廓,以便我可以使用该图像进行扭曲透视投影和检测。

这样做时面临的主要问题是文档边缘检测到任何类型的背景。

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


            MORPH = 9
            CANNY = 84
            HOUGH = 25

            IM_HEIGHT, IM_WIDTH, _ = rescaled_image.shape

            # convert the image to grayscale and blur it slightly
            gray = cv2.cvtColor(rescaled_image, cv2.COLOR_BGR2GRAY)
            gray = cv2.GaussianBlur(gray, (7,7), 0)

            #dilate helps to remove potential holes between edge segments
            kernel = cv2.getStructuringElement(cv2.MORPH_RECT,(MORPH,MORPH))
            dilated = cv2.dilate(gray, kernel)

            # find edges and mark them in the output map using the Canny algorithm
            edged = cv2.Canny(dilated, 0, CANNY)
            test_corners = self.get_corners(edged)

            approx_contours = []

    (_, cnts, hierarchy) = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
            cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:5]

            # loop over the contours
            for c in cnts:
                # approximate the contour
                approx = cv2.approxPolyDP(c, 80, True)
                if self.is_valid_contour(approx, IM_WIDTH, IM_HEIGHT):
                    approx_contours.append(approx)
                    break

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

【问题讨论】:

  • 请建议在 findcontours 的输入中应采用哪种类型的图像
  • 是否有一些基于图像的规则可以让我们预测边界框,如果没有,您正在查看 ML 应用程序,如果是,请提及一些规则,最好的方法应该是梯度分析或 Graph Cut,但如果没有关于数据集的更多信息,就不能确定。
  • 不,它不是某个数据集上基于 ML 的应用程序。它需要直接检测放置在任何背景中的文档的边缘。
  • 没有规则,但它应该适用于任何文档,让我添加示例图像
  • stackoverflow.com/questions/51927043/…也有类似的问题,但是这里页面也是弯曲的,光照不一样。

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


【解决方案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 个轮廓 这里高斯模糊的大小是位敏感的,所以根据图像大小相应地选择它。 经过上述操作后,图像可能看起来像..

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

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

【讨论】:

  • 哟高斯模糊的大小是否敏感?
  • 感谢您的出色回答。它主要适用于所有背景。
  • 这段代码看起来很像pyimagesearch.com/2014/09/01/…中的代码
【解决方案2】:

还有一个类似的问题,叫做正交投影。

Orthographic approaches

与其做高斯模糊+形态学运算得到文档边缘,不如先做正射投影,再通过​​你的方法找轮廓。

为了确定正确的边界框,请尝试一些预设值或参考字母,然后正交投影将允许您计算边界框的高度和尺寸。

【讨论】:

  • 让我试一试。谢谢
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-06-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多