www.gusucode.com > IPCV_Eval_Kit_R2019a_0ce6858工具箱matlab程序源码 > IPCV_Eval_Kit_R2019a_0ce6858/code/demo_files/I5_04_1a_myHOGDigitClassification_MSVM.m

    %% HOG (Histogram of Oriented Gradient) 摿挜検 偲
%  儅儖僠僋儔僗 SVM 傪巊偭偨丄庤彂偒悢帤偺幆暿
clear;clc;close all;imtool close all

% 僩儗乕僯儞僌夋憸乮101枃x10暥帤庬乯偲僥僗僩夋憸乮12枃x10暥帤庬乯傊偺愨懳僷僗傪愝掕
pathData = [toolboxdir('vision'), '\visiondata\digits']
trainSet = imageSet([pathData,'\synthetic'  ], 'recursive');
testSet  = imageSet([pathData,'\handwritten'], 'recursive');

%% 慡僩儗乕僯儞僌梡夋憸椺偺昞帵
figure;montage([trainSet.ImageLocation], 'Size', [26 40]);

%% 慡僥僗僩夋憸傪儌儞僞乕僕儏昞帵 (12枃 x 10暥帤庬丗奺庤彂偒悢帤傪擣幆)
figure;montage([testSet(:).ImageLocation], 'Size', [10,12]);

%% 4x4偺僙儖僒僀僘傪巊梡 (324師尦儀僋僩儖)
cellSize = [4 4];
hogFeatureSize = 324;                   % length(hog_4x4)

%% [暘椶婍偺峔抸]丗fitcecoc傪巊梡
% 10暥帤暘偺trainingFeatures 傪奿擺偡傞攝楍傪偁傜偐偠傔嶌惢
trainingFeatures  = zeros(10*101,hogFeatureSize, 'single');
trainingLabels    = zeros(10*101,1);

% HOG摿挜検傪拪弌
for digit = 0:9   % 暥帤'0'乣'9'
  for i = 1:101         % 奺悢帤偛偲偵101枃偺僩儗乕僯儞僌梡夋憸
    img = read(trainSet(digit+1), i);  %僩儗乕僯儞僌夋憸偺撉崬傒       trainSet()偼丄1偐傜巒傑傞偺偱丄+1
    img = im2bw(img,graythresh(img));   % 擇抣壔
             
    trainingFeatures((digit)*101+i,:) = extractHOGFeatures(img,'CellSize',cellSize);
    trainingLabels((digit)*101+i)     = digit;
  end
end
% 懡僋儔僗暘椶婍偺妛廗乮ECOC岆傝掶惓晞崋 懡僋儔僗儌僨儖乯
svmModel = fitcecoc(trainingFeatures, trainingLabels)

%% [幆暿] 嶌惉偟偨暘椶婍偱庤彂偒悢帤(120枃)傪幆暿\帵丗predict()
Ir = zeros([16,16,3,120], 'uint8');      % 寢壥傪奿擺偡傞攝楍
cntTrue = 0;
for digit = 0:9   % 
  for i = 1:12         % 奺悢帤偛偲偵12枃偺庤彂偒暥帤
    img = read(testSet(digit+1), i);    % testSet()偼丄1偐傜巒傑傞偺偱丄+1
    BW = im2bw(img,graythresh(img));    % 2抣壔

    testFeatures = extractHOGFeatures(BW,'CellSize',cellSize);
    predictedNum = predict(svmModel, testFeatures);           % testFeature 傪攝楍偵偟偰丄偁偲偱傑偲傔偰敾掕傕壜
    
    if predictedNum == digit    %惓偟偄幆暿偼惵怓丄岆擣幆偼愒怓
      Ir(:,:,:,digit*12+i) = insertText(img,[6 4],num2str(predictedNum),'FontSize',9,'TextColor','blue','BoxOpacity',0.4);
      cntTrue = cntTrue+1;
    else
      Ir(:,:,:,digit*12+i) = insertText(img,[6 4],num2str(predictedNum),'FontSize',9,'TextColor','red','BoxOpacity',0.4); 
    end 

  end
end
% 寢壥偺昞帵
figure;montage(Ir, 'Size', [10,12]); title(['Correct Prediction: ' num2str(cntTrue)]);

%% 廔椆






