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

Posted Reach high, for stars lie hidd

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用HOG+LBP实现动物分类:matlab版本相关的知识,希望对你有一定的参考价值。

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

2.代码部分

%% 利用HOG + LBP分类

%% 1 数据集,包括训练的和测试的  
currentPath = pwd;  % 获得当前的工作目录

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

imdsTest = imageDatastore(fullfile(pwd,‘test_image‘)); 

% imdsTrain = imageDatastore(‘C:\Program Files\MATLAB\R2017a\bin\proj_xiangbin\train_images‘,...  
%     ‘IncludeSubfolders‘,true,...  
%     ‘LabelSource‘,‘foldernames‘);  
% imdsTest = imageDatastore(‘C:\Program Files\MATLAB\R2017a\bin\proj_xiangbin\test_image‘);  
  
%%   2 对训练集中的每张图像进行hog特征提取,测试图像一样  
% 预处理图像,主要是得到features特征大小,此大小与图像大小和Hog特征参数相关  

%% LBP参数
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,[]);
% 对所有训练图像进行特征提取  
numImages = length(imdsTrain.Files);  
featuresTrain1 = zeros(numImages,size(lbpFeatures,2),‘single‘); % featuresTrain为单精度 

  
scaleImage = imresize(image1,imageSize);  
[features, visualization] = extractHOGFeatures(scaleImage,‘CellSize‘,[8,8]);
featuresTrain2 = zeros(numImages,size(features,2),‘single‘); % featuresTrain为单精度 
 
for i = 1:numImages  
    imageTrain = readimage(imdsTrain,i);  
    imageTrain = imresize(imageTrain,imageSize);  
    % LBP
    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,[]);
    featuresTrain1(i,:) = lbpFeatures;  
    
    % HOG
    featuresTrain2(i,:) = extractHOGFeatures(imageTrain,‘CellSize‘,[8,8]);    
    
end  

% 特征合并
featuresTrain = [featuresTrain1,featuresTrain2];

% 所有训练图像标签  
trainLabels = imdsTrain.Labels;  
  
% 开始svm多分类训练,注意:fitcsvm用于二分类,fitcecoc用于多分类,1 VS 1方法  
classifer = fitcecoc(featuresTrain,trainLabels);  
  
correctCount = 0;
%% 预测并显示预测效果图  
numTest = length(imdsTest.Files);  
for i = 1:numTest  
    testImage = readimage(imdsTest,i);  %  imdsTest.readimage(1)
    scaleTestImage = imresize(testImage,imageSize);  
    % LBP
    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));
    featureTest1 = reshape(lbpCellHists,1,[]);
    
    % HOG
    featureTest2 = extractHOGFeatures(scaleTestImage,‘CellSize‘,[8,8]); 
    % 合并
    featureTest = [featureTest1,featureTest2];
    
    [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([‘predictImage: ‘,tt2,‘--‘,char(predictIndex)]);  
    fprintf(‘%s == %s\n‘,tt2,char(predictIndex));
end  

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

  

以上是关于使用HOG+LBP实现动物分类:matlab版本的主要内容,如果未能解决你的问题,请参考以下文章

图像特征提取三大法宝:HOG特征,LBP特征,Haar特征

图像特征提取三大法宝:HOG特征,LBP特征,Haar特征

Opencv学习之路—Opencv下基于HOG特征的KNN算法分类训练

经典图像特征SIFT HOG LBP Haar

在 MATLAB 中使用 SVM 实现 HOG 特征

SIFT Hog LBP 了解