matlab编程实现Harris角点检测算法

  • 格式:docx
  • 大小:16.15 KB
  • 文档页数:2

img=imread('Lena.bmp');

[m,n]=size(img);

tmp=zeros(m+2,n+2);

tmp(2:m+1,2:n+1)=img; %扩展图像边缘1个像素

Ix=zeros(m+2,n+2); Iy=zeros(m+2,n+2);

Ix(:,2:n+1)=tmp(:,3:n+2)-tmp(:,1:n); % x方向上的差分

Iy(2:m+1,:)=tmp(3:m+2,:)-tmp(1:m,:); % y方向上的差分

Ix2=Ix(2:m+1,2:n+1).^2;

Iy2=Iy(2:m+1,2:n+1).^2;

Ixy=Ix(2:m+1,2:n+1).*Iy(2:m+1,2:n+1);

h=fspecial('gaussian',[7 7],2); %生成高斯滤波器,消弱噪声的影响

Ix2=filter2(h,Ix2); %滤波

Iy2=filter2(h,Iy2);

Ixy=filter2(h,Ixy);

R=zeros(m,n);

for i=1:m

for j=1:n

M=[Ix2(i,j) Ixy(i,j);Ixy(i,j) Iy2(i,j)]; %构造Hessian矩阵

R(i,j)=det(M)-0.06*(trace(M))^2; %角点判定公式

end

end

Rmax=max(max(R));

loc=[]; %记录角点位置

tmp(2:m+1,2:n+1)=R; %扩展图像边缘1个像素

for i=2:m+1

for j=2:n+1

if tmp(i,j)>0.01*Rmax %要求矩阵R中的每个元素值大于0.01倍的最大值

sq=tmp(i-1:i+1,j-1:j+1);

sq=reshape(sq,1,9);

sq=[sq(1:4),sq(6:9)];

if tmp(i,j)>sq %局部非极大值抑制

loc=[loc;[j-1,i-1]];

end

end

end

end

%以下代码显示获取的Harris角点

X=loc(:,1);

Y=loc(:,2);

subplot(1,2,1);

imshow(img);

title('原图')

subplot(1,2,2);

imshow(img); title('角点检测')

hold on

plot(X,Y,'*','color','g');

hold off