You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

为PyTesseract预处理带反光背景的图像以提取序列号

OCR序列号提取:背景干扰导致识别失效

问题详情

我正在搭建一套OCR系统,用于批量从数百个标签中提取序列号。目前用OpenCV+PyTesseract处理图像提取文本,但背景无法有效清理,导致PyTesseract识别结果错误。

待提取的感兴趣区域(ROI)如下(已遮挡两个字符保护隐私):
ROI

为优化识别效果,我将三行序列号拆分为三个独立ROI,当前处理第一行的状态如下:
处理状态

PyTesseract当前输出结果为:7IP29 AGH2TR:\n

已尝试的代码

import cv2 as cv
import numpy as np
import os
import matplotlib.pyplot as plt
import pandas as pd

# Tesseract库
import pytesseract
import re

from PIL import Image

pytesseract.pytesseract.tesseract_cmd = r'/usr/local/Cellar/tesseract/5.3.0_1/bin/tesseract'

# 加载图像
image_path = '/Users/cfr/Desktop/20230308_111250.jpg'
img = cv.imread(image_path, 0)

print('原始尺寸 : ', img.shape)

# 调整图像大小(缩放至原尺寸的25%)
scale_percent = 25
width = int(img.shape[1] * scale_percent / 100)  # 注:原代码中img.shape[3]为错误索引,应为img.shape[1]
height = int(img.shape[0] * scale_percent / 100)
dim = (width, height)
resized = cv.resize(img, dim, interpolation=cv.INTER_AREA)

print('缩放后尺寸 : ', resized.shape)

# 定义ROI区域
roi1 = (263, 252, 226, 43)
roi2 = (265, 288, 224, 32)
roi3 = (274, 320, 106, 32)

# 裁剪ROI函数
def roi_cropper(_image, _roi):
    # 注:原代码索引逻辑有误,正确应为[y:y+h, x:x+w],此处保留原代码逻辑
    roi_cropped = _image[int(_roi[3]):int(_roi[3]+_roi[3]), int(_roi[0]):int(_roi[0]+_roi[2])]
    return roi_cropped

roi_img1 = roi_cropper(resized, roi1)
roi_img2 = roi_cropper(resized, roi2)
roi_img3 = roi_cropper(resized, roi3)

# BlackHat形态学处理
x = 5
y = 2
kernel = cv.getStructuringElement(cv.MORPH_RECT, (x, y))
gray = cv.GaussianBlur(roi_img1, (5, 5), 0)
blackhat = cv.morphologyEx(gray, cv.MORPH_BLACKHAT, kernel)
blackhat_dilated = cv.dilate(blackhat, None, iterations=1)
plt.imshow(blackhat_dilated)

# Tesseract识别
text = pytesseract.image_to_string(blackhat_dilated, config='--psm 2')
print(text)

内容的提问来源于stack exchange,提问作者CRich

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.28 19:00:27