博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
项目代码matlab
阅读量:5049 次
发布时间:2019-06-12

本文共 10222 字,大约阅读时间需要 34 分钟。

1 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 2 % 可调参数 3  4 test_path=''; 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 classes10 %   .N1         - [5*N/M] number of data points for training each tree11 %   .F1         - [sqrt(F)] number features to sample for each node split12 %   .split      - ['gini'] options include 'gini', 'entropy' and 'twoing'13 %   .minCount   - [1] minimum number of data points to allow split14 %   .minChild   - [1] minimum number of data points allowed at child nodes15 %   .maxDepth   - [64] maximum depth of tree16 %   .dWts       - [] weights used for sampling and weighing each data point17 %   .fWts       - [] weights used for sampling features18 %   .discretize - [] optional function mapping structured to class labels19 %                    format: [hsClass,hBest] = discretize(hsStructured,H);20 varargin.M=1000;21 %varargin.H=10;22 23 % forestApply()的输入设置24 %  data     - [NxF] N length F feature vectors25 %  forest   - learned forest classification model26 %  maxDepth - [] maximum depth of tree27 %  minCount - [] minimum number of data points to allow split28 %  best     - [0] if true use single best prediction per tree29 30 %  forestApply()输出结果及对比的阀值31 %  hs       - [Nx1] predicted output labels32 %  ps       - [NxH] predicted output label probabilities33 ps_val_more_than0_3=0.2;34 35 %滑窗检测,窗口尺度,步长36 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%37 38 data=[];39 label=[];40 temp_r1=0;41 temp_c1=0;42 43 for i_digit=0:944 %     if(i_digit==target_digit)                                %%%%%%%%%%%%%%%%%%%%%%45 %         this_image_label=1;46 %     end47     %数字转字符48     str=num2str(i);                                          %%数据是不是不平衡49     path_temp=strcat('C:\Users\cong\Desktop\研一实战\项目\图像中时间数字识别\trainingSample\num',str,'\');50     file=dir(path_temp);51     for i=3:length(file)52         path= strcat(path_temp,file(i).name);53 54         %%%%%%%%%%%%%%%%%%%%%%%%%%55         % 加载图片56         %%%%%%%%%%%%%%%%%%%%%%%%%%57         I=imread(path);58         %I=imread('E:/WeChat.jpg');59         %%%%%%%%%%%%%%%%%%%%%%%%%%60         % 提取channel features61         %%%%%%%%%%%%%%%%%%%%%%%%%%62         [all_channel_difference_features,temp_r1,temp_c1]=extract_features(I,1);63         data=[data,all_channel_difference_features];64         label=[label;i_digit+1];65         66 %         if(i>100 && this_image_label~=1)               %%这里只取了前100帧,实际上可以随意抽取一百张67 %             break;68 %         end69     end % for i=3:length(file)70     71 end  % for i_digit=0:972 73 %%%%%%%%%%%%%%%%%%%%%%%%%%74 % 扔进分类器中,训练75 %%%%%%%%%%%%%%%%%%%%%%%%%%76 77 forest = forestTrain( data, label, varargin );78 79 %%%%%%%%%%%%%%%%%%%%%%%%%%80 % 检测,测试81 test_image=imread(test_path);82 %滑窗检测,窗口尺度,步长83 [test_r,test_c,test_z]=size(test_image);84 for i_test=1:test_r85     %model86     87     %resize88     test_image=imresize(model,temp_r1,temp_c1);89     test_data=extract_features(test_image,1);90     [hs,ps] = forestApply( test_data, forest, [], [], [] );%尺度问题91     if(ps>ps_val_more_than0_3)92       %画框93       94     end    95 end96 97 %%%%%%%%%%%%%%%%%%%%%%%%%%
1 function [ all_channel_difference_features,,r1,c1 ] = extract_features( I,shrink_or_not ) 2 %EXTRACT_FEATURES 此处显示有关此函数的摘要 3 %   此处显示详细说明 4     %%%%%%%%%%%%%%%%%%%%%%%%%% 5     % 提取channel features 6     %%%%%%%%%%%%%%%%%%%%%%%%%% 7     % 参数设置 8     if(shrink_or_not==1) 9         pChns.shrink=4;10     end11 12     pChns.pColor.enabled=1;13     pChns.pColor.smooth=1;14     pChns.pColor.colorSpace='luv';15 16     pChns.pGradMag.enabled=1;17     pChns.pGradMag.colorChn=0;18     pChns.pGradMag.normRad=5;19     pChns.pGradMag.normConst=.005;20     pChns.pGradMag.full=0;21 22     pChns.pGradHist.enabled=1;23     %pChns.pGradHist.binSize=24     pChns.pGradHist.nOrients=6;25     pChns.pGradHist.softBin=0;26     pChns.pGradHist.useHog=0;27     pChns.pGradHist.clipHog=.2;28 29     %pChns.pCustom.**30 31     %pChns.complete=32 33     % 提取channel features34     chns = chnsCompute( I, pChns );35     % 将各个通道放在矩阵中36     [r1,c1,ch1]=size(chns.data{
1});37 [r2,c2,ch2]=size(chns.data{
2});38 [r3,c3,ch3]=size(chns.data{
3});39 ch=ch1+ch2+ch3;40 all_channel=zeros(r1,c1,ch);41 all_channel(:,:,1:ch1)=chns.data{
1};42 all_channel(:,:,ch1+1:ch1+ch2)=chns.data{
2};43 all_channel(:,:,ch1+ch2+1:ch)=chns.data{
3};44 %%%%%%%%%%%%%%%%%%%%%%%%%%45 % pooling46 %%%%%%%%%%%%%%%%%%%%%%%%%%47 for ii=1:ch48 %向下采样49 all_pooling(:,:,ii)=imresize(all_channel(:,:,ii),0.2);50 end51 52 %%%%%%%%%%%%%%%%%%%%%%%%%%53 % 再次做相减特征54 %%%%%%%%%%%%%%%%%%%%%%%%%%55 all_channel_difference_features=[];56 for ij=1:ch57 temp=difference_features( all_pooling(:,:,ij),neighbour_pixels_affect );58 all_channel_difference_features = [all_channel_difference_features;temp];59 end60 61 end
1 function [ one_channel_difference_features ] = difference_features( one_channel_features,neighbour_pixels_affect ) 2 %DIFFERENCE_FEATURES 计算邻域内个特征之间两两相减 3 %input: 4 % one_channel_features  5 %neighbour_pixels_affect 6 %output: 7 %one_channel_difference_features 8  9 [r,c]=size(one_channel_features);10 11 one_channel_difference_features=[];12 for i=1:r-neighbour_pixels_affect+113     for j=1:c-neighbour_pixels_affect+114         local_features=one_channel_features(i:i+neighbour_pixels_affect-1,j:j+neighbour_pixels_affect-1);15         temp=local_feature_compute(local_features);16         one_channel_difference_features=[one_channel_difference_features;temp];%特征拼接17     end18 end19 end20 21 function [ local_differece_feature ]=local_feature_compute( local_features )22 [r,c]=size(local_features);23 result_mat=local_features-local_features(1,1).*ones(r,c);24 result_vector=reshape(result_mat,r*c,1);25 local_differece_feature=result_vector(2:r*c,1);%把第一个特征去掉,自己减自己没有任何特征信息可言26 end

 

%{%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% 可调参数test_path='C:\Users\cong\Desktop\研一实战\项目\图像中时间数字识别\OCR\one\3.jpg';neighbour_pixels_affect=3;target_digit=2;% forestTrain()参数设置%   .M          - [1] number of trees to train%   .H          - [max(hs)] number of classes%   .N1         - [5*N/M] number of data points for training each tree%   .F1         - [sqrt(F)] number features to sample for each node split%   .split      - ['gini'] options include 'gini', 'entropy' and 'twoing'%   .minCount   - [1] minimum number of data points to allow split%   .minChild   - [1] minimum number of data points allowed at child nodes%   .maxDepth   - [64] maximum depth of tree%   .dWts       - [] weights used for sampling and weighing each data point%   .fWts       - [] weights used for sampling features%   .discretize - [] optional function mapping structured to class labels%                    format: [hsClass,hBest] = discretize(hsStructured,H);varargin.M=1000;%varargin.H=10;% forestApply()的输入设置%  data     - [NxF] N length F feature vectors%  forest   - learned forest classification model%  maxDepth - [] maximum depth of tree%  minCount - [] minimum number of data points to allow split%  best     - [0] if true use single best prediction per tree%  forestApply()输出结果及对比的阀值%  hs       - [Nx1] predicted output labels%  ps       - [NxH] predicted output label probabilitiesps_val_more_than0_3=0.2;%滑窗检测,窗口尺度,步长win_h=20;win_w=20;step=1;disp('参数配置成功...');%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%disp('正在读入图片及特征提取...');%读入图片及特征提取data=[];label=[];temp_r1=0;temp_c1=0;for i_digit=0:9%     if(i_digit==target_digit)                                %%%%%%%%%%%%%%%%%%%%%%%         this_image_label=1;%     end    %数字转字符    str=num2str(i_digit);                                          %%数据是不是不平衡    path_temp=strcat('C:\Users\cong\Desktop\研一实战\项目\图像中时间数字识别\trainingSample\num',str,'\');    file=dir(path_temp);    for i=3:length(file)        path= strcat(path_temp,file(i).name);        %%%%%%%%%%%%%%%%%%%%%%%%%%        % 加载图片        %%%%%%%%%%%%%%%%%%%%%%%%%%        I=imread(path);        %I=imread('E:/WeChat.jpg');        %%%%%%%%%%%%%%%%%%%%%%%%%%        % 提取channel features        %%%%%%%%%%%%%%%%%%%%%%%%%%        [all_channel_difference_features,temp_r1,temp_c1]=extract_features(I,neighbour_pixels_affect,1);        data=[data,all_channel_difference_features];        label=[label;i_digit+1];        if(rem(i,100)==0)            disp('...');        end    end % for i=3:length(file)    disp('数字')    i_digit    disp('的特征提取完毕...');end  % for i_digit=0:9disp('读入图片及特征提取完毕...');%%%%%%%%%%%%%%%%%%%%%%%%%%% 扔进分类器中,训练%%%%%%%%%%%%%%%%%%%%%%%%%%data=data';disp('正在训练,请稍等...');forest = forestTrain( data, label, varargin );disp('训练完毕...');%}%%%%%%%%%%%%%%%%%%%%%%%%%%% 检测,测试test_label=[];test_label_p=[];win_h=40;win_w=30;windSize = [30,40]; step=2;ps_val_more_than0_3=0.07;disp('正在检测...');test_image=imread(test_path);%滑窗检测,窗口尺度,步长[test_r,test_c,test_z]=size(test_image);figure;imshow(test_image);hold onfor i_test=1:step:test_r-win_h+1    for j_test=1:step:test_c-win_w+1        %model        model=test_image(i_test:i_test+win_h-1,j_test:j_test+win_w-1,:);        %resize        test_image_rs=imresize(model,[temp_r1 temp_c1]);        test_data=extract_features(test_image_rs,neighbour_pixels_affect,0);        test_data=test_data';        test_data=single(test_data);                [hs,ps] = forestApply( test_data, forest,0,0,1);%尺度问题         test_label=[test_label,hs];          test_label_p=[test_label_p,ps(hs)];        if(ps>ps_val_more_than0_3)          %画框          %draw_rect(test_image,);          i_test          j_test          rectangle('Position',[i_test,j_test,20,20],'LineWidth',4,'EdgeColor','r');          %pointAll = [i_test,j_test];            %[state,results]=draw_rect(test_image,pointAll,windSize);           hold on        end             end    hold onenddisp('检测完毕!恭喜恭喜!')%%%%%%%%%%%%%%%%%%%%%%%%%%

 

转载于:https://www.cnblogs.com/Wanggcong/p/4933081.html

你可能感兴趣的文章
MFC六大核心机制
查看>>
iPhone 屏幕分辨率
查看>>
thinkPHP 模板中变量的使用
查看>>
java 的访问权限控制
查看>>
BZOJ 3676: [Apio2014]回文串
查看>>
在C#4.0中使用NPLOT
查看>>
用FPGA驱动ov7670摄像头用tft9328显示
查看>>
图像增强
查看>>
Android 迷之Version管理
查看>>
类变量与实例变量、析构函数、私有属性与私有方法
查看>>
linux_宿主目录
查看>>
[译] 如何调试CSS的跨浏览器样式bug
查看>>
纯虚函数
查看>>
python基础
查看>>
ServletContext对象
查看>>
HTML表格及网页编辑
查看>>
mysql事务
查看>>
[最大环+缩点+BFS]codeforces Round 95 Div2
查看>>
asp.net 获取服务器及客户端的相关信息
查看>>
Python基础01
查看>>