项目调试2——有点结果
%这里只是主函数
1 %{ 2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 3 % 可调参数 4 5 test_path='C:\Users\cong\Desktop\研一实战\项目\图像中时间数字识别\OCR\one\3.jpg'; 6 neighbour_pixels_affect=3; 7 target_digit=2; 8 % forestTrain()参数设置 9 % .M - [1] number of trees to train 10 % .H - [max(hs)] number of classes 11 % .N1 - [5*N/M] number of data points for training each tree 12 % .F1 - [sqrt(F)] number features to sample for each node split 13 % .split - ['gini'] options include 'gini', 'entropy' and 'twoing' 14 % .minCount - [1] minimum number of data points to allow split 15 % .minChild - [1] minimum number of data points allowed at child nodes 16 % .maxDepth - [64] maximum depth of tree 17 % .dWts - [] weights used for sampling and weighing each data point 18 % .fWts - [] weights used for sampling features 19 % .discretize - [] optional function mapping structured to class labels 20 % format: [hsClass,hBest] = discretize(hsStructured,H); 21 varargin.M=1000; 22 %varargin.H=10; 23 24 % forestApply()的输入设置 25 % data - [NxF] N length F feature vectors 26 % forest - learned forest classification model 27 % maxDepth - [] maximum depth of tree 28 % minCount - [] minimum number of data points to allow split 29 % best - [0] if true use single best prediction per tree 30 31 % forestApply()输出结果及对比的阀值 32 % hs - [Nx1] predicted output labels 33 % ps - [NxH] predicted output label probabilities 34 ps_val_more_than0_3=0.2; 35 36 %滑窗检测,窗口尺度,步长 37 win_h=20; 38 win_w=20; 39 step=1; 40 disp('参数配置成功...'); 41 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 42 disp('正在读入图片及特征提取...'); 43 %读入图片及特征提取 44 data=[]; 45 label=[]; 46 temp_r1=0; 47 temp_c1=0; 48 49 for i_digit=0:9 50 % if(i_digit==target_digit) %%%%%%%%%%%%%%%%%%%%%% 51 % this_image_label=1; 52 % end 53 %数字转字符 54 str=num2str(i_digit); %%数据是不是不平衡 55 path_temp=strcat('C:\Users\cong\Desktop\研一实战\项目\图像中时间数字识别\trainingSample\num',str,'\'); 56 file=dir(path_temp); 57 for i=3:length(file) 58 path= strcat(path_temp,file(i).name); 59 60 %%%%%%%%%%%%%%%%%%%%%%%%%% 61 % 加载图片 62 %%%%%%%%%%%%%%%%%%%%%%%%%% 63 I=imread(path); 64 %I=imread('E:/WeChat.jpg'); 65 %%%%%%%%%%%%%%%%%%%%%%%%%% 66 % 提取channel features 67 %%%%%%%%%%%%%%%%%%%%%%%%%% 68 [all_channel_difference_features,temp_r1,temp_c1]=extract_features(I,neighbour_pixels_affect,1); 69 data=[data,all_channel_difference_features]; 70 label=[label;i_digit+1]; 71 if(rem(i,100)==0) 72 disp('...'); 73 end 74 end % for i=3:length(file) 75 disp('数字') 76 i_digit 77 disp('的特征提取完毕...'); 78 end % for i_digit=0:9 79 disp('读入图片及特征提取完毕...'); 80 %%%%%%%%%%%%%%%%%%%%%%%%%% 81 % 扔进分类器中,训练 82 %%%%%%%%%%%%%%%%%%%%%%%%%% 83 data=data'; 84 disp('正在训练,请稍等...'); 85 forest = forestTrain( data, label, varargin ); 86 disp('训练完毕...'); 87 %} 88 %%%%%%%%%%%%%%%%%%%%%%%%%% 89 % 检测,测试 90 test_label=[]; 91 test_label_p=[]; 92 win_h=40; 93 win_w=30; 94 windSize = [30,40]; 95 step=1; 96 ps_val_more_than0_3=0.07; 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 %画框 122 %%%%i_test 123 %j_test 124 125 rectangle('Position',[j_test,i_test,20,20],'LineWidth',4,'EdgeColor','r'); 126 %pointAll = [i_test,j_test]; 127 %[state,results]=draw_rect(test_image,pointAll,windSize); 128 hold on 129 end 130 131 end 132 end 133 disp('检测完毕!恭喜恭喜!') 134 %%%%%%%%%%%%%%%%%%%%%%%%%%