Main Content

evaluateImageRetrieval

评估图像的年代earch results

Description

example

averagePrecision= evaluateImageRetrieval(queryImage,imageIndex,expectedIDs)returns the average precision metric for measuring the accuracy of image search results for thequeryImage. TheexpectedIDsinput contains the indices of images withinimageIndexthat are known to be similar to the query image.

[averagePrecision,imageIDs,scores] = evaluateImageRetrieval(queryImage,imageIndex,expectedIDs)optionally returns the indices corresponding to images withinimageIndexthat are visually similar to the query image. It also returns the corresponding similarity scores.

[averagePrecision,imageIDs,scores] = evaluateImageRetrieval(___,Name,Value)uses additional options specified by one or moreName,Valuepair arguments, using any of the preceding syntaxes.

Examples

collapse all

Define a set of images.

dataDir = fullfile(toolboxdir('vision'),'visiondata','bookCovers'); bookCovers = imageDatastore(dataDir);

Display the set of images.

thumbnailGallery = [];fori = 1:length(bookCovers.Files) img = readimage(bookCovers,i); thumbnail = imresize(img,[300 300]); thumbnailGallery = cat(4,thumbnailGallery,thumbnail);endfigure montage(thumbnailGallery);

Figure contains an axes object. The axes object contains an object of type image.

Index the images. This will take a few minutes.

imageIndex = indexImages(bookCovers);
Creating an inverted image index using Bag-Of-Features. ------------------------------------------------------- Creating Bag-Of-Features. ------------------------- * Selecting feature point locations using the Detector method. * Extracting SURF features from the selected feature point locations. ** detectSURFFeatures is used to detect key points for feature extraction. * Extracting features from 58 images...done. Extracted 29216 features. * Keeping 80 percent of the strongest features from each category. * Balancing the number of features across all image categories to improve clustering. ** Image category 1 has the least number of strongest features: 23373. ** Using the strongest 23373 features from each of the other image categories. * Creating a 20000 word visual vocabulary. * Number of levels: 1 * Branching factor: 20000 * Number of clustering steps: 1 * [Step 1/1] Clustering vocabulary level 1. * Number of features : 23373 * Number of clusters : 20000 * Initializing cluster centers...100.00%. * Clustering...completed 10/100 iterations (~1.08 seconds/iteration)...converged in 10 iterations. * Finished creating Bag-Of-Features Encoding images using Bag-Of-Features. -------------------------------------- * Encoding 58 images...done. Finished creating the image index.

Select and display the query image.

queryDir = fullfile(dataDir,'queries',filesep); query = imread([queryDir'query2.jpg']); figure imshow(query)

Figure contains an axes object. The axes object contains an object of type image.

Evaluation requires knowing the expected results. Here, the query image is known to be the 3rd book in the imageIndex.

expectedID = 3;

Find and report the average precision score.

[averagePrecision,actualIDs] = evaluateImageRetrieval(query,...imageIndex,expectedID); fprintf('Average Precision: %f\n\n',averagePrecision)
Average Precision: 0.041667

Show the query and best match side-by-side.

bestMatch = actualIDs(1); bestImage = imread(imageIndex.ImageLocation{bestMatch}); figure imshowpair(query,bestImage,'montage')

Figure contains an axes object. The axes object contains an object of type image.

Create an image set of book covers.

dataDir = fullfile(toolboxdir('vision'),'visiondata','bookCovers'); bookCovers = imageDatastore(dataDir);

Index the image set. The indexing may take a few minutes.

imageIndex = indexImages(bookCovers,'Verbose',false);

Create a set of query images.

queryDir = fullfile(dataDir,'queries',filesep); querySet = imageDatastore(queryDir);

Specify the expected search results for each query image.

expectedIDs = [1 2 3];

Evaluate each query image and collect average precision scores.

fori = 1:numel(querySet.Files) query = readimage(querySet,i); averagePrecision(i) = evaluateImageRetrieval(query, imageIndex, expectedIDs(i));end

Compute mean average precision (MAP).

map = mean(averagePrecision)
map = 0.8333

Input Arguments

collapse all

Input query image, specified as either anM-by-N-by-3 truecolor image or anM-by-N2-D grayscale image.

Data Types:single|double|int16|uint8|uint16|logical

Image search index, specified as aninvertedImageIndexobject. TheindexImagesfunction creates theinvertedImageIndexobject, which stores the data used for the image search.

Image indices, specified as a row or column vector. The indices correspond to the images withinimageIndexthat are known to be similar to the query image.

Name-Value Arguments

Specify optional pairs of arguments asName1=Value1,...,NameN=ValueN, whereNameis the argument name andValueis the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and encloseNamein quotes.

Example:'NumResults',25

Maximum number of search results to evaluate, specified as the comma-separated pair consisting of 'NumResults' and a positive integer value. The function evaluates the topNumResultsand returns the average-precision-at-NumResultsmetric.

Rectangular search region within the query image, specified as the comma-separated pair consisting of 'ROI' and a [xywidthheight] formatted vector.

Output Arguments

collapse all

Average precision metric, returned as a scalar value in the range [0 1]. The average precision metric represents the accuracy of image search results for the query image.

Ranked index of retrieved images, returned as anM1的向量. The image IDs are returned in ranked order, from the most to least similar matched image.

Similarity metric, returned as anN1的向量. This output contains the scores that correspond to the retrieved images in theimageIDsoutput. The scores are computed using the cosine similarity and range from 0 to 1.

Version History

Introduced in R2015a