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

    %% Create Search Index Using Custom Bag of Features
%
%%
% Create an image set.
setDir  = fullfile(toolboxdir('vision'),'visiondata','imageSets','cups');
imgSets = imageSet(setDir, 'recursive');
%%  
% Display image set.
thumbnailGallery = [];
for i = 1:imgSets.Count
    I = read(imgSets, i);
    thumbnail = imresize(I, [300 300]);
    thumbnailGallery = cat(4, thumbnailGallery, thumbnail);
end

figure
montage(thumbnailGallery);
%%
% Train a bag of features using a custom feature extractor.
extractor = @exampleBagOfFeaturesExtractor;
bag = bagOfFeatures(imgSets,'CustomExtractor',extractor);

%%
% Use the trained bag of features to index the image set.
imageIndex = indexImages(imgSets,bag,'Verbose',false) 

queryImage = read(imgSets,4);

figure
imshow(queryImage)
%%
% Search for the image from image index using query image.
indices = retrieveImages(queryImage,imageIndex);
bestMatch = imageIndex.ImageLocation{indices(1)};
figure
imshow(bestMatch)