基于Harris的图像拼接技术MATLAB源代码Word文件下载.docx
《基于Harris的图像拼接技术MATLAB源代码Word文件下载.docx》由会员分享,可在线阅读,更多相关《基于Harris的图像拼接技术MATLAB源代码Word文件下载.docx(19页珍藏版)》请在冰豆网上搜索。
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矩阵
iflength(size(pic))==3
pic=rgb2gray(pic);
end
pic=double(pic);
hx=[-101];
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]=size(pic);
alpha=0.06;
R=zeros(heigth,width);
fori=1:
heigth
forj=1:
width
M=[Ix2(i,j)Ixy(i,j);
Ixy(i,j)Iy2(i,j)];
R(i,j)=det(M)-alpha*(trace(M)^2);
end
Rmax=max(max(R));
pMap=zeros(heigth,width);
fori=2:
heigth-1
forj=2:
width-1
ifR(i,j)>
0.01*Rmax
tm=R(i-1:
i+1,j-1:
j+1);
tm(2,2)=0;
tm
pMap(i,j)=1;
[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(:
Y2=points2(:
dif=size(pic1,2);
imshowpair(pic1,pic2,'
montage'
holdon
plot(X1,Y1,'
b*'
plot(X2+dif,Y2,'
functiondes=myHarrisCornerDescription(pic,points)
Harris角点特征描述
%pic:
原图像
%points:
角点位置
%des:
8×
N的角点特征描述矩阵
len=length(points);
des=zeros(8,len);
fork=1:
len
p=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,p
(2)-1)-pc;
des(4,k)=pic(p
(1)+1,p
(2))-pc;
des(5,k)=pic(p
(1)+1,p
(2)+1)-pc;
des(6,k)=pic(p
(1),p
(2)+1)-pc;
des(7,k)=pic(p
(1)-1,p
(2)+1)-pc;
des(8,k)=pic(p
(1)-1,p
(2))-pc;
des(:
k)=des(:
k)/sum(des(:
k));
functionmatchs=myMatch(des1,des2)
特征点双向匹配
%des1、des2:
特征点描述信息构成的矩阵
%matchs:
匹配的特征点对应关系
len1=length(des1);
len2=length(des2);
match1=zeros(len1,2);
cor1=zeros(1,len2);
len1
d1=des1(:
i);
len2
d2=des2(:
j);
cor1(j)=(d1'
*d2)/sqrt((d1'
*d1)*(d2'
*d2));
[~,indx]=max(cor1);
match1(i,:
)=[i,indx];
match2=zeros(len2,2);
cor2=zeros(1,len1);
cor2(j)=(d1'
[~,indx]=max(cor2);
match2(i,:
)=[indx,i];
matchs=[];
length(match1)
length(match2)
ifmatch1(i,:
)==match2(j,:
)
matchs=[matchs;
match1(i,:
)];
functiondrawLinedCorner(pic1,loc1,pic2,loc2)
画出匹配角点的连接
%loc1、loc2:
匹配角点位置
X1=loc1(:
Y1=loc1(:
X2=loc2(:
Y2=loc2(:
length(X1)
plot(X1(k),Y1(k),'
plot(X2(k)+dif,Y2(k),'
line([X1(k),X2(k)+dif],[Y1(k),Y2(k)],'
r'
function[newLoc1,newLoc2]=pointsSelect(loc1,loc2)
筛选匹配特征点对,获取高精度的控制点
粗匹配特征点位置
%newLoc1、newLoc2:
精匹配控制点位置
slope=(loc2(:
1)-loc1(:
1))./(loc2(:
2)-loc1(:
2));
3
slope=slope-mean(slope);
len=length(slope);
t=sort(abs(slope));
thresh=t(round(0.5*len));
ind=abs(slope)<
=thresh;
slope=slope(ind);
loc1=loc1(ind,:
loc2=loc2(ind,:
newLoc1=loc1;
newLoc2=loc2;
functionim=picMatched(pic1,newLoc1,pic2,newLoc2)
获取拼接之后的图片
待拼接图片
变换控制点矩阵
%im:
拼接后的图片
iflength(size(pic1))==2
pic1=cat(3,pic1,pic1,pic1);
iflength(size(pic2))==2
pic2=cat(3,pic2,pic2,pic2);
SZ=2000;
X1=newLoc1(:
Y1=newLoc1(:
X2=newLoc2(:
Y2=newLoc2(:
sel=randperm(length(newLoc1),3);
x=X2(sel)'
;
y=Y2(sel)'
X=X1(sel)'
Y=Y1(sel)'
U=[x;
y;
ones(1,3)];
V=[X;
Y;
T=V/U;
cntrX=SZ/2;
cntrY=SZ/2;
im=zeros(SZ,SZ,3);
size(pic2,1)
size(pic2,2)
tmp=T*[j;
i;
nx=round(tmp
(1))+cntrX;
ny=round(tmp
(2))+cntrY;
ifnx>
=1&
&
nx<
=SZ&
ny>
ny<
=SZ
im(ny,nx,:
)=pic2(i,j,:
im=imresize(im,1,'
bicubic'
tpic1=zeros(SZ,SZ,3);
tpic1(1+cntrY:
size(pic1,1)+cntrY,1+cntrX:
size(pic1,2)+cntrX,:
)=pic1;
re=rgb2gray(uint8(im))-rgb2gray(uint8(tpic1));
ta=im(:
:
k);
tb=tpic1(:
ta(re==0)=tb(re==0);
im(:
k)=ta;
cleartatbretpic1
im=getPicture(im,SZ);
im=uint8(im);
im=rgb2gray(im);
functionim=getPicture(pic,SZ)
获取图像有用区域
拼接图像
%SZ:
预定图像尺寸
有用区域图像
iflength(size(pic))==2
pic=cat(3,pic,pic,pic);
k=1;
whilek<
SZ
ifany(any(pic(k,:
)))
break
k=k+1;
ceil=k;
%上边界
k=SZ;
whilek>
k=k-1;
bottom=k;
%下边界
ifany(any(pic(:
k,:
left=k;
%左边界
right=k;
%右边界
%%获取图像
im=pic(ceil:
bottom,left:
right,:
运行结果:
A组实验图
拼接图像A组
检测出的Harris角点
粗匹配的角点对连线
去除无匹配之后的角点对连线
拼接效果图
B组实验图
拼接图像B组