www.gusucode.com > vision 源码程序 matlab案例代码 > vision/GeometricVerificationUsingEstimateGeometricTransformExample.m
%% Geometric Verification Using |estimateGeometricTransform| Function % Use the locations of visual words to verify the best search result. % To rerank the search results based on geometric information, repeat % this procedure for the top _N_ search results. %% % Specify the location of the images. dataDir = fullfile(toolboxdir('vision'),'visiondata','bookCovers'); bookCovers = imageDatastore(dataDir); %% % Index the image set. This process can take a few minutes. imageIndex = indexImages(bookCovers); %% % Select and display the query image. queryDir = fullfile(dataDir,'queries',filesep); queryImage = imread([queryDir 'query3.jpg']); figure imshow(queryImage) %% % Retrieve the best matches. The |queryWords| output contains visual word % locations information for the query image. Use this information to % verify the search results. [imageIDs, ~, queryWords] = retrieveImages(queryImage,imageIndex); %% % Find the best match for the query image by extracting the visual words % from the image index. The image index contains the visual word % information for all images in the index. bestMatch = imageIDs(1); bestImage = imread(imageIndex.ImageLocation{bestMatch}); bestMatchWords = imageIndex.ImageWords(bestMatch); %% % Generate a set of tentative matches based on visual word assignments. % Each visual word in the query can have multiple matches due to the hard % quantization used to assign visual words. queryWordsIndex = queryWords.WordIndex; bestMatchWordIndex = bestMatchWords.WordIndex; tentativeMatches = []; for i = 1:numel(queryWords.WordIndex) idx = find(queryWordsIndex(i) == bestMatchWordIndex); matches = [repmat(i, numel(idx), 1) idx]; tentativeMatches = [tentativeMatches; matches]; end %% % Show the point locations for the tentative matches. There are many % poor matches. points1 = queryWords.Location(tentativeMatches(:,1),:); points2 = bestMatchWords.Location(tentativeMatches(:,2),:); figure showMatchedFeatures(queryImage,bestImage,points1,points2,'montage') %% % Remove poor visual word assignments using |estimateGeometricTransform| % function. Keep the assignments that fit a valid geometric transform. [tform,inlierPoints1,inlierPoints2] = ... estimateGeometricTransform(points1,points2,'affine',... 'MaxNumTrials',2000); %% % Rerank the search results by the percentage of inliers. Do this when % the geometric verification procedure is applied to the top _N_ search % results. Those images with a higher percentage of inliers are more % likely to be relevant. percentageOfInliers = size(inlierPoints1,1)./size(points1,1); figure showMatchedFeatures(queryImage,bestImage,inlierPoints1,... inlierPoints2,'montage') %% % Apply the estimated transform. outputView = imref2d(size(bestImage)); Ir = imwarp(queryImage, tform, 'OutputView', outputView); figure imshowpair(Ir,bestImage,'montage')