Harris角點檢測

lifeitengup發表於2013-10-01


貼一下 計算機視覺課的作業

有時候 需要用下 

function F = DetectCorner(I, threshold, sigma)
%Harris Corner Detect
%by:LiFeiteng 
%Version1.0  灰度影像  2013/5/08
%Version1.1  彩色影像  2013/5/13
%存在問題:沒有實現跟 corner 相似的介面,對噪聲也不能很好的處理<checkerboard(50,2,2)>。

if nargin < 3
    sigma = 0.01;
end
if nargin < 2
    threshold = 0.6; %實驗表明 對此引數不敏感
    sigma = 0.01; %對此引數敏感
end
if size(I,3)==3
    I = rgb2gray(I);
end
W = fspecial('gaussian', [5 5], 2);
I = double(I);
I = imfilter(I, W, 'same');

Ix = imfilter(I,[-1 0 1], 'same');
Iy = imfilter(I,[-1 0 1]', 'same');
A = Ix.^2;
B = Ix.*Iy;
C = Iy.^2;

A = imfilter(A, W, 'same');
B = imfilter(B, W, 'same');
C = imfilter(C, W, 'same');
%F = (A.*C-B.*B)./(A+C+eps); 
F = (A.*C-B.*B)-threshold*(A+C); 
F = F/max(F(:));
F = F.*(F>sigma); %F = F.*(F>0.01);
cor = 10;
F(1:cor,1:cor)=0; F(1:cor,end-cor:end)=0;F(end-cor:end,1:cor)=0;F(end-cor:end,end-cor:end)=0;
F = imregionalmax(F, 8);
[row, col] = find(F==1);
F = [col, row];
end

測試用例:

img = imread('lena_gray.tif');
img = imread('corner.jpg');
%img = checkerboard(50,2,2);
% img = rgb2gray(img);
img = imnoise(img,'gaussian');
figure(1),imshow(img, [])
F = DetectCorner(img,0.6);

figure(1),hold on, plot(F(:,1), F(:,2), 'y.')



相關文章