python实现文档图像倾斜矫正,实现类似扫描仪功能
生活随笔
收集整理的這篇文章主要介紹了
python实现文档图像倾斜矫正,实现类似扫描仪功能
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
圖片中文檔提取與矯正,實現(xiàn)類似掃描儀功能
- 第一個文檔,scan.py
- 第二個文檔,transform .py
- 第三個文檔,imgEnhance.py
- 效果圖
這幾天看見一個軟件,可以手機(jī)拍照一個文檔,自動提取出文檔后把歪曲的圖像矯正,就好像掃描出來的一樣,很有意思。作為圖像處理愛好者,手癢忍不住自己試試(^ o ^) 。廢話不多說,直接上代碼(我是代碼搬運工…):
第一個文檔,scan.py
from transform import four_point_transform import cv2, imutils import imgEnhancedef preProcess(image):ratio = image.shape[0] / 500.0image = imutils.resize(image, height=500)grayImage = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)gaussImage = cv2.GaussianBlur(grayImage, (5, 5), 0)edgedImage = cv2.Canny(gaussImage, 75, 200)cnts = cv2.findContours(edgedImage.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)cnts = cnts[0] if imutils.is_cv2() else cnts[1]cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:5]for c in cnts:peri = cv2.arcLength(c, True) # Calculating contour circumferenceapprox = cv2.approxPolyDP(c, 0.02 * peri, True)if len(approx) == 4:screenCnt = approxbreakreturn screenCnt, ratioif __name__ == "__main__":image = cv2.imread("image.jpg")screenCnt, ratio = preProcess(image)warped = four_point_transform(image, screenCnt.reshape(4, 2) * ratio)enhancer = imgEnhance.Enhancer()enhancedImg = enhancer.gamma(warped,1.63)cv2.imshow("org", imutils.resize(image, height=500))cv2.imshow("gamma", imutils.resize(enhancedImg, height=500))cv2.waitKey(0)cv2.destroyAllWindows()首先,一股腦預(yù)處理,然后提取文檔四個頂角坐標(biāo)(假設(shè)我們的文檔都是方形的,存在四個頂角,這個假設(shè)通常還是成立的)。毫無疑問,主要工作在提取四角旋轉(zhuǎn)函數(shù),這就上第二個文檔。
第二個文檔,transform .py
import numpy as np import cv2def order_points(pts):rect = np.zeros((4,2), dtype = "float32")s = np.sum(pts, 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 rectdef four_point_transform(image, pts):rect = order_points(pts)(tl, tr, br, bl) = rectwidthA = np.sqrt((tr[0] - tl[0]) ** 2 + (tr[1] - tl[1]) ** 2)widthB = np.sqrt((br[0] - bl[0]) ** 2 + (br[1] - bl[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先做一個order_points()函數(shù),搞清楚哪個是左上角、右上角、右下角、左下角,接著,計算下新圖像的長寬,然后旋轉(zhuǎn)起來吧。作為追求極致的人,當(dāng)然不能止步于此(^ _ ^),繼續(xù)折騰,圖像增強(qiáng),把圖片質(zhì)量搞好點,畢竟手機(jī)拍的照片有時光線不太好??次臋n三。
第三個文檔,imgEnhance.py
from PIL import ImageEnhance import numpy as np import cv2class Enhancer:def bright(self, image, brightness):enh_bri = ImageEnhance.Brightness(image)brightness = brightnessimageBrightend = enh_bri.enhance(brightness)return imageBrightenddef color(self, image, color):enh_col = ImageEnhance.Color(image)color = colorimageColored = enh_col.enhance(color)return imageColoreddef contrast(self, image, contrast):enh_con = ImageEnhance.Contrast(image)contrast = contrastimage_contrasted = enh_con.enhance(contrast)return image_contrasteddef sharp(self, image, sharpness):enh_sha = ImageEnhance.Sharpness(image)sharpness = sharpnessimage_sharped = enh_sha.enhance(sharpness)return image_sharpeddef gamma(self, image, gamma):# gamma_table = [np.power(x / 255.0, gamma) * 255.0 for x in range(256)]# gamma_table = np.round(np.array(gamma_table)).astype(np.uint8)# return cv2.LUT(image, gamma_table)gamma_image = np.power(image / float(np.max(image)), gamma)return gamma_image就是幾個常見的增強(qiáng),對比度、銳化、色彩、gamma矯正什么的,本來想裝高大上用下Retinex,還沒時間寫。手邊隨便抽了本書,試試效果圖如下。
效果圖
原圖
轉(zhuǎn)換后
哈
總結(jié)
以上是生活随笔為你收集整理的python实现文档图像倾斜矫正,实现类似扫描仪功能的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: linux convert 命令 把gi
- 下一篇: NYOJ 19 擅长排列的小名 next