%% APP傪巊偭偨暘椶婍偺惗惉
dataTable = table(trainingFeatures, trainingLabels, 'VariableNames',{'features', 'label'});     % 1x324 偺摿挜儀僋僩儖 + 儔儀儖   偑丄200峴
openvar('dataTable');
classificationLearner
  % 怴婯僙僢僔儑儞 => 儚乕僋僗儁乕僗偐傜
  % 僨乕僞偺僀儞億乕僩丗(僨僼僅儖僩偺丄'楍傪曄悢偲偟偰巊梡' 傪慖戰)
  % 堦斣壓傪丄"梊應巕"偐傜"墳摎"傊曄峏
  %    梊應巕丗妛廗梡偺摿挜検乮峴:僨乕僞悢 x 楍:摿挜検 偺悢抣攝楍乯
  %    墳摎 丗 嫵巘僨乕僞乮categorical array, cell array of strings, character array, logical, or numeric 偺楍儀僋僩儖乯
  % 慄宍SVM傪慖戰偟 -> 妛廗儃僞儞
  % Confusion Matrix偺昞帵
  %   岎嵎専掕丗N屄偺僌儖乕僾偵暘妱偟丄#1傪庢傝彍偒#2~#N-1偺僌儖乕僾偱妛廗偟#1偱僥僗僩丄師偼#2傪庢傝彍偒丄丄丄丄丄傪孞傝曉偡
  %   儂乕儖僪傾僂僩専掕丗堦晹偺僨乕僞傪丄僥僗僩梡偺僨乕僞偲偟偰庢傝彍偄偨僨乕僞偱妛廗丗僥僗僩梡僨乕僞偵懳偟岆傝棪傪昡壙
  %   専掕側偟          丗慡偰偺妛廗僨乕僞偱妛廗丗梡偄偨慡偰偺妛廗僨乕僞偱岆傝棪傪寁嶼
  % ROC (receiver operating characteristic curve) 嬋慄昞帵
  
  % 儌僨儖偺僄僋僗億乕僩丗 trainedClassifier
  %      僐儞僷僋僩儌僨儖丗妛廗僨乕僞偼僄僋僗億乕僩偟側偄
%%
Ir = zeros([16,16,3,120], 'uint8');      % 寢壥傪奿擺偡傞攝楍
cntTrue = 0;
for digit = 0:9   % 
  for i = 1:12         % 奺悢帤偛偲偵12枃偺庤彂偒暥帤
    img = read(testSet(digit+1), i);    % testSet()偼丄1偐傜巒傑傞偺偱丄+1
    BW = im2bw(img,graythresh(img));    % 2抣壔

    features = extractHOGFeatures(BW,'CellSize',cellSize);
    predictedNum = trainedClassifier.predictFcn(table(features));           % testFeature 傪攝楍偵偟偰丄偁偲偱傑偲傔偰敾掕傕壜
    
    if predictedNum == digit    %惓偟偄幆暿偼惵怓丄岆擣幆偼愒怓
      Ir(:,:,:,digit*12+i) = insertText(img,[6 4],num2str(predictedNum),'FontSize',9,'TextColor','blue','BoxOpacity',0.4);
      cntTrue = cntTrue+1;
    else
      Ir(:,:,:,digit*12+i) = insertText(img,[6 4],num2str(predictedNum),'FontSize',9,'TextColor','red','BoxOpacity',0.4); 
    end 

  end
end
% 寢壥偺昞帵
figure;montage(Ir, 'Size', [10,12]); title(['Correct Prediction: ' num2str(cntTrue)]);

%% 廔椆








%% 晞崋壔愝寁峴楍傪昞帵 :1懳1 晞崋壔愝寁乮妛廗婍悢丗K(K-1)/2 乯
CodingMat = svmModel.CodingMatrix
%% 暿偺晞崋壔愝寁丗 1懳懠 晞崋壔愝寁乮妛廗婍悢丗K 乯
svmModel = fitcecoc(trainingFeatures, trainingLabels, 'Coding','onevsall')
CodingMat = svmModel.CodingMatrix

%% 暿偺晞崋壔愝寁丗姰慡2崁 晞崋壔愝寁 (忢偵慡偰偺僋儔僗傪巊梡丄妛廗婍悢丗2^(K-1) -1)
svmModel = fitcecoc(trainingFeatures, trainingLabels, 'Coding','binarycomplete')
CodingMat = svmModel.CodingMatrix


%% Copyright 2013-2014 The MathWorks, Inc.
% 夋憸僨乕僞僙僢僩
% 僩儗乕僯儞僌夋憸丗insertText娭悢偱帺摦嶌惉 (廃埻偵暿偺悢帤桳傝)
% 僥僗僩夋憸丗庤彂偒偺夋憸傪巊梡

% 晞崋壔愝寁峴楍傪昞帵 :1懳1 晞崋壔愝寁乮妛廗婍悢丗K(K-1)/2 乯
CodingMat = svmModel.CodingMatrix