1 # Author:Winter Liu is coming! 2 import cv2 as cv 3 import numpy as np 4 import pytesseract 5 6 7 # 預處理,高斯濾波(用處不大),4次開操作 8 # 過濾輪廓唯一 9 def contour_demo(img): 10 gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) 11 gray = cv.GaussianBlur(gray, (5, 5), 1) 12 ref, thresh = cv.threshold(gray, 127, 255, cv.THRESH_BINARY) 13 kernel = np.ones((9, 9), np.uint8) 14 thresh = cv.morphologyEx(thresh, cv.MORPH_OPEN, kernel, iterations=4) 15 contours, hierachy = cv.findContours(thresh, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE) 16 print(len(contours)) 17 return contours 18 19 20 def capture(img): 21 contours = contour_demo(img) 22 # 輪廓唯一,以后可以擴展 23 contour = contours[0] 24 # 求周長,可在后面的轉換中使用周長和比例 25 print(cv.arcLength(contour,True)) 26 img_copy = img.copy() 27 # 使用approxPolyDP,將輪廓轉換為直線,22為精度(越高越低),TRUE為閉合 28 approx = cv.approxPolyDP(contour, 22, True) 29 # print(approx.shape) 30 # print(approx) 31 # cv.drawContours(img_copy, [approx], -1, (255, 0, 0), 15) 32 n = [] 33 # 生產四個角的坐標點 34 for x, y in zip(approx[:, 0, 0], approx[:, 0, 1]): 35 n.append((x, y)) 36 p1 = np.array(n, dtype=np.float32) 37 # 對應點 38 p2 = np.array([(0, 0), (0, 1500), (1000, 1500), (1000, 0)], dtype=np.float32) 39 M = cv.getPerspectiveTransform(p1, p2) # 變換矩陣 40 # 使用透視變換 41 result = cv.warpPerspective(img_copy, M, (0, 0)) 42 # 重新截取 43 result = result[:1501, :1001] 44 cv.imwrite(r"C:\PycharmProjects\OpenCV\pic\ocr.png", result) 45 return result 46 47 48 # 圖像識別代碼,需要預先下載安裝開源工具包 pytesseract,配置環境變量 49 # pip install pytesseract 50 # 修改“C:\Python\Python37\Lib\site-packages\pytesseract\pytesseract.py”中“cmd”為絕對路徑 51 def ocr_img(img): 52 gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY) 53 # 圖像清晰度越高結果越精確,時間更長 54 text = pytesseract.image_to_string(gray) 55 print(text) 56 57 58 src = cv.imread(r"C:\PycharmProjects\OpenCV\pic\page.jpg") 59 res = capture(src) 60 ocr_img(res) 61 cv.waitKey(0) 62 cv.destroyAllWindows()