⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scriptfeaextract.m

📁 脱机手写体识别Matlab源程序 包括特征提取、bayes分类器、K近邻分类及最近邻分类。 TestScriptRecognition.m:测试代码 ScriptFeaExtract.m :
💻 M
字号:
% ScriptFeaExtract
% 提取签字图像的特征向量(列向量)
% 需要指定划分的strips数量,默认为6
function [FeatureVector]=ScriptFeaExtract(Image,varargin)
if(nargin==1)
    Np=6;
else
    Np=varargin{1};
end
[peripheralFeature]=extractPeripheralFeature(Image,Np);
FeatureVector=peripheralFeature.';


% 提取peripheral特征,包括ET1特征及DT12特征
% reference:Offline signature verification with generated training samples
% Np is the number of strips
function [peripheralFeature]=extractPeripheralFeature(Image,Np)
[imageHeight imageWidth]=size(Image);

% 首先遍历的序列
firstBar{1}=1:imageHeight;     % left
firstBar{2}=1:imageWidth;      % bottom
firstBar{3}=1:imageHeight;     % right
firstBar{4}=1:imageWidth;      % top

% 其次遍历的序列
secondBar{1}=1:imageWidth;     % left
secondBar{2}=imageHeight:-1:1; % bottom
secondBar{3}=imageWidth:-1:1;  % right
secondBar{4}=1:imageHeight;    % top

Thresh=max(max(Image))/2;      % 将图像二值话的阈值,取最大值的一半
orentationCnt=4;               % 需要搜索方向的数量
ETCnt=cell(1,orentationCnt);  
DTCnt=cell(1,orentationCnt);

% flag 标志状态说明:
%   1    初始状态,假设初始点像素为白色
%   2    黑色像素
%   3    白色像素
%   当第一次由白色像素进入黑色像素,状态由1变为2 
%   当第一次由黑色像素进入白色像素,状态由2变为3
%   当第二次由白色像素进入黑色像素,中止搜索,跳出
for curO=1:orentationCnt                 % 搜索四个方向的特征
    ETCnt{curO}=zeros(1,length(firstBar{curO}));
    DTCnt{curO}=zeros(1,length(firstBar{curO}));
    for i=firstBar{curO}                 
        flag=1;
        curDTCnt=0;
        curETCnt=0;
        for j=secondBar{curO}
            if(curO==1||curO==3)
                pixel=(Image(i,j)>=Thresh);
            else
                pixel=(Image(j,i)>=Thresh);
            end
            if((flag==1)&&(pixel==1))    % 满足ET特征的像素
                curETCnt=curETCnt+1;
            elseif(flag==1&&pixel==0)    % 由白色像素进入黑色像素
                flag=2;
            elseif(flag==2&&pixel==1)    % 由黑色像素进入白色像素 
                curDTCnt=1;
                flag=3;
            elseif(flag==3&&pixel==1)    % 满足DT特征的像素
                curDTCnt=curDTCnt+1;
            elseif(flag==3&&pixel==0)    % 跳出
                break;
            end
        end 
        ETCnt{curO}(i)=curETCnt;
        DTCnt{curO}(i)=curDTCnt;
    end
end

% 这里划分 strips 并不严谨
% horizontalStrips = 0    22    44    66    88   110   129 (间隔并不均匀)
% verticalStrips =   0    16    32    48    64    80    94
% 当总数与数量没有整除时,还会存在其他问题,此处有待改进@
% TODO!!!!!!
horizontalDiv=round(imageWidth/Np);              % 水平方向划分间隔
verticalDiv=round(imageHeight/Np);               % 垂直方向划分间隔 
horizontalStrips=0:horizontalDiv:imageWidth;      
horizontalStrips=[horizontalStrips imageWidth];  % 水平方向 strips
verticalStrips=0:verticalDiv:imageHeight;
verticalStrips=[verticalStrips imageHeight];     % 垂直方向 strips

% left right 按 verticalStrips    累加
% bottom top 按 horizontalStrips  累加
feaCnt=1;
% 将数据按 strips 划分,计算水平方向特征
for curK=2:length(verticalStrips)
    curIndex=verticalStrips(curK-1)+1:verticalStrips(curK);
    ETFea1(feaCnt)=sum(ETCnt{1}(curIndex));
    ETFea3(feaCnt)=sum(ETCnt{3}(curIndex));
    DTFea1(feaCnt)=sum(DTCnt{1}(curIndex));
    DTFea3(feaCnt)=sum(DTCnt{3}(curIndex));
    feaCnt=feaCnt+1;
end

feaCnt=1;
% 将数据按 strips 划分,计算垂直方向特征
for curK=2:length(horizontalStrips)
    curIndex=horizontalStrips(curK-1)+1:horizontalStrips(curK);
    ETFea2(feaCnt)=sum(ETCnt{2}(curIndex));
    ETFea4(feaCnt)=sum(ETCnt{4}(curIndex));
    DTFea2(feaCnt)=sum(DTCnt{2}(curIndex));
    DTFea4(feaCnt)=sum(DTCnt{4}(curIndex));
    feaCnt=feaCnt+1;
end

peripheralFeature=[ETFea1 ETFea2 ETFea3 ETFea4 DTFea1 DTFea2 DTFea3 DTFea4];



% testdata
% a =[ 1     1     1     1     1     1     1     0     0     1     1
%      1     0     0     0     1     1     1     0     0     1     0
%      0     1     1     1     1     1     1     1     1     1     0
%      0     0     0     0     0     0     0     0     0     0     0
%      1     1     0     0     1     1     1     0     0     1     1
%      1     1     1     1     1     1     1     1     1     1     1
%      0     0     0     0     0     0     0     0     0     0     0];

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -