时间数字识别项目之大体完成

大框已经接近收尾。接下来剩下:

1)噪声添加

2)类标返回

3)汉字负样本添加

4)困难样本回收训练

5)......

这些细节,更加提高准确率。

  1 %{
  2 clear all;
  3 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  4 % 可调参数
  5 
  6 %test_path='C:\Users\cong\Desktop\研一实战\项目\图像中时间数字识别\OCR\one\22.jpg';%####
  7 %test_path='C:\Users\cong\Desktop\研一实战\项目\图像中时间数字识别\big.jpg';
  8 %target_digit=2;
  9 %local_step=3;%####
 10 
 11 %滑窗检测,窗口尺度,步长
 12 % win_h=20;
 13 % win_w=20;
 14 
 15 % step=1; 
 16 
 17 disp('参数配置成功...');
 18 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 19 disp('正在读入图片及特征提取...');
 20 %读入图片及特征提取
 21 %data=[];
 22 %label=[];
 23 temp_r1=0;
 24 temp_c1=0;
 25 k_data=1;
 26 %k_label=1;
 27 for i_digit=0:1:10
 28 %     if(i_digit==target_digit)                                %%%%%%%%%%%%%%%%%%%%%%
 29 %         this_image_label=1;
 30 %     end
 31     %数字转字符
 32     str=num2str(i_digit);                                          %%数据是不是不平衡
 33     path_temp=strcat('C:\Users\cong\Desktop\研一实战\项目\图像中时间数字识别\trainingSample\',str,'\');
 34     %path_temp=strcat('C:\Users\cong\Desktop\研一实战\项目\图像中时间数字识别\trainingSample\',str,'\');
 35     %path_temp=strcat('C:\Users\cong\Desktop\研一实战\项目\图像中时间数字识别\trainingSample\num',str,'\');
 36     file=dir(path_temp);
 37     for i=3:length(file)
 38         path= strcat(path_temp,file(i).name);
 39 
 40         %%%%%%%%%%%%%%%%%%%%%%%%%%
 41         % 加载图片
 42         %%%%%%%%%%%%%%%%%%%%%%%%%%
 43         I=imread(path);
 44        % if(i_digit==10)
 45        % I=imresize(I,[80 70]);
 46         % end
 47         %I=imread('E:/WeChat.jpg');
 48         %%%%%%%%%%%%%%%%%%%%%%%%%%
 49         % 提取channel features
 50         %%%%%%%%%%%%%%%%%%%%%%%%%%
 51         %[all_channel_difference_features,temp_r1,temp_c1]=channel_feature_2_vector(I,1);
 52         %data=[data,all_channel_difference_features];
 53         %[d3_data,~,~]=channel_feature_2_vector(I,1);
 54         pChns=chnsCompute();
 55         chns = chnsCompute( I, pChns );
 56         %all_channel_difference_features=cat(3,chns.data{:});
 57         %data(:,:,:,k_data)=single(d3_data);
 58         data(:,:,:,k_data)=cat(3,chns.data{:});
 59         label_t(1,k_data)=i_digit;
 60         k_data=k_data+1;
 61         %d3_data=channel_feature_2_vector(I,1);
 62         %data=[data,d3_data];
 63         %label=[label;i_digit];%类标从0开始
 64         if(rem(i,500)==0)
 65             disp('...');
 66         end
 67     end % for i=3:length(file)
 68     disp('数字')
 69     i_digit
 70     disp('的特征提取完毕...');
 71 end  % for i_digit=0:9
 72 disp('读入图片及特征提取完毕...');
 73 %%%%%%%%%%%%%%%%%%%%%%%%%%
 74 % 扔进分类器中,训练
 75 %%%%%%%%%%%%%%%%%%%%%%%%%%
 76 %参数设置
 77 %data=data';
 78 param.nclass = 11; % number of classes
 79 param.ntree = 100; % number of tree
 80 param.depth = 16; % the maximum depth of tree
 81 param.sigma = 5; % random parameter
 82 
 83 %%
 84 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 85 %训练
 86 disp('正在训练,请稍等...');
 87 handle = crndforest(); % call constructor
 88 
 89 %data=single(data);
 90 label=int32(label_t);
 91 crndforest(handle, 'train', data, label, param); %
 92 crndforest(handle, 'save', 'forest_model2'); % optionally save into disk
 93 crndforest(handle, 'delete'); % release object
 94 disp('训练完毕...');
 95 %}
 96 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 97 %测试
 98 %{
 99 clear all;
100 data_num=1;
101 disp('正在测试...');
102 handle = crndforest();
103 crndforest(handle, 'load', 'forest_model2');
104 %载入图像
105 %输入data,提取特征
106 test_digit=2;%%%%%%%%%%%%%%%%%%%%%
107 all_test_result=[];
108 %all_test_backgroud=[];
109 %all_channel_difference_features=[];
110 for test_digit=0:10
111     %test_backgroud=[];
112     test_result=0;
113     str=num2str(test_digit);
114     path_temp=strcat('C:\Users\cong\Desktop\研一实战\项目\图像中时间数字识别\trainingSample\no',str,'\');
115     file=dir(path_temp);
116     
117     for i=3:length(file)
118         path= strcat(path_temp,file(i).name);
119         I=imread(path); 
120        % I=imresize(I,[80 70]);
121         %[all_channel_difference_features,temp_r1,temp_c1]=channel_feature_2_vector(I,1);%all_channel_difference_features就是data
122         %随机森林预测
123 
124         pChns=chnsCompute();
125         chns = chnsCompute( I, pChns );
126         all_channel_difference_features(:,:,:,data_num)=cat(3,chns.data{:});
127         label_test_t(data_num)=test_digit;
128         %all_channel_difference_features=single(all_channel_difference_features);
129         data_num=data_num+1; 
130     end
131     data_num=1;
132     [pred_label, prob] =crndforest(handle, 'predict', all_channel_difference_features);
133     label_test=int32(label_test_t);
134     label_test=label_test';
135     test_result=sum(pred_label==label_test);
136     test_result
137     test_result=test_result/(length(file)-2);
138 %     test_backgroud
139     %test_result=test_result/198;
140     all_test_result=[ all_test_result,test_result];
141     %all_test_backgroud=[ all_test_backgroud,test_backgroud];
142     disp('检测数字:')
143     test_digit+1
144 end    
145 %输出结果
146 disp('恭喜恭喜,结果出来啦!')
147 all_test_result
148 crndforest(handle, 'delete');
149 disp('测试完毕...');
150 %}
151 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
152 %}
153 % 检测,测试
154 %参数设置
155 clear all;
156 scale_num=5;
157 scale_factor=1.2;
158 all_channel_difference_features=single(zeros(11,8,10,2));%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%先这么滴
159 %输入图,并提取特征
160 test_path='E:\研一实战\项目\图像中时间数字识别\OCR\one\3.jpg';
161 I=imread(test_path);
162 pChns=chnsCompute();
163 chns = chnsCompute( I, pChns );
164 channel_feature=cat(3,chns.data{:});
165 [ch_r,ch_c,ch_num]=size(channel_feature);
166 %原图和通道特征图的比例
167 [I_r,I_c,~]=size(I);
168 radio_r=I_r/ch_r;
169 radio_c=I_c/ch_c;
170 %多个尺度对原图处理
171 %数据结构
172 scale_factor_t(1)=scale_factor;
173 for i=2:scale_num
174     scale_factor_t(i)=scale_factor_t(i-1)*scale_factor;
175 end
176 
177 
178 for i=1:scale_num
179     [chf_r,chf_c]=size(imresize(channel_feature(:,:,1),scale_factor_t(i)));
180     temp=zeros(chf_r,chf_c,ch_num);%预先求矩阵的尺度
181     for ii=1:ch_num
182         %元胞的个数
183         temp(:,:,ii)=imresize(channel_feature(:,:,ii),scale_factor_t(i));%用元胞来装吧,数据大小不一样了,temp是global吗
184     end
185     channel_feature_scale{i}= temp;
186 end
187 
188 %获得训练图的尺度大小
189 patch_row=11;
190 patch_column=8;
191 %all_channel_difference_features=single(zeros(11,8,10,2));
192 data_num=1;
193 coordinate=zeros(4,2);
194 %all_boundingbox=zeros(5,2);
195 all_boundingbox=[];
196 for j=1:scale_num
197     I_scale=channel_feature_scale{j};%第j个尺度的图
198     [row,column,~]=size(I_scale);
199     
200     for l=1:ch_num                   %第l个通道的图
201         for m=1:2:row-patch_row+1      %某个patch
202             for n=1:2:column-patch_column+1
203                 all_channel_difference_features(:,:,:,data_num)=I_scale(m:m+patch_row-1,n:n+patch_column-1,:);%把滑窗的数据依次记录下来
204                 %坐标记录下来
205                 %coordinate[data_num}=[m,l,patch_row,patch_column];%存坐标
206                 coordinate(:,data_num)=[m*radio_r,n*radio_c,patch_row*radio_r,patch_column*radio_c];
207                 data_num=data_num+1;
208             end
209         end
210     end
211     data_num=data_num-1;
212     %在多个尺度图中缩放图中滑窗并检测,坐标和预测类标,置信度存好
213     handle = crndforest();
214     crndforest(handle, 'load', 'forest_model2');
215     [hs, ps] =crndforest(handle, 'predict', all_channel_difference_features);%尺度问题 
216     %对存好的坐标进行变换,映射到原图
217 %     for i=1:data_num
218 %         coordinate{i}=coordinate{i}/scale_factor_t(j);
219 %     end
220     coordinate=coordinate/scale_factor_t(j);
221     %所有的boundingbox组合作为输入
222     %prob=ps(hs+1,:);
223     [rr,~]=size(hs);
224     prob=zeros(rr,1);
225     for t=1:rr
226         prob(t)=ps(hs(t,1)+1,t);
227     end
228     hs=double(hs);
229     bb_partial=[coordinate',prob,hs];
230    %all_boundingbox(:,j)=[coordinate,hs,ps(hs)];
231    all_boundingbox=[all_boundingbox;bb_partial];
232    disp('')
233    j
234    disp('个尺度');
235    data_num=1;
236 end
237 %nms函数
238 % bbs1 = bbNms(all_boundingbox, 'type','max' );
239 % %显示
240 % im(I); 
241 % bbApply('draw',bbs1(1:4),'g');% all_boundingbox(:,[1,2])=all_boundingbox(:,[2,1]);
242 % all_boundingbox(:,[3,4])=all_boundingbox(:,[4,3]);
243 disp('正在显示,请稍等...');
244 [all_bb_r,all_bb_c]=size(all_boundingbox);
245 bbs=[];
246 for x=1:all_bb_r
247 %     if all_boundingbox(x,6)~=10
248     if all_boundingbox(x,6)==5
249         bbs=[bbs;all_boundingbox(x,:)];
250     end
251 end
252 
253 %bbs1 = bbNms(bbs, 'type','max' );
254 bbs1 = bbNms(bbs, 'thr',.35, 'type','ms');
255 
256 im(I); 
257 bbApply('draw',bbs1,'g');
258 disp('显示完成!恭喜恭喜!');
259 %}

注:

要附上其它代码:

1)随机森林c++版本(mex编译)

2)channel features

posted on 2015-12-11 17:13  一动不动的葱头  阅读(405)  评论(0编辑  收藏  举报

导航