当前位置:   article > 正文

OpenCV(九)--文字扫描OCR识别_opencv ocr

opencv ocr

步骤:边缘检测+计算轮廓+变换+OCR

def show(name, img):
    cv2.imshow(name, img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

img = cv2.imread('tip.png')
show('img', img)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

在这里插入图片描述

img = cv2.resize(img, (500, int((500 * img.shape[0])/img.shape[1])))
# 预处理
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5, 5), 0)
# 边缘检测
edged = cv2.Canny(gray, 75, 200)
show('edged', edged)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

在这里插入图片描述

# 轮廓处理
cnts = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[0]
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:5]  # reverse=True表示倒序(从大到小)

for c in cnts:
    # 计算轮廓近似
    peri = cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, 0.02*peri, True)
    # 如果近似为一个矩形
    if len(approx) == 4:
        screenCnt = approx
        break

cv2.drawContours(img, [screenCnt], -1, (0, 255, 0),2)
show('img', img)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

在这里插入图片描述

# 透视变换
def four_point_transform(img, pts):
    p = sorted(pts, key=lambda pts: pts[0, 1], reverse=False)
    p = sorted(p, key=lambda p: p[0, 0], reverse=False)
    tl, bl, tr, br = [i[0] for i in p]
    # 计算边长度
    wA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2))
    wB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2))
    mw = max(int(wB), int(wA))+1

    hA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2))
    hB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2))
    mh = max(int(hB), int(hA))+1

    # 变换后对应坐标位置
    dst = np.array([
        [0, 0],
        [mw-20, 0],
        [mw-20, mh-20],
        [0, mh-20]
    ], dtype="float32")
    # 计算变换矩阵
    rect = np.zeros((4,2), dtype="float32")
    rect[0] = tl.astype(float).tolist()
    rect[1] = tr.astype(float).tolist()
    rect[2] = br.astype(float).tolist()
    rect[3] = bl.astype(float).tolist()
    M = cv2.getPerspectiveTransform(rect, dst)
    warped = cv2.warpPerspective(img, M, (mw, mh))
    return warped

warped = four_point_transform(orig, screenCnt)
show('warped', warped)
# 二值处理
warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY)
ref = cv2.threshold(warped, 180, 255, cv2.THRESH_BINARY)[1]
show('ref', ref)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

在这里插入图片描述
在这里插入图片描述

OCR识别

import pytesseract
from PIL import Image
cv2.imwrite("ocr.png", ref)
# 下载地址:https://github.com/UB-Mannheim/tesseract/wiki
# tesseract.exe的路径
pytesseract.pytesseract.tesseract_cmd = 'C:\Program Files\Tesseract-OCR\\tesseract.exe'
# 指定训练集的路径
tessdata_dir_config = r'--tessdata-dir "C:\Program Files\Tesseract-OCR\tessdata"'
test =pytesseract.image_to_string(Image.open("ocr.png"))
print(test)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

识别结果:
在这里插入图片描述

完整代码

import cv2  # opencv读取的格式是BGR
import matplotlib.pyplot as plt
import numpy as np

def show(name, img):
    cv2.imshow(name, img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

img = cv2.imread('tip.png')
# show('img', img)
ratio = img.shape[0] / 500.0
orig = img.copy()

img = cv2.resize(img, (500, int((500 * img.shape[0])/img.shape[1])))
# 预处理
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (5, 5), 0)
# 边缘检测
edged = cv2.Canny(gray, 75, 200)
# show('edged', edged)
# 轮廓处理
cnts = cv2.findContours(edged.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)[0]
cnts = sorted(cnts, key=cv2.contourArea, reverse=True)[:5]  # reverse=True表示倒序(从大到小)

for c in cnts:
    # 计算轮廓近似
    peri = cv2.arcLength(c, True)
    approx = cv2.approxPolyDP(c, 0.02*peri, True)
    # 如果近似为一个矩形
    if len(approx) == 4:
        screenCnt = approx
        break

cv2.drawContours(img, [screenCnt], -1, (0, 255, 0), 2)
# show('img', img)

# 透视变换
def four_point_transform(img, pts):
    p = sorted(pts, key=lambda pts: pts[0, 1], reverse=False)
    p = sorted(p, key=lambda p: p[0, 0], reverse=False)
    tl, bl, tr, br = [i[0] for i in p]
    # 计算边长度
    wA = np.sqrt(((br[0] - bl[0]) ** 2) + ((br[1] - bl[1]) ** 2))
    wB = np.sqrt(((tr[0] - tl[0]) ** 2) + ((tr[1] - tl[1]) ** 2))
    mw = max(int(wB), int(wA))+1

    hA = np.sqrt(((tr[0] - br[0]) ** 2) + ((tr[1] - br[1]) ** 2))
    hB = np.sqrt(((tl[0] - bl[0]) ** 2) + ((tl[1] - bl[1]) ** 2))
    mh = max(int(hB), int(hA))+1

    # 变换后对应坐标位置
    dst = np.array([
        [0, 0],
        [mw-20, 0],
        [mw-20, mh-20],
        [0, mh-20]
    ], dtype="float32")
    # 计算变换矩阵
    rect = np.zeros((4,2), dtype="float32")
    rect[0] = tl.astype(float).tolist()
    rect[1] = tr.astype(float).tolist()
    rect[2] = br.astype(float).tolist()
    rect[3] = bl.astype(float).tolist()
    M = cv2.getPerspectiveTransform(rect, dst)
    warped = cv2.warpPerspective(img, M, (mw, mh))
    return warped

warped = four_point_transform(orig, screenCnt)
# show('warped', warped)
# 二值处理
warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY)
ref = cv2.threshold(warped, 180, 255, cv2.THRESH_BINARY)[1]
show('ref', ref)
import pytesseract
from PIL import Image
cv2.imwrite("ocr.png", ref)
# 下载地址:https://github.com/UB-Mannheim/tesseract/wiki
# tesseract.exe的路径
pytesseract.pytesseract.tesseract_cmd = 'C:\Program Files\Tesseract-OCR\\tesseract.exe'
# 指定训练集的路径
tessdata_dir_config = r'--tessdata-dir "C:\Program Files\Tesseract-OCR\tessdata"'
test =pytesseract.image_to_string(Image.open("ocr.png"))
print(test)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小桥流水78/article/detail/771072
推荐阅读
相关标签
  

闽ICP备14008679号