www.gusucode.com > vision 源码程序 matlab案例代码 > vision/TrainEvaluateAndApplyAnImageCategoryClassifierExample.m

    %% Train, Evaluate, and Apply Image Category Classifier
%
%% 
% Load two image categories.
setDir  = fullfile(toolboxdir('vision'),'visiondata','imageSets');
imds = imageDatastore(setDir,'IncludeSubfolders',true,'LabelSource',...
    'foldernames');
%% 
% Split the data set into a training and test data. Pick 30% of images 
% from each set for the training data and the remainder  70% for the 
% test data.
[trainingSet,testSet] = splitEachLabel(imds,0.3,'randomize');
%% 
% Create bag of visual words.
bag = bagOfFeatures(trainingSet);
%% 
% Train a classifier with the training sets.
categoryClassifier = trainImageCategoryClassifier(trainingSet,bag);
%% 
% Evaluate the classifier using test images. Display the confusion matrix.
confMatrix = evaluate(categoryClassifier,testSet)
%% 
% Find the average accuracy of the classification.
mean(diag(confMatrix))
%% 
% Apply the newly trained classifier to categorize new images.
img = imread(fullfile(setDir,'cups','bigMug.jpg'));
[labelIdx, score] = predict(categoryClassifier,img);
%% 
% Display the classification label.
categoryClassifier.Labels(labelIdx)