小編給大家分享一下如何實(shí)現(xiàn)Opencv圖片的OCR識(shí)別,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
創(chuàng)新互聯(lián)公司專注于企業(yè)營(yíng)銷(xiāo)型網(wǎng)站、網(wǎng)站重做改版、嘉蔭網(wǎng)站定制設(shè)計(jì)、自適應(yīng)品牌網(wǎng)站建設(shè)、H5頁(yè)面制作、商城網(wǎng)站開(kāi)發(fā)、集團(tuán)公司官網(wǎng)建設(shè)、成都外貿(mào)網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁(yè)設(shè)計(jì)等建站業(yè)務(wù),價(jià)格優(yōu)惠性價(jià)比高,為嘉蔭等各大城市提供網(wǎng)站開(kāi)發(fā)制作服務(wù)。導(dǎo)入相關(guān)函數(shù),遇到報(bào)錯(cuò)的話,直接pip install 函數(shù)名。
import numpy as np import argparse import cv2
參數(shù)初始化
ap = argparse.ArgumentParser() ap.add_argument("-i", "--image", required = True, help = "Path to the image to be scanned") args = vars(ap.parse_args())
Parameters:
--image images\page.jpg
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 image if width is None: r = height / float(h) dim = (int(w * r), height) else: r = width / float(w) dim = (width, int(h * r)) resized = cv2.resize(image, dim, interpolation=inter) return resized
讀取圖片后進(jìn)行重置大小,并計(jì)算縮放倍數(shù);進(jìn)行灰度化、高斯濾波以及Canny輪廓提取
image = cv2.imread(args["image"]) ratio = image.shape[0] / 500.0 orig = image.copy() image = resize(orig, height = 500) gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) gray = cv2.GaussianBlur(gray, (5, 5), 0) edged = cv2.Canny(gray, 75, 200)
檢測(cè)輪廓并排序,遍歷輪廓。
cnts = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[0]# 輪廓檢測(cè) cnts = sorted(cnts, key = cv2.contourArea, reverse = True)[:5]#保留前5個(gè)輪廓 # 遍歷輪廓 for c in cnts: # 計(jì)算輪廓近似 peri = cv2.arcLength(c, True)# 計(jì)算輪廓長(zhǎng)度,C表示輸入的點(diǎn)集,True表示輪廓是封閉的 #(C表示輸入的點(diǎn)集,epslion判斷點(diǎn)到相對(duì)應(yīng)的line segment 的距離的閾值,曲線是否閉合的標(biāo)志位) approx = cv2.approxPolyDP(c, 0.02 * peri, True) # 4個(gè)點(diǎn)的時(shí)候就拿出來(lái) if len(approx) == 4: screenCnt = approx break
畫(huà)出近似輪廓,透視變換,二值處理
cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 2) warped = four_point_transform(orig, screenCnt.reshape(4, 2) * ratio)#透視變換 # 二值處理 warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY) ref = cv2.threshold(warped, 100, 255, cv2.THRESH_BINARY)[1] cv2.imwrite('scan.jpg', ref)
鏈接: 下載
在環(huán)境變量、系統(tǒng)變量的Path里面添加安裝路徑,例如:E:\Program Files (x86)\Tesseract-OCR
tesseract -v#打開(kāi)命令行,進(jìn)行測(cè)試 tesseract XXX.png result#得到結(jié)果 pip install pytesseract#安裝依賴包
打開(kāi)python安裝路徑里面的python文件,例如C:\ProgramData\Anaconda3\Lib\site-packages\pytesseract\pytesseract.py
將tesseract_cmd 修改為絕對(duì)路徑即可,例如:tesseract_cmd = ‘C:/Program Files (x86)/Tesseract-OCR/tesseract.exe'
from PIL import Image import pytesseract import cv2 import os
讀取圖片、灰度化、濾波
image = cv2.imread('scan.jpg') gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) 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)
以上是“如何實(shí)現(xiàn)Opencv圖片的OCR識(shí)別”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!