1. 程式人生 > >使用HOG+LBP實現動物分類:matlab版本

使用HOG+LBP實現動物分類:matlab版本

path ict store ima blog 顯示 ges count 結束

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版本