Contenido principal

predict

R2026b

Predict image category

Description

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

example

[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.

___ = predict(___,UseParallel=parallelOpts) additionally specifies whether to perform computations in parallel.

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.

* Creating a 500 word visual vocabulary.
* Number of levels: 1
* Branching factor: 500
* Number of clustering steps: 1

* [Step 1/1] Clustering vocabulary level 1.
* Number of features          : 61440
* Number of clusters          : 500
* Initializing cluster centers...100.00%.
* Clustering...completed 55/100 iterations (~0.69 seconds/iteration)...converged in 55 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);
Encoding images using Bag-Of-Features.
--------------------------------------
* Encoding an image...done.
categoryClassifier.Labels(labelIdx)
ans = 1×1 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.

Since R2026b

Option to perform computations in parallel using a parallel pool of workers, specified as one of these values:

  • "off" — Run in serial on the MATLAB client.

  • "auto" — Use a parallel pool if one is open or if MATLAB can automatically create one. If a parallel pool is not available, run in serial on the MATLAB client.

  • "on" — Use a parallel pool if one is open or if MATLAB can automatically create one. If a parallel pool is not available, throw an error.

If you do not have a parallel pool open and automatic pool creation is enabled, MATLAB opens a pool using the default cluster profile. Using parallel computing requires Parallel Computing Toolbox™. For more information, see Run MATLAB Functions with Automatic Parallel Support (Parallel Computing Toolbox).

Data Types: char | string

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

expand all

Version History

Introduced in R2014b

expand all