
sift算法
-
2023年3月5日发(作者:关爱残疾人的标语)%[image,descriptors,locs]=sift(imageFile)
%
%ThisfunctionreadsanimageandreturnsitsSIFTkeypoints。
%Inputparameters:
%imageFile:thefilenamefortheimage。
%
%Returned:
%image:theimagearrayindoubleformat
%descriptors:aK—by-128matrix,whereeachrowgivesan
invariant
%criptorisa
vector
%of128valuesnormalizedtounitlength。
%locs:K-by—4matrix,inwhicheachrowhasthe4valuesfora
%keypointlocation(row,column,scale,orientation).The
%orientationisintherange[-PI,PI]radians.
%
%Credits:ThanksforinitialversionofthisprogramtoD。Alvaroand
%J。J。Guerrero,UniversidaddeZaragoza(modifiedbyD。
Lowe)
function[image,descriptors,locs]=sift(imageFile)
%Loadimage
image=imread(imageFile);
%IfyouhavetheImageProcessingToolbox,youcanuncommentthe
following
%linestoallowinputofcolorimages,whichwillbeconvertedto
grayscale。
%ifisrgb(image)
%image=rgb2gray(image);
%end
[rows,cols]=size(image);
%ConvertintoPGMimagefile,readableby”keypoints"executable
f=fopen('','w’);
iff==—1
error(’。’);
end
fprintf(f,’P5n%dn%dn255n',cols,rows);
fwrite(f,image',’uint8’);
fclose(f);
%Callkeypointsexecutable
ifisunix
command=’!./sift';
else
command='!siftWin32';
end
command=[command’〈tmp。pgm〉tmp。key'];
eval(command);
%Opentmp。keyandcheckitsheader
g=fopen('tmp。key’,'r');
ifg==—1
error(’。');
end
[header,count]=fscanf(g,’%d%d',[12]);
ifcount~=2
error(’Invalidkeypointfilebeginning。’);
end
num=header(1);
len=header(2);
iflen~=128
error(’Keypointdescriptorlengthinvalid(shouldbe128)。');
end
%Createsthetwooutputmatrices(useknownsizeforefficiency)
locs=double(zeros(num,4));
descriptors=double(zeros(num,128));
%Parsetmp。key
fori=1:num
[vector,count]=fscanf(g,’%f%f%f%f’,[14]);%rowcol
scaleori
ifcount~=4
error('Invalidkeypointfileformat’);
end
locs(i,:)=vector(1,:);
[descrip,count]=fscanf(g,'%d',[1len]);
if(count~=128)
error(’Invalidkeypointfilevalue.');
end
%Normalizeeachinputvectortounitlength
descrip=descrip/sqrt(sum(descrip。^2));
descriptors(i,:)=descrip(1,:);
end
fclose(g);