OpenCV+python文字識別


# Author:Winter Liu is coming!
import cv2 as cv
import numpy as np
import pytesseract


# 預處理,高斯濾波(用處不大),4次開操作
# 過濾輪廓唯一
def contour_demo(img):
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
gray = cv.GaussianBlur(gray, (5, 5), 1)
ref, thresh = cv.threshold(gray, 127, 255, cv.THRESH_BINARY)
kernel = np.ones((9, 9), np.uint8)
thresh = cv.morphologyEx(thresh, cv.MORPH_OPEN, kernel, iterations=4)
contours, hierachy = cv.findContours(thresh, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
print(len(contours))
return contours


def capture(img):
contours = contour_demo(img)
# 輪廓唯一,以后可以擴展
contour = contours[0]
# 求周長,可在后面的轉換中使用周長和比例
print(cv.arcLength(contour,True))
img_copy = img.copy()
# 使用approxPolyDP,將輪廓轉換為直線,22為精度(越高越低),TRUE為閉合
approx = cv.approxPolyDP(contour, 22, True)
# print(approx.shape)
# print(approx)
# cv.drawContours(img_copy, [approx], -1, (255, 0, 0), 15)
n = []
# 生產四個角的坐標點
for x, y in zip(approx[:, 0, 0], approx[:, 0, 1]):
n.append((x, y))
p1 = np.array(n, dtype=np.float32)
# 對應點
p2 = np.array([(0, 0), (0, 1500), (1000, 1500), (1000, 0)], dtype=np.float32)
M = cv.getPerspectiveTransform(p1, p2) # 變換矩陣
# 使用透視變換
result = cv.warpPerspective(img_copy, M, (0, 0))
# 重新截取
result = result[:1501, :1001]
cv.imwrite(r"C:\PycharmProjects\OpenCV\pic\ocr.png", result)
return result


# 圖像識別代碼,需要預先下載安裝開源工具包 pytesseract,配置環境變量
# pip install pytesseract
# 修改“C:\Python\Python37\Lib\site-packages\pytesseract\pytesseract.py”中“cmd”為絕對路徑
def ocr_img(img):
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
# 圖像清晰度越高結果越精確,時間更長
text = pytesseract.image_to_string(gray)
print(text)


src = cv.imread(r"C:\PycharmProjects\OpenCV\pic\page.jpg")
res = capture(src)
ocr_img(res)
cv.waitKey(0)
cv.destroyAllWindows()


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM