调试5——还是50%的效果吧,没啥希望
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=20; 92 win_w=15; 93 windSize = [30,40]; 94 step=2; 95 ps_val_more_than0_3=0.07; 96 count=0; 97 temp_hs=0; 98 99 disp('正在检测...'); 100 test_image=imread(test_path); 101 %滑窗检测,窗口尺度,步长 102 [test_r,test_c,test_z]=size(test_image); 103 figure; 104 imshow(test_image); 105 hold on 106 107 for j_test=1:step:(test_c-win_w+1) 108 for i_test=1:step:(test_r-win_h+1) 109 %model 110 111 model=test_image(i_test:i_test+win_h-1,j_test:j_test+win_w-1,:); 112 %resize 113 test_image_rs=imresize(model,[temp_r1 temp_c1]); 114 test_data=extract_features(test_image_rs,neighbour_pixels_affect,0); 115 test_data=test_data'; 116 test_data=single(test_data); 117 118 [hs,ps] = forestApply( test_data, forest,0,0,1);%尺度问题 119 test_label=[test_label,hs]; 120 test_label_p=[test_label_p,ps(hs)]; 121 %if(ps>ps_val_more_than0_3) 122 if (hs~=11) && (ps(hs)>0.27) 123 %画框 124 %%%%i_test 125 %j_test 126 count=count+1; 127 %hs 128 129 if hs~=temp_hs 130 switch hs 131 case 1 132 rectangle('Position',[j_test,i_test,3,3],'LineWidth',2,'EdgeColor','r'); 133 case 2 134 rectangle('Position',[j_test,i_test,3,3],'LineWidth',2,'EdgeColor','g'); 135 case 3 136 rectangle('Position',[j_test,i_test,3,3],'LineWidth',2,'EdgeColor','b'); 137 case 4 138 rectangle('Position',[j_test,i_test,3,3],'LineWidth',2,'EdgeColor','c'); 139 case 5 140 rectangle('Position',[j_test,i_test,3,3],'LineWidth',2,'EdgeColor','m'); 141 case 6 142 rectangle('Position',[j_test,i_test,3,3],'LineWidth',2,'EdgeColor','y'); 143 case 7 144 rectangle('Position',[j_test,i_test,3,3],'LineWidth',2,'EdgeColor','b'); 145 case 8 146 rectangle('Position',[j_test,i_test,3,3],'LineWidth',2,'EdgeColor','m'); 147 case 9 148 rectangle('Position',[j_test,i_test,3,3],'LineWidth',2,'EdgeColor','r'); 149 case 10 150 rectangle('Position',[j_test,i_test,3,3],'LineWidth',2,'EdgeColor','g'); 151 end 152 153 %rectangle('Position',[j_test,i_test,30,40],'LineWidth',2,'EdgeColor','r'); 154 %pointAll = [i_test,j_test]; 155 %[state,results]=draw_rect(test_image,pointAll,windSize); 156 hold on 157 end 158 temp_hs=hs; 159 end 160 161 end 162 end 163 disp(' the value of count is:') 164 count 165 disp('检测完毕!恭喜恭喜!') 166 %%%%%%%%%%%%%%%%%%%%%%%%%%