调试4

  1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2 % 可调参数
  3 %{
  4 test_path='C:\Users\cong\Desktop\研一实战\项目\图像中时间数字识别\OCR\one\3.jpg';
  5 neighbour_pixels_affect=3;
  6 target_digit=2;
  7 % forestTrain()参数设置
  8 %   .M          - [1] number of trees to train
  9 %   .H          - [max(hs)] number of classes
 10 %   .N1         - [5*N/M] number of data points for training each tree
 11 %   .F1         - [sqrt(F)] number features to sample for each node split
 12 %   .split      - ['gini'] options include 'gini', 'entropy' and 'twoing'
 13 %   .minCount   - [1] minimum number of data points to allow split
 14 %   .minChild   - [1] minimum number of data points allowed at child nodes
 15 %   .maxDepth   - [64] maximum depth of tree
 16 %   .dWts       - [] weights used for sampling and weighing each data point
 17 %   .fWts       - [] weights used for sampling features
 18 %   .discretize - [] optional function mapping structured to class labels
 19 %                    format: [hsClass,hBest] = discretize(hsStructured,H);
 20 varargin.M=1000;
 21 %varargin.H=10;
 22 
 23 % forestApply()的输入设置
 24 %  data     - [NxF] N length F feature vectors
 25 %  forest   - learned forest classification model
 26 %  maxDepth - [] maximum depth of tree
 27 %  minCount - [] minimum number of data points to allow split
 28 %  best     - [0] if true use single best prediction per tree
 29 
 30 %  forestApply()输出结果及对比的阀值
 31 %  hs       - [Nx1] predicted output labels
 32 %  ps       - [NxH] predicted output label probabilities
 33 ps_val_more_than0_3=0.2;
 34 
 35 %滑窗检测,窗口尺度,步长
 36 win_h=20;
 37 win_w=20;
 38 step=1;
 39 disp('参数配置成功...');
 40 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 41 disp('正在读入图片及特征提取...');
 42 %读入图片及特征提取
 43 data=[];
 44 label=[];
 45 temp_r1=0;
 46 temp_c1=0;
 47 
 48 for i_digit=0:10
 49 %     if(i_digit==target_digit)                                %%%%%%%%%%%%%%%%%%%%%%
 50 %         this_image_label=1;
 51 %     end
 52     %数字转字符
 53     str=num2str(i_digit);                                          %%数据是不是不平衡
 54     path_temp=strcat('C:\Users\cong\Desktop\研一实战\项目\图像中时间数字识别\trainingSample\num',str,'\');
 55     file=dir(path_temp);
 56     for i=3:length(file)
 57         path= strcat(path_temp,file(i).name);
 58 
 59         %%%%%%%%%%%%%%%%%%%%%%%%%%
 60         % 加载图片
 61         %%%%%%%%%%%%%%%%%%%%%%%%%%
 62         I=imread(path);
 63         %I=imread('E:/WeChat.jpg');
 64         %%%%%%%%%%%%%%%%%%%%%%%%%%
 65         % 提取channel features
 66         %%%%%%%%%%%%%%%%%%%%%%%%%%
 67         [all_channel_difference_features,temp_r1,temp_c1]=extract_features(I,neighbour_pixels_affect,1);
 68         data=[data,all_channel_difference_features];
 69         label=[label;i_digit+1];
 70         if(rem(i,200)==0)
 71             disp('...');
 72         end
 73     end % for i=3:length(file)
 74     disp('数字')
 75     i_digit
 76     disp('的特征提取完毕...');
 77 end  % for i_digit=0:9
 78 disp('读入图片及特征提取完毕...');
 79 %%%%%%%%%%%%%%%%%%%%%%%%%%
 80 % 扔进分类器中,训练
 81 %%%%%%%%%%%%%%%%%%%%%%%%%%
 82 data=data';
 83 disp('正在训练,请稍等...');
 84 forest = forestTrain( data, label, varargin );
 85 disp('训练完毕...');
 86 %}
 87 %%%%%%%%%%%%%%%%%%%%%%%%%%
 88 % 检测,测试
 89 test_label=[];
 90 test_label_p=[];
 91 win_h=45;
 92 win_w=30;
 93 windSize = [30,40]; 
 94 step=1;
 95 ps_val_more_than0_3=0.07;
 96 count=0;
 97 
 98 disp('正在检测...');
 99 test_image=imread(test_path);
100 %滑窗检测,窗口尺度,步长
101 [test_r,test_c,test_z]=size(test_image);
102 figure;
103 imshow(test_image);
104 hold on
105 
106 for j_test=1:step:(test_c-win_w+1)
107     for i_test=1:step:(test_r-win_h+1)
108         %model
109         
110         model=test_image(i_test:i_test+win_h-1,j_test:j_test+win_w-1,:);
111         %resize
112         test_image_rs=imresize(model,[temp_r1 temp_c1]);
113         test_data=extract_features(test_image_rs,neighbour_pixels_affect,0);
114         test_data=test_data';
115         test_data=single(test_data);
116         
117         [hs,ps] = forestApply( test_data, forest,0,0,1);%尺度问题
118          test_label=[test_label,hs]; 
119          test_label_p=[test_label_p,ps(hs)];
120         %if(ps>ps_val_more_than0_3)
121         if (hs~=11) && (ps(hs)>0.194)
122           %画框
123           %%%%i_test
124           %j_test
125           count=count+1;
126           %hs
127           switch hs
128               case 1
129                   rectangle('Position',[j_test,i_test,30,40],'LineWidth',2,'EdgeColor','r');
130               case 2
131                   rectangle('Position',[j_test,i_test,30,40],'LineWidth',2,'EdgeColor','g');
132               case 3
133                   rectangle('Position',[j_test,i_test,30,40],'LineWidth',2,'EdgeColor','b');
134               case 4
135                   rectangle('Position',[j_test,i_test,30,40],'LineWidth',2,'EdgeColor','c');
136               case 5
137                   rectangle('Position',[j_test,i_test,30,40],'LineWidth',2,'EdgeColor','m');
138               case 6
139                   rectangle('Position',[j_test,i_test,30,40],'LineWidth',2,'EdgeColor','y');
140               case 7
141                   rectangle('Position',[j_test,i_test,30,40],'LineWidth',2,'EdgeColor','b');
142               case 8
143                   rectangle('Position',[j_test,i_test,30,40],'LineWidth',2,'EdgeColor','m');
144               case 9
145                   rectangle('Position',[j_test,i_test,30,40],'LineWidth',2,'EdgeColor','r');
146               case 10
147                   rectangle('Position',[j_test,i_test,30,40],'LineWidth',2,'EdgeColor','g');
148           end
149         
150           %rectangle('Position',[j_test,i_test,30,40],'LineWidth',2,'EdgeColor','r');
151           %pointAll = [i_test,j_test];  
152           %[state,results]=draw_rect(test_image,pointAll,windSize); 
153           hold on
154         end 
155         
156     end
157 end
158 disp(' the value of count is:')
159 count
160 disp('检测完毕!恭喜恭喜!')
161 %%%%%%%%%%%%%%%%%%%%%%%%%%

 

posted on 2015-11-05 19:02  一动不动的葱头  阅读(227)  评论(0编辑  收藏  举报

导航