predict

Predict image category

Description

example

[labelIdx,score] = predict(categoryClassifier,I) returns the predicted label index and score for the input image.

predict supports parallel computing using multiple MATLAB® workers. Enable parallel computing using the Computer Vision Toolbox Preferences dialog. To open Computer Vision Toolbox™ preferences, on the Home tab, in the Environment section, click Preferences. Select Computer Vision Toolbox.

[labelIdx,score] = predict(categoryClassifier,imds) returns the predicted label index and score for the images specified in imds.

[labelIdx,score] = predict(___,'Verbose',true) also enables progress display to the screen. Set to false to turn it off.

Examples

collapse all

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);
Creating Bag-Of-Features.
-------------------------
* Image category 1: books
* Image category 2: cups
* Selecting feature point locations using the Grid method.
* Extracting SURF features from the selected feature point locations.
** The GridStep is [8 8] and the BlockWidth is [32 64 96 128].

* Extracting features from 4 images...done. Extracted 76800 features.

* Keeping 80 percent of the strongest features from each category.

* Using K-Means clustering to create a 500 word visual vocabulary.
* Number of features          : 61440
* Number of clusters (K)      : 500

* Initializing cluster centers...100.00%.
* Clustering...completed 21/100 iterations (~0.71 seconds/iteration)...converged in 21 iterations.

* Finished creating Bag-Of-Features

Train a classifier.

categoryClassifier = trainImageCategoryClassifier(trainingSet,bag);
Training an image category classifier for 2 categories.
--------------------------------------------------------
* Category 1: books
* Category 2: cups

* Encoding features for 4 images...done.

* Finished training the category classifier. Use evaluate to test the classifier on a test set.

Predict category label for one of the images in test set.

img = readimage(testSet,1);
[labelIdx, score] = predict(categoryClassifier,img);
categoryClassifier.Labels(labelIdx)
ans = 1x1 cell array
    {'books'}

Input Arguments

collapse all

Input image, specified as either an M-by-N-by-3 truecolor image or an M-by-N 2-D grayscale image.

Image category classifier, specified as an imageCategoryClassifier object.

Images, specified as an ImageDatastore object.

Output Arguments

collapse all

Predicted label index, returned as either an M-by-1 vector for M images or a scalar value for a single image. The labelIdx output value corresponds to the index of an image set used to train the bag of features. The prediction index corresponds to the class with the lowest average binary loss of the ECOC SVM classifier.

Prediction score, specified as a 1-by-N vector or an M-by-N matrix. N represents the number of classes. M represents the number of images in the imageSet input object, imgSet. The score provides a negated average binary loss per class. Each class is a support vector machine (SVM) multiclass classifier that uses the error-correcting output codes (ECOC) approach.

Extended Capabilities

Introduced in R2014b