如何使用opencv python在各种彩色背景中找到文档边缘?【各种背景下的文档扫描】 [英] How to find the document edges in various coloured backgrounds using opencv python? [Document Scanning in various backgrounds]

查看:55
本文介绍了如何使用opencv python在各种彩色背景中找到文档边缘?【各种背景下的文档扫描】的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am currently have a document that needs to be smart scanned.

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

For that, I need to find proper contours of the document in any background so that I can do a warped perspective projection and detection with that image.

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

The main issue faced while doing this is that the document edge detects any kind of background.

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

I have tried to use the function HoughLineP and tried to find contours on the grayscale blurred image passed through canny edge detection until now.


            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 代码在文档周围找到合适的边界框.任何帮助都感激不尽.(文档以任何角度和任何彩色背景从相机中拍摄.)

How to find a proper bounding box around the document via OpenCV code. Any help will be much appreciated. (The document is taken from the camera in any angle and any coloured background.)

推荐答案

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

Following code might help you to detect/segment the page in the image...

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

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

Resized the image to make the operations more faster so that we can work on realtime..

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 个轮廓这里高斯模糊的大小有点敏感,所以根据图像大小相应地选择它.上面操作后的图像可能看起来像..

Here we will consider only first 5 contours from the sorted list based on area Here the size of the gaussian blur is bit sensitive, so chose it accordingly based on the image size. After the above operations image may look like..

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

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

Final Image may look like...

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

Rest of the things may be handled after getting the final image...

代码参考:- Git 存储库

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

I guess this answer would be helpful...

这篇关于如何使用opencv python在各种彩色背景中找到文档边缘?【各种背景下的文档扫描】的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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