资源描述:
《sift算法的MATLAB程序.doc》由会员上传分享,免费在线阅读,更多相关内容在行业资料-天天文库。
1、%[image,descriptors,locs]=sift(imageFile)%%ThisfunctionreadsanimageandreturnsitsSIFTkeypoints.%Inputparameters:%imageFile:thefilenamefortheimage.%%Returned:%image:theimagearrayindoubleformat%descriptors:aK-by-128matrix,whereeachrowgivesaninvariant%descriptorforoneoftheK
2、keypoints.Thedescriptorisavector%of128valuesnormalizedtounitlength.%locs:K-by-4matrix,inwhicheachrowhasthe4valuesfora%keypointlocation(row,column,scale,orientation).The%orientationisintherange[-PI,PI]radians.%%Credits:ThanksforinitialversionofthisprogramtoD.Alvaroand%J.
3、J.Guerrero,UniversidaddeZaragoza(modifiedbyD.Lowe)function[image,descriptors,locs]=sift(imageFile)%Loadimageimage=imread(imageFile);%IfyouhavetheImageProcessingToolbox,youcanuncommentthefollowing%linestoallowinputofcolorimages,whichwillbeconvertedtograyscale.%ifisrgb(im
4、age)%image=rgb2gray(image);%end[rows,cols]=size(image);%ConvertintoPGMimagefile,readableby"keypoints"executablef=fopen('tmp.pgm','w');iff==-1error('Couldnotcreatefiletmp.pgm.');endfprintf(f,'P5%d%d255',cols,rows);fwrite(f,image','uint8');fclose(f);%Callkeypoints
5、executableifisunixcommand='!./sift';elsecommand='!siftWin32';endcommand=[command'tmp.key'];eval(command);%Opentmp.keyandcheckitsheaderg=fopen('tmp.key','r');ifg==-1error('Couldnotopenfiletmp.key.');end[header,count]=fscanf(g,'%d%d',[12]);ifcount~=2error('Invali
6、dkeypointfilebeginning.');endnum=header(1);len=header(2);iflen~=128error('Keypointdescriptorlengthinvalid(shouldbe128).');end%Createsthetwooutputmatrices(useknownsizeforefficiency)locs=double(zeros(num,4));descriptors=double(zeros(num,128));%Parsetmp.keyfori=1:num[vecto
7、r,count]=fscanf(g,'%f%f%f%f',[14]);%rowcolscaleoriifcount~=4error('Invalidkeypointfileformat');endlocs(i,:)=vector(1,:);[descrip,count]=fscanf(g,'%d',[1len]);if(count~=128)error('Invalidkeypointfilevalue.');end%Normalizeeachinputvectortounitlengthdescrip=descrip/sqrt(su
8、m(descrip.^2));descriptors(i,:)=descrip(1,:);endfclose(g);