OpenCV-文档扫描OCR识别-04
生活随笔
收集整理的這篇文章主要介紹了
OpenCV-文档扫描OCR识别-04
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
# 導入工具包
import numpy as np
import argparse
import cv2
def order_points(pts):# 初始化4個坐標點的矩陣rect = np.zeros((4, 2), dtype = "float32")# 按順序找到對應坐標0123分別是 左上,右上,右下,左下# 計算左上,右下print("pts :\n ",pts)s = pts.sum(axis = 1)# 沿著指定軸計算第N維的總和print("s : \n",s)rect[0] = pts[np.argmin(s)]# 即pts[1]rect[2] = pts[np.argmax(s)]# 即pts[3]print("第一次rect : \n",rect)# 計算右上和左下diff = np.diff(pts, axis = 1)# 沿著指定軸計算第N維的離散差值print("diff : \n",diff)rect[1] = pts[np.argmin(diff)]# 即pts[0]rect[3] = pts[np.argmax(diff)]# 即pts[2]print("第二次rect :\n ",rect)return rect
def four_point_transform(image, pts):# 獲取輸入坐標點rect = order_points(pts)(tl, tr, br, bl) = rect# 計算輸入的w和h值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],# 防止出錯,-1[maxWidth - 1, maxHeight - 1],[0, maxHeight - 1]], dtype = "float32")# 計算變換矩陣 (平移+旋轉+翻轉),其中M = cv2.getPerspectiveTransform(rect, dst)# (原坐標,目標坐標)print("M:",M)print("M.shape:",M.shape)warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight))# 返回變換后結果return warped
def resize(image, width=None, height=None, inter=cv2.INTER_AREA):dim = None(h, w) = image.shape[:2]if width is None and height is None:return imageif width is None:# 無w有h時r = height / float(h)# 新h與舊h的比例為rdim = (int(w * r), height)# 讓w也乘以這個比例,得到新welse:r = width / float(w)dim = (width, int(h * r))resized = cv2.resize(image, dim, interpolation=inter)return resized
if __name__ == "__main__":# 讀取輸入image = cv2.imread("receipt.jpg")# resize 坐標也會相同變化ratio = image.shape[0] / 500.0orig = image.copy()image = resize(orig, height = 500)# 同比例變化:h指定500,w也會跟著變化# 預處理gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)gray = cv2.GaussianBlur(gray, (5, 5), 0)edged = cv2.Canny(gray, 75, 200)# 展示預處理結果print("STEP 1: 邊緣檢測")cv2.imshow("Image", image)cv2.imshow("Edged", edged)cv2.waitKey(0)cv2.destroyAllWindows()# 輪廓檢測cnts = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[1]# cnts中可檢測到許多個輪廓,取前5個最大面積的輪廓cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:5]# 遍歷輪廓for c in cnts:# C表示輸入的點集# 計算輪廓近似peri = cv2.arcLength(c, True)# epsilon表示從原始輪廓到近似輪廓的最大距離,它是一個準確度參數# True表示封閉的approx = cv2.approxPolyDP(c, 0.02 * peri, True)print("approx:\n",approx)print("approx.shape",approx.shape)# 4個點的時候就拿出來,screenCnt是這4個點的坐標if len(approx) == 4:# 近似輪廓得到4個點,意味著可能得到的是矩形screenCnt = approx# 并且最大的那個輪廓是很有可能圖像的最大外圍break
安裝工具包
https://digi.bib.uni-mannheim.de/tesseract/
配置環境變量如E:\Program Files (x86)\Tesseract-OCR
tesseract -v進行測試
tesseract XXX.png 得到結果
pip install pytesseract
anaconda lib site-packges pytesseract pytesseract.py
tesseract_cmd 修改為絕對路徑即可
總結
以上是生活随笔為你收集整理的OpenCV-文档扫描OCR识别-04的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OpenCV-信用卡数字识别-03
- 下一篇: OpenCV-图像特征harris角点检