|
发表于 2022-11-3 23:40:34
|
显示全部楼层
试试这个代码- function untited1()
- clc;
- I=imread('gongshi.png');%璇诲彇鍥剧墖
- subplot(3,3,1);imshow(I);
- I_gray = rgb2gray(I);
- subplot(3,3,2);imshow(I_gray);
- I_bw = I_gray<150;
- subplot(3,3,3);imshow(I_bw);
- [m,n]= size(I_bw);
- result = {I_bw};%
- direction=0;
- flag_cut = 1;
- name_lib = {'x','y','z','=','1','2','3','divide','+'};
- mat_lib = getMatLib(name_lib);
- while flag_cut
- flag_cut = 0;
- n_result = length(result);
- for i_result = 1:n_result
- if length(result{i_result})>10%如果长度大于10,认为是图片,而不是字符串
- result0 = cut_ex(result{i_result},direction);
- %判断上下标
- if direction ==0
- position =[];
- for i=1:length(result0)
- [height,center] = getHeightAndCenter(result0{i});
- position=[position;height,center];
- end
- center_average=mean(position(:,2));
- shift = (position(:,2)-center_average)/center_average;
- index_upper = shift<-0.3;
- end
- direction = 1-direction;
- i = 1;
- while i <= length(result0)
- I_trim = trim(result0{i});
- pos = recognize(mat_lib,name_lib,I_trim)
-
- %如果上标的话就加个.^符号比如 x2变成 x.^2
- if index_upper(i) == 1
- result0 ={ result0{1:i-1},'.^', result0{i:end}};
- index_upper=[index_upper(1:i-1);0;index_upper(i:end)];
- i = i+1;
- end
-
- %尝试识别
- if pos>0%如果识别出来,直接把图像矩阵换为识别结果字符
- result0{i} = name_lib{pos};
- i = i+1;
- else%否则更换切割方向,并且用括号把未识别部分括起来
- %如果不加括号,y=1/(x+1)可能就变为1=1/x+1,造成逻辑错误
- flag_cut = 1;
- result0 ={ result0{1:i-1},'(', result0{i},')', result0{i+1:end}};
- index_upper=[index_upper(1:i-1);0;index_upper(i);0;index_upper(i+1:end)];
- i = i+3;
- end
- end
- result =[ result(1:i_result-1) result0 result(i_result+1:end)];
- end%if
- end %for
- end
- state = [result{1:end}];
- state1 = strrep(state,'divide','./');
- x = 0.01:0.01:3;
- eval(state1);
- plot(x,y);
- expression = '=\((.+)\.\/(.+)\)';
- replace = '=\\frac {$1}{$2}';
- newStr = regexprep(state1,expression,replace)
- latexf=['$$',newStr,'$$'];
- lgh=legend(latexf);
- set(lgh,'interpreter','latex');
复制代码
|
|