function robustFormulaRecognizer()
% 创建主窗口
fig = figure('Name', '鲁棒公式识别系统', 'NumberTitle', 'off', ...
'Position', [100, 100, 1200, 800], 'MenuBar', 'none', ...
'Color', [0.95 0.95 0.95]);
% 创建UI控件
uicontrol('Style', 'pushbutton', 'String', '上传图像', ...
'Position', [30, 750, 100, 30], 'Callback', @uploadImage, ...
'FontSize', 11, 'BackgroundColor', [0.3 0.6 0.9], 'ForegroundColor', 'white');
uicontrol('Style', 'pushbutton', 'String', '识别公式', ...
'Position', [150, 750, 100, 30], 'Callback', @recognizeFormula, ...
'FontSize', 11, 'BackgroundColor', [0.1 0.7 0.3], 'ForegroundColor', 'white');
uicontrol('Style', 'pushbutton', 'String', '清除结果', ...
'Position', [270, 750, 100, 30], 'Callback', @clearResults, ...
'FontSize', 11, 'BackgroundColor', [0.9 0.5 0.1], 'ForegroundColor', 'white');
% 创建图像显示区域
ax_original = axes('Parent', fig, 'Position', [0.05, 0.5, 0.4, 0.35]);
title(ax_original, '原始图像');
axis(ax_original, 'off');
ax_processed = axes('Parent', fig, 'Position', [0.55, 0.5, 0.4, 0.35]);
title(ax_processed, '处理图像');
axis(ax_processed, 'off');
ax_equal = axes('Parent', fig, 'Position', [0.05, 0.1, 0.4, 0.35]);
title(ax_equal, '等号检测');
axis(ax_equal, 'off');
% 创建结果展示区域
result_panel = uipanel('Title', '识别结果', 'FontSize', 12, ...
'BackgroundColor', 'white', 'Position', [0.55, 0.1, 0.4, 0.35]);
uicontrol('Parent', result_panel, 'Style', 'text', 'String', '识别公式:', ...
'Position', [20, 200, 80, 20], 'FontSize', 11, 'HorizontalAlignment', 'left');
formula_text = uicontrol('Parent', result_panel, 'Style', 'text', 'String', '', ...
'Position', [110, 200, 300, 20], 'FontSize', 11, 'HorizontalAlignment', 'left', ...
'BackgroundColor', 'white');
uicontrol('Parent', result_panel, 'Style', 'text', 'String', '计算结果:', ...
'Position', [20, 170, 80, 20], 'FontSize', 11, 'HorizontalAlignment', 'left');
calc_text = uicontrol('Parent', result_panel, 'Style', 'text', 'String', '', ...
'Position', [110, 170, 300, 20], 'FontSize', 11, 'HorizontalAlignment', 'left', ...
'BackgroundColor', 'white');
uicontrol('Parent', result_panel, 'Style', 'text', 'String', '用户答案:', ...
'Position', [20, 140, 80, 20], 'FontSize', 11, 'HorizontalAlignment', 'left');
answer_text = uicontrol('Parent', result_panel, 'Style', 'text', 'String', '', ...
'Position', [110, 140, 300, 20], 'FontSize', 11, 'HorizontalAlignment', 'left', ...
'BackgroundColor', 'white');
uicontrol('Parent', result_panel, 'Style', 'text', 'String', '验证结果:', ...
'Position', [20, 110, 80, 20], 'FontSize', 11, 'HorizontalAlignment', 'left');
result_text = uicontrol('Parent', result_panel, 'Style', 'text', 'String', '', ...
'Position', [110, 110, 300, 20], 'FontSize', 11, 'HorizontalAlignment', 'left', ...
'BackgroundColor', 'white');
% 等号检测日志
equal_log = uicontrol('Parent', result_panel, 'Style', 'listbox', ...
'String', {}, 'Position', [20, 30, 360, 70], 'FontSize', 10, ...
'BackgroundColor', 'white');
% 存储GUI句柄
handles = struct();
handles.ax_original = ax_original;
handles.ax_processed = ax_processed;
handles.ax_equal = ax_equal;
handles.formula_text = formula_text;
handles.calc_text = calc_text;
handles.answer_text = answer_text;
handles.result_text = result_text;
handles.equal_log = equal_log;
handles.current_image = [];
handles.binary_image = [];
handles.region_stats = [];
handles.region_bboxes = [];
handles.equal_sign_index = [];
handles.ocr_results = [];
guidata(fig, handles);
% ======================== 回调函数 ========================
function uploadImage(~, ~)
[filename, pathname] = uigetfile({'*.jpg;*.png;*.bmp', '图像文件 (*.jpg, *.png, *.bmp)'}, ...
'选择公式图像');
if isequal(filename, 0)
return;
end
img_path = fullfile(pathname, filename);
img = imread(img_path);
handles = guidata(fig);
handles.current_image = img;
% 显示原始图像
axes(handles.ax_original);
imshow(img);
title('原始图像');
% 预处理图像
gray_img = im2gray(img);
% 图像锐化 - 增强边缘
sharpened_img = imsharpen(gray_img, 'Radius', 1.5, 'Amount', 1.2, 'Threshold', 0.1);
% 自适应二值化
binary_img = imbinarize(sharpened_img, 'adaptive', 'Sensitivity', 0.7);
inverted_img = ~binary_img;
handles.binary_image = inverted_img;
% 显示处理图像
axes(handles.ax_processed);
imshow(inverted_img);
title('二值化图像');
% 重置结果
set(handles.formula_text, 'String', '');
set(handles.calc_text, 'String', '');
set(handles.answer_text, 'String', '');
set(handles.result_text, 'String', '');
set(handles.equal_log, 'String', {});
axes(handles.ax_equal);
cla;
title('等号检测');
guidata(fig, handles);
end
function recognizeFormula(~, ~)
handles = guidata(fig);
if isempty(handles.current_image)
errordlg('请先上传图像', '错误');
return;
end
try
inverted_img = handles.binary_image;
[img_h, img_w] = size(inverted_img);
% ================= 第一步:区域分割 =================
axes(handles.ax_processed);
imshow(inverted_img);
title('区域分割');
drawnow;
% 区域分割
cc = bwconncomp(inverted_img);
stats = regionprops(cc, 'BoundingBox', 'Area', 'Eccentricity', 'Orientation', 'Solidity');
% 过滤噪点
min_area = max(20, img_h*img_w*0.0005); % 动态最小面积
valid_idx = [stats.Area] > min_area;
stats = stats(valid_idx);
% 按水平位置排序(从左到右)
bboxes = vertcat(stats.BoundingBox);
[~, order] = sort(bboxes(:,1));
bboxes = bboxes(order, :);
stats = stats(order);
% 存储区域信息
handles.region_stats = stats;
handles.region_bboxes = bboxes;
% 显示分割结果
imshow(inverted_img); hold on;
for i = 1:size(bboxes,1)
rectangle('Position', bboxes(i,:), 'EdgeColor', [0.8 0.2 0.2], 'LineWidth', 1);
end
hold off;
title(sprintf('分割出 %d 个区域', length(stats)));
drawnow;
axes(handles.ax_equal);
imshow(inverted_img);
title('等号检测过程');
hold on;
log_entries = {};
% ================= 第二步:等号检测(从左到右扫描相邻区域)=================
equal_sign_index = [];
merged_regions = false(1, length(stats)); % 标记已合并的区域
for i = 1:length(stats)-1
if merged_regions(i), continue; end % 跳过已合并的区域
bbox1 = bboxes(i,:);
bbox2 = bboxes(i+1,:);
% 计算区域中心位置
center1_x = bbox1(1) + bbox1(3)/2;
center1_y = bbox1(2) + bbox1(4)/2;
center2_x = bbox2(1) + bbox2(3)/2;
center2_y = bbox2(2) + bbox2(4)/2;
% 计算区域间距
dx = abs(center1_x - center2_x);
dy = abs(center1_y - center2_y);
% 计算水平重叠度
overlap_x = min(bbox1(1)+bbox1(3), bbox2(1)+bbox2(3)) - max(bbox1(1), bbox2(1));
overlap_ratio = overlap_x / max(bbox1(3), bbox2(3));
% 等号检测条件:
% 1. 水平位置相近(水平中心距离小于最大宽度的60%)
% 2. 垂直方向分离(垂直中心距离大于最小高度的40%)
% 3. 水平重叠度大于30%
% 4. 两个区域高度相近(高度比在0.5-2之间)
height_ratio = min(bbox1(4), bbox2(4)) / max(bbox1(4), bbox2(4));
if dx < 0.6 * max(bbox1(3), bbox2(3)) && ... % 水平位置相近
dy > 0.2 * min(bbox1(4), bbox2(4)) && ... % 垂直方向分离
overlap_ratio > 0.3 && ... % 水平重叠足够
height_ratio > 0.5 && ... % 高度相近
height_ratio < 2
% 标记为等号候选
log_entries{end+1} = sprintf('区域 %d 和 %d 可能是等号组件', i, i+1);
log_entries{end+1} = sprintf('-- 水平距离: %.1f, 垂直距离: %.1f, 重叠率: %.2f', dx, dy, height_ratio);
% 提取区域图像进行分析
region_img1 = inverted_img(max(1,floor(bbox1(2))):min(img_h,floor(bbox1(2)+bbox1(4))-1), ...
max(1,floor(bbox1(1))):min(img_w,floor(bbox1(1)+bbox1(3))-1));
region_img2 = inverted_img(max(1,floor(bbox2(2))):min(img_h,floor(bbox2(2)+bbox2(4))-1), ...
max(1,floor(bbox2(1))):min(img_w,floor(bbox2(1)+bbox2(3))-1));
% 检查是否为横线结构
[is_line1, ~] = isHorizontalLine(region_img1);
[is_line2, ~] = isHorizontalLine(region_img2);
if is_line1 && is_line2
% 确定等号位置(使用第一个区域的索引)
equal_sign_index = i;
log_entries{end+1} = sprintf('确认区域 %d 和 %d 组成等号', i, i+1);
% 标记这两个区域
rectangle('Position', bbox1, 'EdgeColor', [0 0.8 0], 'LineWidth', 2);
rectangle('Position', bbox2, 'EdgeColor', [0 0.8 0], 'LineWidth', 2);
text(bbox1(1)+bbox1(3)/2, bbox1(2)-15, '=', ...
'Color', [0 0.5 0], 'FontSize', 20, 'FontWeight', 'bold', ...
'HorizontalAlignment', 'center');
% 标记这两个区域已合并
merged_regions(i) = true;
merged_regions(i+1) = true;
% 创建合并后的等号区域
merged_bbox = [
min(bbox1(1), bbox2(1)), ...
min(bbox1(2), bbox2(2)), ...
max(bbox1(1)+bbox1(3), bbox2(1)+bbox2(3)) - min(bbox1(1), bbox2(1)), ...
max(bbox1(2)+bbox1(4), bbox2(2)+bbox2(4)) - min(bbox1(2), bbox2(2))
];
% 更新区域列表(用合并区域替换原来的两个区域)
bboxes(i,:) = merged_bbox;
bboxes(i+1,:) = []; % 删除第二个区域
% 更新区域统计信息
merged_stats = stats(i);
merged_stats.BoundingBox = merged_bbox;
stats(i) = merged_stats;
stats(i+1) = [];
% 更新合并标记
merged_regions(i+1:end) = [];
merged_regions = [merged_regions(1:i) false(1, length(stats)-i)];
% 重绘区域编号
for j = 1:length(stats)
bbox = stats(j).BoundingBox;
rectangle('Position', bbox, 'EdgeColor', [0.8 0.2 0.2], 'LineWidth', 1);
text(bbox(1)+5, bbox(2)-10, sprintf('%d', j), ...
'Color', [0.2 0.2 0.8], 'FontSize', 10, 'FontWeight', 'bold');
end
break; % 找到一个等号后停止搜索
else
log_entries{end+1} = sprintf('区域 %d 和 %d 不符合横线特征', i, i+1);
end
end
end
% 如果没有找到等号,使用中间位置作为等号
if isempty(equal_sign_index)
[~, eq_idx] = min(abs(1:size(bboxes,1) - size(bboxes,1)/2));
log_entries{end+1} = sprintf('未找到等号,使用区域 %d 作为等号', eq_idx);
equal_sign_index = eq_idx;
% 标记等号区域
bbox = bboxes(eq_idx,:);
rectangle('Position', bbox, 'EdgeColor', [0.9 0.6 0], 'LineWidth', 3);
text(bbox(1)+bbox(3)/2, bbox(2)-15, '=', ...
'Color', [0.7 0.4 0], 'FontSize', 20, 'FontWeight', 'bold', ...
'HorizontalAlignment', 'center');
end
hold off;
% 更新日志
set(handles.equal_log, 'String', log_entries);
% 存储等号位置和更新后的区域信息
handles.equal_sign_index = equal_sign_index;
handles.region_stats = stats;
handles.region_bboxes = bboxes;
% ================= 第三步:字符识别 =================
ocr_results = cell(1, length(stats));
for i = 1:length(stats)
% 如果是等号区域,直接标记
if i == equal_sign_index
ocr_results{i} = '=';
continue;
end
bbox = stats(i).BoundingBox;
x = floor(bbox(1));
y = floor(bbox(2));
w = max(floor(bbox(3)), 1);
h = max(floor(bbox(4)), 1);
% 提取区域图像
region_img = inverted_img(max(1,y):min(img_h,y+h-1), max(1,x):min(img_w,x+w-1));
% 运算符特征检测
[is_operator, operator_type] = detectOperator(region_img);
if is_operator
ocr_results{i} = operator_type;
else
% OCR识别数字和字母
char_set = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
ocr_result = ocr(region_img, 'CharacterSet', char_set, 'TextLayout', 'Block');
if ~isempty(ocr_result.Text)
char = strtrim(ocr_result.Text);
% 数字校正
if strcmp(char, '7') || strcmp(char, '1')
char = correctDigit(region_img, char);
end
ocr_results{i} = char;
else
ocr_results{i} = '?';
end
end
end
% ================= 第四步:构建表达式 =================
% 分割表达式和答案
expr_str = '';
answer_str = '';
for i = 1:length(ocr_results)
if i < equal_sign_index
expr_str = [expr_str ocr_results{i}];
elseif i > equal_sign_index
answer_str = [answer_str ocr_results{i}];
end
end
% 特殊字符转换
expr_str = strrep(expr_str, 'x', '*');
expr_str = strrep(expr_str, 'X', '*');
expr_str = strrep(expr_str, '÷', '/');
expr_str = strrep(expr_str, ' ', '');
% 尝试转换为数值
try
user_answer = str2double(answer_str);
catch
user_answer = NaN;
end
% 安全计算表达式
try
correct_result = eval(expr_str);
catch
% 修正常见OCR错误
expr_str = strrep(expr_str, 'O', '0');
expr_str = strrep(expr_str, 'o', '0');
expr_str = strrep(expr_str, 'l', '1');
expr_str = strrep(expr_str, 'I', '1');
expr_str = strrep(expr_str, 's', '5');
correct_result = eval(expr_str);
end
% 验证结果
is_correct = abs(user_answer - correct_result) < 1e-6;
% 更新结果展示
set(handles.formula_text, 'String', [expr_str '=' answer_str]);
set(handles.calc_text, 'String', num2str(correct_result));
set(handles.answer_text, 'String', num2str(user_answer));
if is_correct
set(handles.result_text, 'String', '✓ 答案正确', 'ForegroundColor', [0 0.6 0]);
else
set(handles.result_text, 'String', '✗ 答案错误', 'ForegroundColor', [0.8 0 0]);
end
% 显示最终识别结果
axes(handles.ax_processed);
imshow(inverted_img); hold on;
for i = 1:length(stats)
bbox = stats(i).BoundingBox;
% 等号区域用绿色标注
if i == equal_sign_index
rectangle('Position', bbox, 'EdgeColor', [0.2 0.8 0.2], 'LineWidth', 2);
text(bbox(1)+5, bbox(2)-10, '=', ...
'Color', [0 0.5 0], 'FontSize', 12, 'FontWeight', 'bold');
else
% 运算符区域用不同颜色标注
if strcmp(ocr_results{i}, '+')
rect_color = [0.8 0.2 0.8]; % 紫色
text_color = [0.6 0 0.6]; % 深紫色
elseif strcmp(ocr_results{i}, '-')
rect_color = [0.8 0.5 0.2]; % 橙色
text_color = [0.6 0.3 0]; % 深橙色
else
rect_color = [1 0 0]; % 红色
text_color = [0 0 1]; % 蓝色
end
rectangle('Position', bbox, 'EdgeColor', rect_color, 'LineWidth', 1.5);
if ~isempty(ocr_results{i})
text(bbox(1)+5, bbox(2)-10, ocr_results{i}, ...
'Color', text_color, 'FontSize', 10, 'FontWeight', 'bold');
end
end
end
hold off;
title('最终识别结果');
% 保存处理后的数据
handles.ocr_results = ocr_results;
guidata(fig, handles);
catch ME
errordlg(sprintf('识别失败: %s', ME.message), '错误');
end
end
function clearResults(~, ~)
handles = guidata(fig);
% 清除结果文本
set(handles.formula_text, 'String', '');
set(handles.calc_text, 'String', '');
set(handles.answer_text, 'String', '');
set(handles.result_text, 'String', '');
set(handles.equal_log, 'String', {});
% 清除图像
axes(handles.ax_processed);
cla;
title('处理图像');
axes(handles.ax_equal);
cla;
title('等号检测');
% 如果有原始图像,重新显示
if ~isempty(handles.current_image)
axes(handles.ax_original);
imshow(handles.current_image);
title('原始图像');
end
end
end
% ======================== 辅助函数 ========================
function [is_line, confidence] = isHorizontalLine(img)
% 检测区域是否为水平线
[h, w] = size(img);
% 基本检查
if h == 0 || w == 0 || sum(img(:)) == 0
is_line = false;
confidence = 0;
return;
end
% 计算特征
aspect_ratio = w / h;
fill_ratio = sum(img(:)) / (h*w);
% 水平投影
horizontal_proj = sum(img, 2);
max_proj = max(horizontal_proj);
if max_proj == 0
is_line = false;
confidence = 0;
return;
end
% 找到主要行
[~, main_row] = max(horizontal_proj);
line_height = sum(horizontal_proj > 0.5 * max_proj);
% 置信度计算
aspect_conf = min(1, aspect_ratio / 3); % 宽高比越大越好
height_conf = max(0, 1 - (line_height-1)/3); % 行高越接近1越好
fill_conf = min(1, fill_ratio * 3); % 填充率适中
confidence = 0.5 * aspect_conf + 0.3 * height_conf + 0.2 * fill_conf;
% 决策阈值
is_line = confidence > 0.2 && aspect_ratio > 1.1 && line_height <= 8;
end
% ======================== 运算符检测函数 ========================
function [is_operator, operator_type] = detectOperator(region_img)
[h, w] = size(region_img);
% 计算形状特征
aspect_ratio = w / h;
eccentricity = regionprops(region_img, 'Eccentricity').Eccentricity;
is_operator = false;
operator_type = '';
% 减号检测
if aspect_ratio > 3 && eccentricity > 0.9
is_operator = true;
operator_type = '-';
return;
end
% 加号检测
if aspect_ratio > 1.2 && aspect_ratio < 2.5
% 中心区域分析
center_y = round(h/2);
center_x = round(w/2);
% 检查水平和垂直线段
horizontal_line = sum(region_img(center_y, :)) > w*0.7;
vertical_line = sum(region_img(:, center_x)) > h*0.7;
if horizontal_line && vertical_line
is_operator = true;
operator_type = '+';
return;
end
end
% 乘号检测(斜线)
if aspect_ratio > 0.8 && aspect_ratio < 1.2
% 使用Hough变换检测斜线
[H, theta, rho] = hough(region_img, 'Theta', -45:5:45);
peaks = houghpeaks(H, 2);
if size(peaks,1) >= 2
angles = theta(peaks(:,2));
angle_diff = abs(diff(angles));
% 检查是否接近垂直的斜线对
if abs(angle_diff) > 80 && abs(angle_diff) < 100
is_operator = true;
operator_type = '*';
end
end
end
end
% ======================== 数字校正函数 ========================
function char = correctDigit(region_img, original_char)
[h, w] = size(region_img);
% 1的特征:高宽比大,顶部无横线,底部较宽
aspect_ratio = h / w;
top_region = region_img(1:round(h*0.3), :);
bottom_region = region_img(round(h*0.7):end, :);
top_pixels = sum(top_region(:));
bottom_pixels = sum(bottom_region(:));
% 7的特征:顶部有横线,右上角有折角
top_line = sum(region_img(1, :)) > w*0.6;
right_top_corner = region_img(1, end) && region_img(2, end);
if strcmp(original_char, '7')
% 检查是否为1的特征
if aspect_ratio > 3 && top_pixels < numel(top_region)*0.2 && bottom_pixels > numel(bottom_region)*0.5
char = '1';
return;
end
elseif strcmp(original_char, '1')
% 检查是否为7的特征
if aspect_ratio < 2 && top_line && right_top_corner
char = '7';
return;
end
end
char = original_char; % 保持原识别结果
end
代码运行显示,运算符的使用无效,是否是识别不出来各个字符