?譯者 | 布加迪
審校 | 孫淑娟
您可能想要對(duì)文檔進(jìn)行數(shù)字化處理,以節(jié)省物理空間或創(chuàng)建備份。無(wú)論怎樣,編寫一個(gè)程序?qū)⒓堎|(zhì)文件的照片轉(zhuǎn)換成準(zhǔn)格式正是Python所擅長(zhǎng)的任務(wù)。
結(jié)合使用多個(gè)適當(dāng)?shù)膸?kù),您就可以構(gòu)建一個(gè)小型應(yīng)用程序?qū)ξ臋n進(jìn)行數(shù)字化處理。您的程序?qū)⒁晕锢砦臋n的圖像作為輸入,對(duì)其運(yùn)用幾種圖像處理技術(shù),即可輸出輸入的掃描版。
1、準(zhǔn)備好環(huán)境
首先您應(yīng)該熟悉Python的基礎(chǔ)知識(shí),還需要了解如何使用NumPy Python庫(kù)。
打開任何Python IDE,創(chuàng)建兩個(gè)Python文件。將一個(gè)命名為main.py,將另一個(gè)命名為transform.py。然后在終端上執(zhí)行以下命令,安裝所需的庫(kù)。
pip install OpenCV-Python imutils scikit-image NumPy
您將使用OpenCV-Python獲取圖像輸入并進(jìn)行一些圖像處理,使用Imutils來(lái)調(diào)整輸入和輸出圖像的大小,并使用scikit-image對(duì)圖像施加閾值。NumPy將幫助您處理數(shù)組。

等待安裝完成,并等待IDE更新項(xiàng)目骨干內(nèi)容。骨干內(nèi)容更新完成后,您就可以開始編程了。完整的源代碼可以在GitHub代碼庫(kù)中找到。
2、導(dǎo)入已安裝的庫(kù)
打開main.py文件,導(dǎo)入所安裝的庫(kù)。這將使您能夠在必要時(shí)調(diào)用和使用它們的函數(shù)。
import cv2
import imutils
from skimage.filters import threshold_local
from transform import perspective_transform
忽略perspective_transform方面拋出的錯(cuò)誤。您完成處理transform.py文件的工作后,錯(cuò)誤會(huì)消失。
3、獲取并調(diào)整輸入的大小
為想要掃描的文檔拍攝一張清晰的圖像。確保文檔的四個(gè)角及其內(nèi)容都可見。將圖像復(fù)制到存儲(chǔ)程序文件的同一個(gè)文件夾中。

將輸入圖像路徑傳遞給OpenCV。制作原始圖像的副本,因?yàn)槟谕敢曓D(zhuǎn)換期間需要它。將原始圖像的高度除以您想要調(diào)整到的高度。這將保持縱橫比。最后,輸出調(diào)整后的圖像。
# Passing the image path
original_img = cv2.imread('sample.jpg')
copy = original_img.copy()
# The resized height in hundreds
ratio = original_img.shape[0] / 500.0
img_resize = imutils.resize(original_img, height=500)
# Displaying output
cv2.imshow('Resized image', img_resize)
# Waiting for the user to press any key
cv2.waitKey(0)
上述代碼的輸出如下:

現(xiàn)在您已經(jīng)將原始圖像的高度調(diào)整為500像素。
4、將調(diào)整后的圖像轉(zhuǎn)換為灰度圖像
將調(diào)整后的RGB圖像轉(zhuǎn)換為灰度圖像。大多數(shù)圖像處理庫(kù)只處理灰度圖像,因?yàn)樗鼈兏菀滋幚怼?/p>
gray_image = cv2.cvtColor(img_resize, cv2.COLOR_BGR2GRAY)
cv2.imshow('Grayed Image', gray_image)
cv2.waitKey(0)
注意原始圖像和灰度圖像之間的區(qū)別。

IDE上顯示灰色映像的程序輸出
彩色桌變成了黑白桌。
5、運(yùn)用邊緣檢測(cè)器
對(duì)灰度圖像運(yùn)用高斯模糊濾鏡以去除噪聲。然后調(diào)用OpenCV canny函數(shù)來(lái)檢測(cè)圖像中存在的邊緣。
blurred_image = cv2.GaussianBlur(gray_image, (5, 5), 0)
edged_img = cv2.Canny(blurred_image, 75, 200)
cv2.imshow('Image edges', edged_img)
cv2.waitKey(0)
邊緣在輸出上是可見的。

您將處理的邊緣是文檔的邊緣。
6、尋找最大的輪廓
檢測(cè)邊緣圖像中的輪廓。按降序排序,只保留五個(gè)最大的輪廓。通過(guò)循環(huán)排序后的輪廓,近似獲取最大的四邊輪廓。
cnts, _ = cv2.findContours(edged_img, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:5]
for c in cnts:
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.02 * peri, True)
if len(approx) == 4:
doc = approx
break
有四個(gè)邊的輪廓很可能含有文檔。
7、圈出文檔輪廓的四個(gè)角
圈出檢測(cè)到的文檔輪廓的幾個(gè)角。這將幫助您確定您的程序是否能夠檢測(cè)圖像中的文檔。
p = []
for d in doc:
tuple_point = tuple(d[0])
cv2.circle(img_resize, tuple_point, 3, (0, 0, 255), 4)
p.append(tuple_point)
cv2.imshow('Circled corner points', img_resize)
cv2.waitKey(0)
對(duì)調(diào)整后的RGB圖像圈出幾個(gè)角。

