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

基于Matlab的自然图像温度读数OCR提取问题求助

温度读数OCR问题的分析与解决

问题梳理

目标是用MATLAB批量处理图像提取温度读数,已完成灰度化预处理,通过MSERRegions生成掩码并追踪ROI轮廓,单个数字的边界框已生成,但存在以下问题:

  • 数字尺寸不一致,无法合并对应边界框
  • 数字位置不固定,无法设置固定ROI
  • 内置ocr函数返回"Eee",指定CharacterSet后返回空,自定义OCR模型训练失败

优化后的代码实现

imagefiles = dir(fullfile(CuredImages, '*.jpg'))

% 构造完整文件名
filename = fullfile(CuredImages, imagefiles(210).name);
% 读取图像
I = imread(filename);
% I = imrotate(imread(filename),90); % 有旋转需求时启用
imshow(I)

% MSER区域检测
[mserRegions, mserConnComp] = detectMSERFeatures(I, ... 
    "RegionAreaRange",[200 8000],"ThresholdDelta",4);

figure
imshow(I)
hold on
plot(mserRegions, "showPixelList", true,"showEllipses",false)
title("MSER区域")
hold off

mserStats = regionprops('table', mserConnComp,"BoundingBox",'Area', 'Eccentricity','PixelIdxList')

bboxes = vertcat(mserStats.BoundingBox);

% 转换边界框格式:[x y width height] → [xmin ymin xmax ymax]
xmin = bboxes(:,1);
ymin = bboxes(:,2);
xmax = xmin + bboxes(:,3) - 1;
ymax = ymin + bboxes(:,4) - 1;

% 提升边界框扩展比例,增强相邻数字区域的重叠性
expansionAmount = 0.1;
xmin = (1-expansionAmount) * xmin;
ymin = (1-expansionAmount) * ymin;
xmax = (1+expansionAmount) * xmax;
ymax = (1+expansionAmount) * ymax;

% 确保边界框不超出图像范围
xmin = max(xmin, 1);
ymin = max(ymin, 1);
xmax = min(xmax, size(I,2));
ymax = min(ymax, size(I,1));

% 展示扩展后的边界框
expandedBBoxes = [xmin ymin xmax-xmin+1 ymax-ymin+1];
IExpandedBBoxes = insertShape(I,"rectangle",expandedBBoxes,"LineWidth",3);

figure
imshow(IExpandedBBoxes)
title("扩展后的文本边界框")

% 计算边界框重叠率
overlapRatio = bboxOverlapRatio(expandedBBoxes, expandedBBoxes);

% 清除自身重叠率,简化图结构
n = size(overlapRatio,1); 
overlapRatio(1:n+1:n^2) = 0;

% 构建图结构识别连通区域
g = graph(overlapRatio);
componentIndices = conncomp(g);

% 合并同一连通组的边界框
xmin = accumarray(componentIndices', xmin, [], @min);
ymin = accumarray(componentIndices', ymin, [], @min);
xmax = accumarray(componentIndices', xmax, [], @max);
ymax = accumarray(componentIndices', ymax, [], @max);

% 转换回[x y width height]格式
textBBoxes = [xmin ymin xmax-xmin+1 ymax-ymin+1];

% 保留包含多个区域的边界框(温度读数为多数字组合)
numRegionsInGroup = histcounts(componentIndices);
textBBoxes(numRegionsInGroup <= 1, :) = [];

% 展示最终检测区域
ITextRegion = insertShape(I, "rectangle", textBBoxes,"LineWidth",3);

figure
imshow(ITextRegion)
title("检测到的文本区域")

% 优化OCR参数:指定仅识别数字和温度相关符号
ocrOptions = ocrOptions('CharacterSet', '0123456789.-');
ocrtxt = ocr(I, textBBoxes, ocrOptions);
disp(ocrtxt.Text)

核心优化说明

  • 边界框扩展调整:将扩展比例从0.05提升至0.1,让相邻数字的边界框更容易产生重叠,提升合并成功率
  • 过滤规则修正:原代码错误删除含2个区域的框,改为保留多区域框,匹配温度读数的多数字特征
  • OCR参数约束:明确指定字符集为数字、小数点和负号,排除无关字符干扰
  • 可选预处理增强:若图像噪声大,可在读取后添加I = imbinarize(I);或I = medfilt2(I);,强化文本对比度

额外建议

  • 若MSER误识别多,可缩小RegionAreaRange范围,过滤非数字区域
  • 尝试用detectTextFeatures替代MSER,该函数针对场景文本优化,区域合并效果更稳定
  • 自定义OCR模型训练需保证数据集覆盖不同尺寸、光照的温度数字样本,严格遵循MATLAB训练流程

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.20 21:32:52