Pra*_*ati 7 transform background-color perspective python-3.x opencv3.0
我目前有一份需要智能扫描的文档。
为此,我需要在任何背景下找到文档的正确轮廓,以便我可以对该图像进行扭曲的透视投影和检测。
这样做时面临的主要问题是文档边缘检测任何类型的背景。
到目前为止,我一直尝试使用函数 HoughLineP 并尝试在通过精明边缘检测的灰度模糊图像上找到轮廓。
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
Run Code Online (Sandbox Code Playgroud)
如何通过 OpenCV 代码在文档周围找到正确的边界框。任何帮助都感激不尽。(该文档是从相机以任何角度和任何彩色背景拍摄的。)
以下代码可能会帮助您检测/分割图像中的页面...
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))
Run Code Online (Sandbox Code Playgroud)
调整图像大小以使操作更快,以便我们可以实时工作。
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]
Run Code Online (Sandbox Code Playgroud)
这里我们将只考虑基于面积排序列表中的前 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()
Run Code Online (Sandbox Code Playgroud)
最终图像可能看起来像......
剩下的事情可以在得到最终图像后处理......
代码参考:- Git 存储库
我想这个答案会有帮助......
| 归档时间: |
|
| 查看次数: |
9049 次 |
| 最近记录: |