资源描述:
《图像拼接基于harris检测matlab代码.doc》由会员上传分享,免费在线阅读,更多相关内容在应用文档-天天文库。
1、.自定义函数都在下面clcclearall%读入图片pic1=imread('lena1.jpg');pic2=imread('lena2.jpg');%Harris角点检测points1=myHarris(pic1);points2=myHarris(pic2);%画出Harris角点figure(1)drawHarrisCorner(pic1,points1,pic2,points2);%角点特征描述des1=myHarrisCornerDescription(pic1,points1);des2=myHarris
2、CornerDescription(pic2,points2);%角点粗匹配matchs=myMatch(des1,des2);%获取各自出匹配角点位置matchedPoints1=points1(matchs(:,1),:);matchedPoints2=points2(matchs(:,2),:);%粗匹配角点连线figure(2)drawLinedCorner(pic1,matchedPoints1,pic2,matchedPoints2);%角点精匹配[newLoc1,newLoc2]=pointsSelect
3、(matchedPoints1,matchedPoints2);%精匹配角点连线figure(3)drawLinedCorner(pic1,newLoc1,pic2,newLoc2);%图像拼接im=picMatched(pic1,newLoc1,pic2,newLoc2);%显示拼接图像figure(4)imshow(im);set(gcf,'Color','w');functionpoints=myHarris(pic)%功能:寻找Harris角点%输入:RGB图像或gray图%输出:角点所在的行、纵的N×2矩阵i
4、flength(size(pic))==3pic=rgb2gray(pic);endpic=double(pic);hx=[-101];Word资料.Ix=filter2(hx,pic);hy=[-1;0;1];Iy=filter2(hy,pic);Ix2=Ix.*Ix;Iy2=Iy.*Iy;Ixy=Ix.*Iy;h=fspecial('gaussian',[77],2);Ix2=filter2(h,Ix2);Iy2=filter2(h,Iy2);Ixy=filter2(h,Ixy);[heigth,width]=si
5、ze(pic);alpha=0.06;R=zeros(heigth,width);fori=1:heigthforj=1:widthM=[Ix2(i,j)Ixy(i,j);Ixy(i,j)Iy2(i,j)];R(i,j)=det(M)-alpha*(trace(M)^2);endendRmax=max(max(R));pMap=zeros(heigth,width);fori=2:heigth-1forj=2:width-1ifR(i,j)>0.01*Rmaxtm=R(i-1:i+1,j-1:j+1);tm(2,2)=
6、0;ifR(i,j)>tmpMap(i,j)=1;endendendend[row,col]=find(pMap==1);points=[row,col];functiondrawHarrisCorner(pic1,points1,pic2,points2)%功能:画出Harris角点的连接%输入:%pic1、pic2:待拼接的图像%points1、points2:Harris角点位置X1=points1(:,2);Y1=points1(:,1);X2=points2(:,2);Y2=points2(:,1);dif=
7、size(pic1,2);Word资料.imshowpair(pic1,pic2,'montage');holdonplot(X1,Y1,'b*');plot(X2+dif,Y2,'b*');set(gcf,'Color','w');functiondes=myHarrisCornerDescription(pic,points)%功能:Harris角点特征描述%输入:%pic:原图像%points:角点位置%输出:%des:8×N的角点特征描述矩阵iflength(size(pic))==3pic=rgb2gray(
8、pic);endlen=length(points);des=zeros(8,len);fork=1:lenp=points(k,:);pc=pic(p(1),p(2));des(1,k)=pic(p(1)-1,p(2)-1)-pc;des(2,k)=pic(p(1),p(2)-1)-pc;des(3,k)=pic(p(1)+1,