如何利用opencv對(duì)拍攝圖片進(jìn)行文字識(shí)別
代碼示例:
import cv2 as cv import numpy as np import pytesseract from PIL import Image img = cv.imread('test.jpg') rows, cols, _ = img.shape img = cv.resize(img, (int(cols/2), int(rows/2))) img = cv.cvtColor(img, cv.COLOR_BGR2GRAY) nrows, ncols = img.shape print(cols, ncols, rows, nrows) gray_blurred = cv.GaussianBlur(img, (5, 5), 0) flag = 200 lines = [] while len(lines) != 4: # 使用Canny邊緣檢測(cè) edges = cv.Canny(gray_blurred, 50, 150, apertureSize=3) lines = cv.HoughLines(edges, 1, np.pi / 180, flag) if lines is None: lines = [] if flag < 80: raise Exception('未找到合適的邊緣處理參數(shù)') flag -= 5 print(flag) nlines = [] # 如果找到了直線,使用它們來計(jì)算仿射變換矩陣 if lines is not None: for rho, theta in lines[:, 0]: a = np.cos(theta) b = np.sin(theta) x0 = a * rho y0 = b * rho x1 = int(x0 + 1000 * (-b)) y1 = int(y0 + 1000 * (a)) x2 = int(x0 - 1000 * (-b)) y2 = int(y0 - 1000 * (a)) cv.line(img, (x1, y1), (x2, y2), (0, 0, 255), 2) nlines.append([(x1, y1), (x2, y2)]) points = [] for i in range(len(nlines) - 1): for j in range(i + 1, len(nlines)): line = nlines[i] x1, y1 = line[0] x2, y2 = line[1] line1 = nlines[j] x3, y3 = line1[0] x4, y4 = line1[1] try: u = ((x4-x3)*(y1-y3) - (y4-y3)*(x1-x3)) / ((y4-y3)*(x2-x1) - (x4-x3)*(y2-y1)) except Exception as e: continue x = x1 + u * (x2 - x1) y = y1 + u * (y2 - y1) if x > 0 and y > 0 and x < ncols and y < nrows: points.append((x, y)) pytesseract.pytesseract.tesseract_cmd = r'D:\Program Files\Tesseract-OCR\tesseract.exe' center = (int(ncols/2), int(nrows/2)) pstmap = {} for point in points: x, y = point cx, cy = center if x < cx and y < cy: pstmap['lt'] = point elif x > cx and y < cy: pstmap['rt'] = point elif x > cx and y > cy: pstmap['rb'] = point else: pstmap['lb'] = point pst1 = np.float32([pstmap['lt'], pstmap['rt'], pstmap['rb'], pstmap['lb']]) pst2 = np.float32([[0, 0], [ncols, 0], [ncols, nrows], [0, nrows]]) M = cv.getPerspectiveTransform(pst1, pst2) dst = cv.warpPerspective(img, M, (ncols, nrows)) x1, y1 = 0, 0 def mouse_callback(event, x, y, flags, param): global x1, y1 if event == cv.EVENT_LBUTTONDOWN: x1, y1 = x, y elif event == cv.EVENT_LBUTTONUP: x2, y2 = x, y wimg = dst[y1:y2, x1:x2] _, wimg = cv.threshold(wimg, 80, 255, cv.THRESH_BINARY) wimg = cv.bitwise_not(wimg) cv.imwrite('test_dst.jpg', wimg) image = Image.open('test_dst.jpg') # 打印選定區(qū)域的坐標(biāo) print(f"({x1}, {y1}) -> ({x2}, {y2})") print(pytesseract.image_to_string(image, lang='chi_sim')) cv.namedWindow('dst') cv.setMouseCallback("dst", mouse_callback) cv.imshow('img', img) cv.imshow('dst', dst) print(dst[2]) cv.waitKey(0) cv.destroyAllWindows()
方法:
1. 首先讀取圖片, 因?yàn)槲沂謾C(jī)拍攝圖片尺寸太大, 所以進(jìn)行了縮放
2. 對(duì)圖片進(jìn)行高斯模糊, 方便進(jìn)行邊緣處理
3. 從高到低適配不同的閾值檢測(cè)圖片內(nèi)容邊緣
4. 通過反向霍夫變換獲取確定邊緣直線的四個(gè)點(diǎn)
5. 通過直線兩兩相交確定四個(gè)定點(diǎn)
6. 進(jìn)行透視變換
7. 添加鼠標(biāo)事件, 監(jiān)測(cè)鼠標(biāo)選定區(qū)域
8. 鼠標(biāo)選定區(qū)域后, 裁剪圖片, 對(duì)圖片進(jìn)行二值化處理, 我這里做了文字黑白反轉(zhuǎn)
9. 利用pytesseract對(duì)裁剪后的圖片進(jìn)行文字識(shí)別
注意事項(xiàng):
1. 選擇的文字區(qū)域會(huì)影響識(shí)別成功率, 如果文字區(qū)域緊貼文字, 可能會(huì)失敗, 盲猜影響了特征提取
2. 圖片尺寸大小會(huì)影響邊緣檢測(cè), 不縮放圖片時(shí), 閾值調(diào)整不當(dāng)?shù)脑挘?很容易生成N條邊緣直線, 閾值怎么選定請(qǐng)了解霍夫變換的原理。
識(shí)別效果(加了二值化處理的準(zhǔn)確度會(huì)很好):
補(bǔ)充:幾個(gè)常用的OpenCV二值化代碼示例
1. 全局閾值二值化:
import cv2 img = cv2.imread('image.jpg', 0) _, thresh = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY) cv2.imshow('image', img) cv2.imshow('threshold', thresh) cv2.waitKey(0) cv2.destroyAllWindows()
2. 自適應(yīng)閾值二值化:
import cv2 img = cv2.imread('image.jpg', 0) thresh = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2) cv2.imshow('image', img) cv2.imshow('adaptive threshold', thresh) cv2.waitKey(0) cv2.destroyAllWindows()
3. Otsu二值化:
import cv2 img = cv2.imread('image.jpg', 0) _, thresh = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU) cv2.imshow('image', img) cv2.imshow('Otsu threshold', thresh) cv2.waitKey(0) cv2.destroyAllWindows()
這些示例代碼可以根據(jù)需要進(jìn)行修改和調(diào)整,以適應(yīng)不同的圖像處理任務(wù)。
總結(jié)
到此這篇關(guān)于如何利用opencv對(duì)拍攝圖片進(jìn)行文字識(shí)別的文章就介紹到這了,更多相關(guān)opencv對(duì)圖片文字識(shí)別內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python數(shù)據(jù)分析之真實(shí)IP請(qǐng)求Pandas詳解
這篇文章主要給大家介紹了Python數(shù)據(jù)分析之真實(shí)IP請(qǐng)求Pandas,文中通過示例嗲嗎給大家介紹的很詳細(xì),相信對(duì)大家的學(xué)習(xí)或者理解具有一定的參考借鑒價(jià)值,有需要的朋友們可以參考借鑒,下面來一起學(xué)習(xí)學(xué)習(xí)吧。2016-11-11Python如何存儲(chǔ)和讀取ASCII碼形式的byte數(shù)據(jù)
這篇文章主要介紹了Python如何存儲(chǔ)和讀取ASCII碼形式的byte數(shù)據(jù),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05解決Python獲取文件提示找不到指定路徑can‘t?open?file?'area.py':
這篇文章主要給大家介紹了關(guān)于如何解決Python獲取文件提示找不到指定路徑can‘t?open?file?'area.py':[Errno?2]?No?such?file?or?directory的相關(guān)資料,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-11-11python+opencv實(shí)現(xiàn)視頻抽幀示例代碼
下面是采用以幀數(shù)為間隔的方法進(jìn)行視頻抽幀,為了避免不符合項(xiàng)目要求的數(shù)據(jù)增強(qiáng),博主要求技術(shù)人員在錄制視頻時(shí)最大程度地讓攝像頭進(jìn)行移動(dòng)、旋轉(zhuǎn)以及遠(yuǎn)近調(diào)節(jié)等,對(duì)python opencv視頻抽幀示例代碼感興趣的朋友一起看看吧2021-06-06Python實(shí)現(xiàn)微信機(jī)器人的方法
這篇文章主要介紹了Python實(shí)現(xiàn)微信機(jī)器人的方法,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-09-09Python 獲取圖片GPS等信息鎖定圖片拍攝地點(diǎn)、拍攝時(shí)間(實(shí)例代碼)
這篇文章主要介紹了Python 獲取圖片GPS等信息鎖定圖片拍攝地點(diǎn)、拍攝時(shí)間,先把圖片以二進(jìn)制的格式讀取出來,然后通過 exifread 庫把里面的 GPS 信息提取出來,再以特定的格式打印出來,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07