After I careful thinking, I was lucky enough to find out what the problem was and I am stating the following in the hope that it will help others：
When the bagofFeatures object becomes an input parameter to the invertedImageIndex function, i.e. when constructing an object of the invertedImageIndex class, there are no image features inside the instance of this object to establish a one-to-one correspondence with the image. Before the image retrieval task is performed, i.e. before the retrieveImages function is called, features and the ID number of the corresponding image should be added to each image. This is done by the addImageFeatures object function, which was introduced in R2021b for use in the vSLAM task loop closure detection.
The hyperparameter to the bagOfFeatures function is indeed an empirical adjustment, as described in the documentation.
dataDir = fullfile(toolboxdir('vision'),'visiondata','bookCovers');
bookCovers = imageDatastore(dataDir);
queryDir = fullfile(dataDir,'queries',filesep);
queryImage = imread([queryDir 'query3.jpg']);
bof = bagOfFeatures(bookCovers,CustomExtractor=@helperORBFeatureExtractorFunction,...
StrongestFeatures=1,TreeProperties=[1,500]);% when TreeProperties=[3,10],retrieveImage is not correct，Just an experience value
Creating Bag-Of-Features. ------------------------- * Extracting features using a custom feature extraction function: helperORBFeatureExtractorFunction. * Extracting features from 58 images...done. Extracted 58000 features. * Keeping 100 percent of the strongest features from each category. * Creating a 500 word visual vocabulary. * Number of levels: 1 * Branching factor: 500 * Number of clustering steps: 1 * [Step 1/1] Clustering vocabulary level 1. * Number of features : 58000 * Number of clusters : 500 * Initializing cluster centers...100.00%. * Clustering...completed 26/100 iterations (~0.14 seconds/iteration)...converged in 26 iterations. * Finished creating Bag-Of-Features
imageIndex2 = invertedImageIndex(bof,SaveFeatureLocations=false);
% note: imageIndex2 is empty index,so it should add image feature and image
% id for every canadicate image
for i = 1:length(bookCovers.Files)
currImg = readimage(bookCovers,i);
addImageFeatures(imageIndex2, feature, i);
[feature, featureMetrics]= helperORBFeatureExtractorFunction(queryImage);
% addImageFeatures(imageIndex2, feature, i+1);
imageIDs2 = retrieveImages(queryImage,imageIndex2);
simImg = readimage(bookCovers,double(imageIDs2(1)));
function [features, featureMetrics]= helperORBFeatureExtractorFunction(I)
% helperORBFeatureExtractorFunction Implements the ORB feature extraction
% used in bagOfFeatures.
% This is an example helper function that is subject to change or removal
% in future releases.
% Copyright 2021 The MathWorks, Inc.
numPoints = 1000;
% Detect ORB features
Igray = im2gray(I);
points = detectORBFeatures(Igray, 'ScaleFactor', 1.2, 'NumLevels', 8);
% Select a subset of features, uniformly distributed throughout the image
points = selectUniform(points, numPoints, size(Igray, 1:2));
% Extract features
features = extractFeatures(Igray, points);
% Compute the Feature Metric. Use the variance of features as the metric
featureMetrics = var(single(features.Features),,2);
BTW，The relationship between these functions is indeed a bit confusing, so I drew a class diagram by hand and refer to it below, so please point out any incorrectness!