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

    %% Predict Category for Image
%
%% 
% Load two image category sets.
setDir  = fullfile(toolboxdir('vision'),'visiondata','imageSets');
imds = imageDatastore(setDir,'IncludeSubfolders',true,'LabelSource',...
    'foldernames');
%% 
% Separate the two sets into 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 a bag of visual words.
bag = bagOfFeatures(trainingSet);
%% 
% Train a classifier.
categoryClassifier = trainImageCategoryClassifier(trainingSet,bag);
%% 
% Predict category label for one of the images in test set.
img = readimage(testSet,1);
[labelIdx, score] = predict(categoryClassifier,img);
categoryClassifier.Labels(labelIdx)