如何使用opencv python在各种彩色背景中找到文档边缘? 【各种背景下的证件扫描】
How to find the document edges in various coloured backgrounds using opencv python? [Document Scanning in various backgrounds]
我目前有一份文件需要智能扫描。
为此,我需要在任何背景中找到文档的适当轮廓,以便我可以对该图像进行扭曲透视投影和检测。
这样做时面临的主要问题是文档边缘检测到任何类型的背景。
我一直在尝试使用函数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 代码在文档周围找到合适的边界框。
任何帮助都感激不尽。
(文档是从相机以任何角度和任何彩色背景拍摄的。)
还有一个类似的问题叫做正交投影。
与其做,高斯模糊+形态学操作得到文档的边缘,尝试先做正交投影,然后通过你的方法找到轮廓。
为了完善适当的边界框,请尝试一些预设值或参考字母,之后正射投影将允许您计算高度,从而计算边界框的尺寸。
以下代码可能会帮助您detect/segment图像中的页面...
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
我想这个答案会有帮助...
我目前有一份文件需要智能扫描。
为此,我需要在任何背景中找到文档的适当轮廓,以便我可以对该图像进行扭曲透视投影和检测。
这样做时面临的主要问题是文档边缘检测到任何类型的背景。
我一直在尝试使用函数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
还有一个类似的问题叫做正交投影。
与其做,高斯模糊+形态学操作得到文档的边缘,尝试先做正交投影,然后通过你的方法找到轮廓。
为了完善适当的边界框,请尝试一些预设值或参考字母,之后正射投影将允许您计算高度,从而计算边界框的尺寸。
以下代码可能会帮助您detect/segment图像中的页面...
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
我想这个答案会有帮助...