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

    %% Search ROI for Object
%
%%
% Define a set of images to search.
imageFiles = ...
  {'elephant.jpg', 'cameraman.tif', ...
   'peppers.png',  'saturn.png',...
   'pears.png',    'stapleRemover.jpg', ...
   'football.jpg', 'mandi.tif',...
   'kids.tif',     'liftingbody.png', ...
   'office_5.jpg', 'gantrycrane.png',...
   'moon.tif',     'circuit.tif', ...
   'tape.png',     'coins.png'};

imgSet = imageSet(imageFiles);
%%
% Learn the visual vocabulary.
bag = bagOfFeatures(imgSet,'PointSelection','Detector',...
  'VocabularySize',1000);

%%
% Create an image search index and add images.
imageIndex = invertedImageIndex(bag);

addImages(imageIndex, imgSet);

%%
% Specify a query image and an ROI to search for the target object, 
% elephant.
queryImage = imread('clutteredDesk.jpg');
queryROI = [130 175 330 365]; 

figure
imshow(queryImage)
rectangle('Position',queryROI,'EdgeColor','yellow')
%%  
% You can also use the |imrect| function to select an ROI interactively.
% For example, |queryROI = getPosition(imrect)|.

%% 
% Find images that contain the object.
imageIDs = retrieveImages(queryImage,imageIndex,'ROI',queryROI)

bestMatch = imageIDs(1);

figure
imshow(imageIndex.ImageLocation{bestMatch})