MATLAB: MatchFeatures gives two different answers

computer visionComputer Vision Toolboxmatchfeatures

I tried to run this code:
I1 = rgb2gray(imread('viprectification_deskLeft.png'));
I2 = rgb2gray(imread('viprectification_deskRight.png'));
points1 = detectSURFFeatures(I1);
points2 = detectSURFFeatures(I2);
[features1, valid_points1] = extractFeatures(I1, points1);
[features2, valid_points2] = extractFeatures(I2, points2);
indexPairs1 = matchFeatures(features1, features2);
indexPairs2 = matchFeatures(features2, features1);
My Question: Why indexPairs1 and indexPairs2 have different length? I suppose it share the same length rite?

Best Answer

They are not expected to share the same length. The first tries to find the best matches for features1 in features2 and the second tries to find the best matches for features2 in features1.
For the nearest neighbor ratio matching method (the default), this plays out as follows:
Assume features1 has 100 features and features2 has 200.
In the case of matchFeatures(features1,features2), for each of the 100 features in features1, the top 2 matches (2 nearest neighbors) from the 200 features in features2 are found. This gives us 100 pairs of 2 features each. These are then accepted or rejected by seeing whether the distance ratio between the nearest neighbor and the second nearest neighbor.
The process I described above is not symmetric. So, you can expect that matchFeatures(features2,features1) maybe slightly different.
You can look at this paper for details about this: K. Mikolajczyk and C. Shmid, "A Performance Evaluation of Local Descriptors," IEEE PAMI, 27, 10 (2005)
If you would like symmetric matches, try the 'NearestNeighborSymmetric' method:
indexPairs1 = matchFeatures(features1,features2,'Method','NearestNeighborSymmetric');
indexPairs2 = matchFeatures(features2,features1,'Method','NearestNeighborSymmetric');
>> isequal(fliplr(sort(indexPairs2)),sort(indexPairs1))
ans =
1