您好,登录后才能下订单哦!
在数字化时代,文档扫描器是一个非常有用的工具,它可以将纸质文档转换为数字格式,便于存储、编辑和分享。本文将介绍如何使用Python构建一个简单的文档扫描器。我们将使用OpenCV和NumPy库来实现这一功能。
首先,我们需要安装所需的Python库。你可以使用以下命令来安装这些库:
pip install opencv-python numpy
在开始编写代码之前,我们需要导入所需的库:
import cv2
import numpy as np
我们将从加载一张包含文档的图像开始。你可以使用cv2.imread()
函数来加载图像:
image = cv2.imread('document.jpg')
为了更容易地检测文档的边缘,我们需要对图像进行一些预处理。首先,我们将图像转换为灰度图像:
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
接下来,我们对灰度图像进行高斯模糊处理,以减少噪声:
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
然后,我们使用Canny边缘检测算法来检测图像中的边缘:
edged = cv2.Canny(blurred, 75, 200)
现在,我们需要找到图像中最大的轮廓,这通常就是文档的轮廓。我们可以使用cv2.findContours()
函数来查找轮廓:
contours, _ = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHN_APPROX_SIMPLE)
contours = sorted(contours, key=cv2.contourArea, reverse=True)[:5]
接下来,我们遍历这些轮廓,找到近似于矩形的轮廓:
for contour in contours:
perimeter = cv2.arcLength(contour, True)
approx = cv2.approxPolyDP(contour, 0.02 * perimeter, True)
if len(approx) == 4:
document_contour = approx
break
一旦我们找到了文档的轮廓,我们需要对其进行透视变换,以得到一个正面的视图。我们可以使用cv2.getPerspectiveTransform()
和cv2.warpPerspective()
函数来实现这一点:
def four_point_transform(image, pts):
rect = order_points(pts)
(tl, tr, br, bl) = rect
widthA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2))
widthB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2))
maxWidth = max(int(widthA), int(widthB))
heightA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2))
heightB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2))
maxHeight = max(int(heightA), int(heightB))
dst = np.array([
[0, 0],
[maxWidth - 1, 0],
[maxWidth - 1, maxHeight - 1],
[0, maxHeight - 1]], dtype="float32")
M = cv2.getPerspectiveTransform(rect, dst)
warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight))
return warped
warped = four_point_transform(image, document_contour.reshape(4, 2))
最后,我们将扫描后的文档转换为黑白图像,以便更好地进行OCR处理或其他操作:
warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY)
warped = cv2.threshold(warped, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
现在,我们已经完成了文档的扫描和转换。我们可以将结果保存为图像文件:
cv2.imwrite('scanned_document.jpg', warped)
以下是完整的Python代码:
import cv2
import numpy as np
def order_points(pts):
rect = np.zeros((4, 2), dtype="float32")
s = pts.sum(axis=1)
rect[0] = pts[np.argmin(s)]
rect[2] = pts[np.argmax(s)]
diff = np.diff(pts, axis=1)
rect[1] = pts[np.argmin(diff)]
rect[3] = pts[np.argmax(diff)]
return rect
def four_point_transform(image, pts):
rect = order_points(pts)
(tl, tr, br, bl) = rect
widthA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2))
widthB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2))
maxWidth = max(int(widthA), int(widthB))
heightA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2))
heightB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2))
maxHeight = max(int(heightA), int(heightB))
dst = np.array([
[0, 0],
[maxWidth - 1, 0],
[maxWidth - 1, maxHeight - 1],
[0, maxHeight - 1]], dtype="float32")
M = cv2.getPerspectiveTransform(rect, dst)
warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight))
return warped
image = cv2.imread('document.jpg')
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
edged = cv2.Canny(blurred, 75, 200)
contours, _ = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHN_APPROX_SIMPLE)
contours = sorted(contours, key=cv2.contourArea, reverse=True)[:5]
for contour in contours:
perimeter = cv2.arcLength(contour, True)
approx = cv2.approxPolyDP(contour, 0.02 * perimeter, True)
if len(approx) == 4:
document_contour = approx
break
warped = four_point_transform(image, document_contour.reshape(4, 2))
warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY)
warped = cv2.threshold(warped, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
cv2.imwrite('scanned_document.jpg', warped)
通过以上步骤,我们成功地使用Python构建了一个简单的文档扫描器。这个扫描器可以将纸质文档转换为数字格式,并且可以进一步用于OCR处理或其他用途。希望这篇文章对你有所帮助!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。