唐宇迪ocr检测图片
生活随笔
收集整理的這篇文章主要介紹了
唐宇迪ocr检测图片
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
唐宇迪OCR檢測文檔
- 邊緣檢測
- 獲取輪廓
- 透視變換
- ocr識(shí)別
本文主要是學(xué)習(xí)唐宇迪ocr檢測圖片的工具和步驟
邊緣檢測
獲取輪廓
這兒所謂具有規(guī)則形狀的輪廓,必須要求有4個(gè)頂點(diǎn),不一定是矩形。
cv2.arcLength(c, True) 求的是輪廓周長
cv2.approxPolyDP(c, 0.02 * peri, True) 按照算法將輪廓填充完整,0.02 * peri是精度
透視變換
透視變換的具體代碼如下
def four_point_transform(image,pts):rect = order_points(pts)(tl,tr,br,bl) = rect# 計(jì)算輸入的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))# 變換后對(duì)應(yīng)坐標(biāo)位置dst = np.array([[0, 0],[maxWidth - 1, 0],[maxWidth - 1, maxHeight - 1],[0, maxHeight - 1]], dtype="float32")# 計(jì)算變換矩陣M = cv2.getPerspectiveTransform(rect, dst)warped = cv2.warpPerspective(image, M, (maxWidth, maxHeight))# 返回變換后結(jié)果return warped先求出輪廓坐標(biāo)連線的歐氏距離,取長和寬的最大值為一個(gè)矩形標(biāo)準(zhǔn),再求出變換矩陣M,把原來的四邊形變換到矩形標(biāo)準(zhǔn)框內(nèi)。
總結(jié)一下,第一次灰度轉(zhuǎn)換+濾波+邊緣檢測是為了求出輪廓,從而進(jìn)行坐標(biāo)點(diǎn)定位,第二次灰度轉(zhuǎn)換+二值處理是為了得到對(duì)比度更鮮明的圖片,方便ocr工具識(shí)別。
ocr識(shí)別
# https://digi.bib.uni-mannheim.de/tesseract/ # 配置環(huán)境變量如E:\Program Files (x86)\Tesseract-OCR # tesseract -v進(jìn)行測試 # tesseract XXX.png 得到結(jié)果 # pip install pytesseract # anaconda lib site-packges pytesseract pytesseract.py # tesseract_cmd 修改為絕對(duì)路徑即可 from PIL import Image import pytesseract import cv2 import ospreprocess = 'blur' #threshimage = cv2.imread('scan.jpg') gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)if preprocess == "thresh":gray = cv2.threshold(gray, 0, 255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]if preprocess == "blur":gray = cv2.medianBlur(gray, 3)filename = "{}.png".format(os.getpid()) cv2.imwrite(filename, gray)text = pytesseract.image_to_string(Image.open(filename)) print(text) os.remove(filename)cv2.imshow("Image", image) cv2.imshow("Output", gray) cv2.waitKey(0)對(duì)圖片進(jìn)行中值濾波,濾掉噪點(diǎn),ocr將識(shí)別結(jié)果打印到控制臺(tái)。
總結(jié)
以上是生活随笔為你收集整理的唐宇迪ocr检测图片的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: cesium label 显示隐藏到地底
- 下一篇: A*算法在最短路问题的应用及其使用举例