% Example use of the Log Polar Magnitude feature descriptor % % % ========================================================================= % % Copyright (c) 2016 Anders Hast, Damian Matuszewski % Centre for Image Analysis % Uppsala University % % Permission is hereby granted, free of charge, to any person obtaining a % copy of this software and associated documentation files (the "Software"), % to deal in the Software without restriction, subject to the following % conditions: % % 1) Any scientific publications coming from using or modifying this code % should cite the original paper: % Matuszewski, D.J., Hast, A., Wählby, C., Sintorn, I.-M. (2016) % A short feature vector for image matching: the Log Polar Magnitude % feature descriptor. Computer Vision and Image Understanding [submitted] % % 2) The above copyright notice and this permission notice shall be % included in all copies or substantial portions of the Software. % % 3) The Software is provided "as is", without warranty of any kind. % % ========================================================================= % imgAPath = 'imgA.jpg'; imgBPath = 'imgB.jpg'; %% initialize the detector and descriptor detector = FeatureDetector; descriptor = LPMDescriptor; % change the parameters % detector.Opts.Method = 'SIFT'; % detector.Opts.VL_Feat_Path = 'C:/Users/Administrator/Documents/Anders Hast projects'; % descriptor.Opts.FrameScalingFactor = 14; % set the scaling factor to value recommended for SIFT frames % --- % detector.Opts.Method = 'SURF'; % descriptor.Opts.FrameScalingFactor = 9; % set the scaling factor to value recommended for SURF frames % --- detector.Opts.Method = 'Harris'; descriptor.Opts.FixedRadius = true; %% get the features in the two images framesA = detector.getFrames(imgAPath); framesB = detector.getFrames(imgBPath); %% calculate the feature vectors [framesA, featVectorsA] = descriptor.extractDescriptors(imgAPath, framesA); [framesB, featVectorsB] = descriptor.extractDescriptors(imgBPath, framesB); %% find matching features indexPairs = matchFeatures(featVectorsA', featVectorsB') ; matchedPointsA = framesA(:,indexPairs(:, 1)); matchedPointsB = framesB(:,indexPairs(:, 2)); %% verify the matching pairs with Optimal RANSAC addpath(fullfile('RANSAC_code')) % get image size info for setting Optimal RANSAC parameters imgA = imread(imgAPath); info = imfinfo(imgAPath); imgASize = size(imgA); imgB = imread(imgBPath); info = imfinfo(imgBPath); imgBSize = size(imgB); acc = 2.5/min([imgASize(1),imgASize(2),imgBSize(1),imgBSize(2)]); t = acc*1; [Hab1, inliers, iter1] = optimalRansacfithomography(... [matchedPointsA(1,:); matchedPointsA(2,:)], ... [matchedPointsB(1,:); matchedPointsB(2,:)], ... t,acc); %% display the results fprintf('\n') matchedPointsNum = length(matchedPointsA(1,:)) inliersNum = length(inliers) ratio = inliersNum / matchedPointsNum % display visual results addpath(fullfile('Display_code')) % convert the images to double for display imgChannels = 1; if length(imgASize) > 2 if imgASize(3) > 1 imgA = rgb2gray(imgA); imgChannels = imgASize(3); end end imgA = double(imgA)/(2^(info.BitDepth/imgChannels) - 1); imgChannels = 1; if length(imgBSize) > 2 if imgBSize(3) > 1 imgB = rgb2gray(imgB); imgChannels = imgBSize(3); end end imgB = double(imgB)/(2^(info.BitDepth/imgChannels) - 1); % Padding between images sp = floor(min(max(max(imgASize(2),imgBSize(2))*0.01,4),20)); both = ones(max(imgASize(1),imgBSize(1)),imgASize(2)+imgBSize(2)+sp); both(1:imgASize(1),1:imgASize(2)) = imgA; both(1:imgBSize(1),imgASize(2)+sp+1:imgASize(2)+imgBSize(2)+sp) = imgB; figure, imshow(both); figure, showCorr([matchedPointsA(1,:); matchedPointsA(2,:)], ... [matchedPointsB(1,:); matchedPointsB(2,:)], ... imgA, imgB, inliers, false) figure, showCorr([matchedPointsA(1,:); matchedPointsA(2,:)], ... [matchedPointsB(1,:); matchedPointsB(2,:)], ... imgA, imgB, inliers, true);