利用opencv对拍摄图片进行文字识别

python 复制代码
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边缘检测
    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('未找到合适的边缘处理参数')
    flag -= 5
print(flag)
nlines = []
# 如果找到了直线,使用它们来计算仿射变换矩阵
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')
        # 打印选定区域的坐标
        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. 首先读取图片, 因为我手机拍摄图片尺寸太大, 所以进行了缩放

  2. 对图片进行高斯模糊, 方便进行边缘处理

  3. 从高到低适配不同的阈值检测图片内容边缘

  4. 通过反向霍夫变换获取确定边缘直线的四个点

  5. 通过直线两两相交确定四个定点

  6. 进行透视变换

  7. 添加鼠标事件, 监测鼠标选定区域

  8. 鼠标选定区域后, 裁剪图片, 对图片进行二值化处理, 我这里做了文字黑白反转

  9. 利用pytesseract对裁剪后的图片进行文字识别

注意事项:

  1. 选择的文字区域会影响识别成功率, 如果文字区域紧贴文字, 可能会失败, 盲猜影响了特征提取

  2. 图片尺寸大小会影响边缘检测, 不缩放图片时, 阈值调整不当的话, 很容易生成N条边缘直线, 阈值怎么选定请了解霍夫变换的原理。

识别效果(加了二值化处理的准确度会很好):

相关推荐
kay_5456 分钟前
YOLO26改进| 特征融合 | 小波变换的多尺度特征融合
人工智能·目标检测·计算机视觉·目标跟踪·论文·yolo26·yolo26改进
木心术112 分钟前
如何使用AI agent基于产品技术手册和标准协议完成FPGA寄存器的自动化配置、代码修改和编译的完整方案
人工智能·fpga开发·自动化
暗夜猎手-大魔王12 分钟前
转载--AI Agent 架构设计:工具调用失败了怎么办——重试、换方案、还是报告(OpenClaw、Claude Code、Hermes Agent 对比)
人工智能
Deepoch20 分钟前
面向工业现场自主运维:Deepoc 具身模型开发板的端侧智能升级路径
运维·人工智能·科技·巡检机器人·deepoc
科技牛牛25 分钟前
AI爬虫引爆代理IP产业:一场正在发生的数据粮草争夺战
人工智能·爬虫·tcp/ip·数据安全·ip地址查询
涤生大数据1 小时前
AI时代,SQL该何去何从?
数据库·人工智能·sql
冬奇Lab1 小时前
RAG 系列(十一):Rerank——让检索结果按重要性排队
人工智能·llm·源码
冬奇Lab1 小时前
一天一个开源项目(第96篇):OpenHarness - 轻量级 AI 代理基础设施框架
人工智能·开源·资讯
lulu12165440781 小时前
JetBrains IDE 终极AI编程方案:CC GUI插件让Claude Code和Codex丝滑运行
java·ide·人工智能·python·ai编程
TENSORTEC腾视科技2 小时前
腾视科技重磅推出AI NAS,重塑数据管理方式,开启智能高效新时代
人工智能·ai·七牛云存储·nas·企业存储·ainas·家庭存储