使用LBP实现动物分类:matlab版本

1.训练集测试集划分(同上一篇)

2.代码部分

1)训练部分代码:training.m

%% 该函数是使用LBP来提取test_images下图片的特征的,代码编写参考matlab官方文档 

%% 1.获取图片以及相关的分类

currentPath = pwd;  % 获得当前的工作目录

imdsTrain = imageDatastore(fullfile(pwd,'train_images'),...  
    'IncludeSubfolders',true,...  
    'LabelSource','foldernames');   % 载入图片集合

%%   2 对训练集中的每张图像进行hog特征提取,测试图像一样  
% 预处理图像,主要是得到features特征大小,此大小与图像大小和Hog特征参数相关  
imageSize = [256,256];% 对所有图像进行此尺寸的缩放  
I = readimage(imdsTrain,1);
I = imresize(I,imageSize);  
I = rgb2gray(I);
lbpFeatures = extractLBPFeatures(I,'CellSize',[16 16],'Normalization','None');
numNeighbors = 8;
% Upright = false;
numBins = numNeighbors*(numNeighbors-1)+3; % numNeighbors+2;
lbpCellHists = reshape(lbpFeatures,numBins,[]);
lbpCellHists = bsxfun(@rdivide,lbpCellHists,sum(lbpCellHists));
lbpFeatures = reshape(lbpCellHists,1,[]);

% 提示信息
disp('开始训练数据...');
% 对所有训练图像进行特征提取  
numImages = length(imdsTrain.Files);  
featuresTrain = zeros(numImages,size(lbpFeatures,2),'single'); % featuresTrain为单精度  
for i = 1:numImages  
    imageTrain = readimage(imdsTrain,i);  
    imageTrain = imresize(imageTrain,imageSize);  
    
    I = rgb2gray(imageTrain);
    lbpFeatures = extractLBPFeatures(I,'CellSize',[16 16],'Normalization','None');
%     numNeighbors = 8;
%     numBins = numNeighbors*(numNeighbors-1)+3;
    lbpCellHists = reshape(lbpFeatures,numBins,[]);
    lbpCellHists = bsxfun(@rdivide,lbpCellHists,sum(lbpCellHists));
    lbpFeatures = reshape(lbpCellHists,1,[]);
    
    featuresTrain(i,:) = lbpFeatures;  
end  
  
% 所有训练图像标签  
trainLabels = imdsTrain.Labels;  
  
% 开始svm多分类训练,注意:fitcsvm用于二分类,fitcecoc用于多分类,1 VS 1方法  
classifer = fitcecoc(featuresTrain,trainLabels);  

save classifer
% 提示信息
disp('训练阶段结束!!!');

  

2)测试部分代码:classify.m

%% 该函数用来对图片进项分类 LBP + SVM

%% 1.读入待分类的图片集合
currentPath = pwd;
imdsTest = imageDatastore(fullfile(pwd,'test_image')); 

%% 2.分类,预测并显示预测效果图  
% 载入分类器
load classifer

correctCount = 0;
%% 预测并显示预测效果图  
numTest = length(imdsTest.Files);  
for i = 1:numTest  
    testImage = readimage(imdsTest,i);  %  imdsTest.readimage(1)
    scaleTestImage = imresize(testImage,imageSize);  
    
    I = rgb2gray(scaleTestImage);
    lbpFeatures = extractLBPFeatures(I,'CellSize',[16 16],'Normalization','None');
    numNeighbors = 8;
    numBins = numNeighbors*(numNeighbors-1)+3;
    lbpCellHists = reshape(lbpFeatures,numBins,[]);
    lbpCellHists = bsxfun(@rdivide,lbpCellHists,sum(lbpCellHists));
    featureTest = reshape(lbpCellHists,1,[]);
     
    [predictIndex,score] = predict(classifer,featureTest);  
    figure;imshow(imresize(testImage,[256,256]));
    
    imgName = imdsTest.Files(i);
    tt = regexp(imgName,'\','split');
    cellLength =  cellfun('length',tt);
    tt2 = char(tt{1}(1,cellLength));
     % 统计正确率
    if strfind(tt2,char(predictIndex))==1
        correctCount = correctCount+1;
    end
    title(['分类结果: ',tt2,'--',char(predictIndex)]);  
    fprintf('%s == %s \n',tt2,char(predictIndex));
end  

% 显示正确率
fprintf('分类结束,正确了为:%.1f%%\n',correctCount * 100.0 / numTest);

  

posted @ 2017-06-03 12:38  GoodboyBin  阅读(4385)  评论(6编辑  收藏  举报