# OpenCV+Python 文字识别（重点图像透视变换）

2020/01/29 21:08

 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
59 res = capture(src)
60 ocr_img(res)
61 cv.waitKey(0)
62 cv.destroyAllWindows()

0
0 收藏

0 评论
0 收藏
0