You can use the bag-of-features (BoF) framework with many different
types of image features. To use a custom feature extractor instead
of the default speeded-up robust features (SURF) feature extractor,
use the CustomExtractor
property of a bagOfFeatures
object.
This example shows how to write a custom feature extractor function
for bagOfFeatures
. You can open this example function
file and use it as a template by typing the following command at the MATLAB® command
prompt:
edit('exampleBagOfFeaturesExtractor.m')
Step 1. Define the image sets.
Step 2. Create a new extractor function file.
Step 3. Preprocess the image.
Step 4. Select a point location for feature extraction.
Step 5. Extract features.
Step 6. Compute the feature metric.
Read the category images and create image sets.
setDir = fullfile(toolboxdir('vision'),'visiondata','imageSets'); imds = imageDatastore(setDir,'IncludeSubfolders',true,'LabelSource',... 'foldernames');
The extractor function must be specified as a function handle:
extractorFcn = @exampleBagOfFeaturesExtractor; bag = bagOfFeatures(imgSets,'CustomExtractor',extractorFcn)
exampleBagOfFeaturesExtractor
is
a MATLAB function. For example:function [features,featureMetrics] = exampleBagOfFeaturesExtractor(img) ...
location
output:function [features,featureMetrics,location] = exampleBagOfFeaturesExtractor(img) ...
The function must be on the path or in the current working folder.
Argument | Input/Output | Description |
---|---|---|
img | Input |
|
features | Output |
|
featureMetrics | Output |
|
location | Output |
|
Input images can require preprocessing before feature extraction.
To extract SURF features and to use the detectSURFFeatures
or detectMSERFeatures
functions, the images
must be grayscale. If the images are not grayscale, you can convert
them using the rgb2gray
function.
[height,width,numChannels] = size(I); if numChannels > 1 grayImage = rgb2gray(I); else grayImage = I; end
Use a regular spaced grid of point locations. Using the grid over the image allows for dense SURF feature extraction. The grid step is in pixels.
gridStep = 8; gridX = 1:gridStep:width; gridY = 1:gridStep:height; [x,y] = meshgrid(gridX,gridY); gridLocations = [x(:) y(:)];
You can manually concatenate multiple SURFPoints
objects at different scales to achieve
multiscale feature extraction.
multiscaleGridPoints = [SURFPoints(gridLocations,'Scale',1.6); SURFPoints(gridLocations,'Scale',3.2); SURFPoints(gridLocations,'Scale',4.8); SURFPoints(gridLocations,'Scale',6.4)];
detectSURFFeatures
or detectMSERFeatures
, to select point locations.multiscaleSURFPoints = detectSURFFeatures(I);
Extract features from the selected point locations. By default, bagOfFeatures
extracts
upright SURF features.
features = extractFeatures(grayImage,multiscaleGridPoints,'Upright',true);
The feature metrics indicate the strength of each feature. Larger
metric values are assigned to stronger features. Use feature metrics
to identify and remove weak features before using bagOfFeatures
to
learn the visual vocabulary of an image set. Use the metric that is
suitable for your feature vectors.
For example, you can use the variance of the SURF features as the feature metric.
featureMetrics = var(features,[],2);
If you used a feature detector for the point selection, then use the detection metric instead.
featureMetrics = multiscaleSURFPoints.Metric;
You can optionally return the feature location information.
The feature location can be used for spatial or geometric verification
image search applications. See the Geometric Verification Using estimateGeometricTransform2D Function example.
The retrieveImages
and indexImages
functions are used for content-based
image retrieval systems.
if nargout > 2 varargout{1} = multiscaleGridPoints.Location; end