您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 管理学资料 > sift算法的MATLAB程序
%[image,descriptors,locs]=sift(imageFile)%%ThisfunctionreadsanimageandreturnsitsSIFTkeypoints.%Inputparameters:%imageFile:thefilenamefortheimage.%%Returned:%image:theimagearrayindoubleformat%descriptors:aK-by-128matrix,whereeachrowgivesaninvariant%descriptorforoneoftheKkeypoints.Thedescriptorisavector%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)%Loadimageimage=imread(imageFile);%IfyouhavetheImageProcessingToolbox,youcanuncommentthefollowing%linestoallowinputofcolorimages,whichwillbeconvertedtograyscale.%ifisrgb(image)%image=rgb2gray(image);%end[rows,cols]=size(image);%ConvertintoPGMimagefile,readablebykeypointsexecutablef=fopen('tmp.pgm','w');iff==-1error('Couldnotcreatefiletmp.pgm.');endfprintf(f,'P5\n%d\n%d\n255\n',cols,rows);fwrite(f,image','uint8');fclose(f);%Callkeypointsexecutableifisunixcommand='!./sift';elsecommand='!siftWin32';endcommand=[command'tmp.pgmtmp.key'];eval(command);%Opentmp.keyandcheckitsheaderg=fopen('tmp.key','r');ifg==-1error('Couldnotopenfiletmp.key.');end[header,count]=fscanf(g,'%d%d',[12]);ifcount~=2error('Invalidkeypointfilebeginning.');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[vector,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(sum(descrip.^2));descriptors(i,:)=descrip(1,:);endfclose(g);
本文标题:sift算法的MATLAB程序
链接地址:https://www.777doc.com/doc-5231293 .html