Professional Documents
Culture Documents
93009v00 Image Class Bag Features 2016
93009v00 Image Class Bag Features 2016
Image classification, or object recognition, is the process of identifying a specific object or class of objects
in an image or video. Computer Vision System Toolbox™ offers a variety of algorithms, tools, and tech-
niques to create image classification and object recognition systems.
This example shows how to use a “bag of features” approach for image category classification. This tech-
nique is also often referred to as bag of words. Visual image categorization is a process of assigning a cate-
gory label to an image under test. Categories may contain images representing just about anything, for
example, dogs, cats, trains, or boats.
Note that downloading the 126MB set from the web can take 30 minutes or more depending on your
Internet connection. The commands below will block MATLAB® for that period of time. Alternatively, you
can use your web browser to first download the set to your local disk. If you choose that route, repoint the
'url' variable above to the file that you downloaded.
Next, you can construct an array of image sets based on the following categories from Caltech 101:
'airplanes', 'ferry', and 'laptop'. To manage the data, you can use the imageSet class. Since
imageSet operates on image file locations, and therefore does not load all the images into memory, it is
safe to use on large image collections.
Each element of the imgSets variable now contains images associated with the particular category. You
can easily inspect the number of images per category as well as category labels as shown below:
ans =
800 67 81
Note that the labels were derived from directory names used to construct the image sets, but can be cus-
tomized by manually setting the Description property of the imageSet object.
Preparing the Training and Validation Image Sets
imgSets contains an unequal number of images per category. You can adjust it to balance the number of
images in the training set.
% Notice that each set now has exactly the same number of images.
[imgSets.Count]
ans =
67 67 67
You can then separate the sets into training and validation data, using 30% of images from each set for the
training data and the remainder, 70%, for the validation data. To avoid biasing the results, you can ran-
domize the split.
The above call returns two arrays of imageSet objects ready for training and validation tasks. Below, you
can see example images from the three categories included in the training data.
airplanes = read(trainingSets(1),1);
ferry = read(trainingSets(2),1);
laptop = read(trainingSets(3),1);
figure
subplot(1,3,1);
imshow(airplanes)
subplot(1,3,2);
imshow(ferry)
subplot(1,3,3);
imshow(laptop)
bag = bagOfFeatures(trainingSets);
Creating Bag-Of-Features from 3 image sets.
--------------------------------------------
* Image set 1: airplanes.
* Image set 2: ferry.
* Image set 3: laptop.
Additionally, the bagOfFeatures object provides an encode method for counting the visual word
occurrences in an image. It produced a histogram that becomes a new and reduced representation of an
image.
The above function utilizes the encode method of the input bag object to formulate feature vectors repre-
senting each image category from the trainingSets array of imageSet objects.
* Category 1: airplanes
* Category 2: ferry
* Category 3: laptop
PREDICTED
KNOWN | airplanes ferry laptop
--------------------------------------------
airplanes | 0.90 0.10 0.00
ferry | 0.00 1.00 0.00
laptop | 0.00 0.00 1.00
* Average Accuracy is 0.97.
Next, you can evaluate the classifier on the validationSet, which was not used during the training. By
default, the evaluate function returns the confusion matrix, which is a good initial indicator of how well
the classifier is performing.
* Category 1: airplanes
* Category 2: ferry
* Category 3: laptop
PREDICTED
KNOWN | airplanes ferry laptop
--------------------------------------------
airplanes | 0.77 0.15 0.09
ferry | 0.04 0.91 0.04
laptop | 0.09 0.00 0.91
* Average Accuracy is 0.87.
You can derive additional statistics using the rest of the arguments returned by the evaluate function.
You can tweak the various parameters and continue evaluating the trained classifier until you are satisfied
with the results.
'airplanes'
© 2016 The MathWorks, Inc. MATLAB and Simulink are registered trademarks of The MathWorks, Inc. See mathworks.com/trademarks for a list of additional trademarks.
Other product or brand names may be trademarks or registered trademarks of their respective holders.
mathworks.com
93009v00 05/16