檢測(cè)到文檔之后,現(xiàn)在需要從圖像中提取文檔。
8、使用扭曲透視獲得所需的圖像
扭曲透視(warp perspective)是一種計(jì)算機(jī)視覺技術(shù),用于轉(zhuǎn)換圖像以糾正失真。它將圖像轉(zhuǎn)換成不同的平面,讓您可以從不同的角度查看圖像。
warped_image = perspective_transform(copy, doc.reshape(4, 2) * ratio)
warped_image = cv2.cvtColor(warped_image, cv2.COLOR_BGR2GRAY)
cv2.imshow("Warped Image", imutils.resize(warped_image, height=650))
cv2.waitKey(0)
為了獲得扭曲后的圖像,您需要?jiǎng)?chuàng)建一個(gè)簡(jiǎn)單的模塊來(lái)執(zhí)行透視轉(zhuǎn)換。
9、轉(zhuǎn)換模塊
該模塊將對(duì)文檔角的點(diǎn)進(jìn)行排序。它還會(huì)將文檔圖像轉(zhuǎn)換成不同的平面,并將相機(jī)角度更改為俯拍。
打開之前創(chuàng)建的那個(gè)transform.py文件,導(dǎo)入OpenCV庫(kù)和NumPy庫(kù)。
import numpy as np
import cv2
這個(gè)模塊將含有兩個(gè)函數(shù)。創(chuàng)建一個(gè)對(duì)文檔角點(diǎn)的坐標(biāo)進(jìn)行排序的函數(shù)。第一個(gè)坐標(biāo)將是左上角的坐標(biāo),第二個(gè)將是右上角的坐標(biāo),第三個(gè)將是右下角的坐標(biāo),第四個(gè)將是左下角的坐標(biāo)。
def order_points(pts):
# initializing the list of coordinates to be ordered
rect = np.zeros((4, 2), dtype = "float32")
s = pts.sum(axis = 1)
# top-left point will have the smallest sum
rect[0] = pts[np.argmin(s)]
# bottom-right point will have the largest sum
rect[2] = pts[np.argmax(s)]
'''computing the difference between the points, the
top-right point will have the smallest difference,
whereas the bottom-left will have the largest difference'''
diff = np.diff(pts, axis = 1)
rect[1] = pts[np.argmin(diff)]
rect[3] = pts[np.argmax(diff)]
# returns ordered coordinates
return rect
創(chuàng)建將計(jì)算新圖像的角坐標(biāo),并獲得俯拍的第二個(gè)函數(shù)。然后,它將計(jì)算透視變換矩陣,并返回扭曲的圖像。
def perspective_transform(image, pts):
# unpack the ordered coordinates individually
rect = order_points(pts)
(tl, tr, br, bl) = rect
'''compute the width of the new image, which will be the
maximum distance between bottom-right and bottom-left
x-coordinates or the top-right and top-left x-coordinates'''
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))
'''compute the height of the new image, which will be the
maximum distance between the top-left and bottom-left y-coordinates'''
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))
'''construct the set of destination points to obtain an overhead shot'''
dst = np.array([
[0, 0],
[maxWidth - 1, 0],
[maxWidth - 1, maxHeight - 1],
[0, maxHeight - 1]], dtype = "float32")
# compute the perspective transform matrix
transform_matrix = cv2.getPerspectiveTransform(rect, dst)
# Apply the transform matrix
warped = cv2.warpPerspective(image, transform_matrix, (maxWidth, maxHeight))
# return the warped image
return warped
現(xiàn)在您已創(chuàng)建了轉(zhuǎn)換模塊。perspective_transform導(dǎo)入方面的錯(cuò)誤現(xiàn)在將消失。

注意,顯示的圖像有俯拍。
10、運(yùn)用自適應(yīng)閾值,保存掃描輸出
在main.py文件中,對(duì)扭曲的圖像運(yùn)用高斯閾值。這將給扭曲的圖像一個(gè)掃描后的外觀。將掃描后的圖像輸出保存到含有程序文件的文件夾中。
T = threshold_local(warped_image, 11, offset=10, method="gaussian")
warped = (warped_image > T).astype("uint8") * 255
cv2.imwrite('./'+'scan'+'.png',warped)
以PNG格式保存掃描件可以保持文檔質(zhì)量。
11、顯示輸出
輸出掃描后文檔的圖像:
cv2.imshow("Final Scanned image", imutils.resize(warped, height=650))
cv2.waitKey(0)
cv2.destroyAllWindows()
下圖顯示了程序的輸出,即掃描后文檔的俯拍。

12、計(jì)算機(jī)視覺在如何進(jìn)步?
創(chuàng)建文檔掃描器涉及計(jì)算機(jī)視覺的一些核心領(lǐng)域,計(jì)算機(jī)視覺是一個(gè)廣泛而復(fù)雜的領(lǐng)域。為了在計(jì)算機(jī)視覺方面取得進(jìn)步,您應(yīng)該從事有趣味又有挑戰(zhàn)性的項(xiàng)目。
您還應(yīng)該閱讀如何將計(jì)算機(jī)視覺與當(dāng)前前技術(shù)結(jié)合使用方面的更多信息。這讓您能了解情況,并為所處理的項(xiàng)目提供新的想法。
原文鏈接:https://www.makeuseof.com/python-create-document-scanner/