Sunday 29 January 2017

Harris Corner Detector - MATLAB Code


Code:

A = imread(‘cameraman.jpg’);    %needs image file “cameraman.jpg”
If length(size(A))>2
A = rgb2gray(A);
end
dx = [-1 0 1; -1 0 1; -1 0 1];  %Derivative masks
Ix = filter2(dx,A);

dy = [1 1 1; 0 0 0; -1 -1 -1];
Iy = filter2(dy,A);

Ix2 = Ix.^2;                    %computing products of derivatives
Iy2 = Iy.^2;
Ixy = Ix.*Iy;
clear Ix;
clear Iy;

%applying gaussian filter on the computed value
g= fspecial('gaussian',[6 6],2);
Ix2 = filter2(g,Ix2);
Iy2 = filter2(g,Iy2);
Ixy = filter2(g,Ixy);
height = size(A,1);
width = size(A,2);
res = zeros(height,width);
R = zeros(height,width);

Rmax = 0;
for a = 1:height
for b = 1:width
C = [Ix2(a,b) Ixy(a,b);Ixy(a,b) Iy2(a,b)];
R(a,b) = det(C)-0.01*(trace(C))^2;
if R(a,b) > Rmax
Rmax = R(a,b);
end;
end;
end;
count = 0;
for a = 2:height-1
for b = 2:width-1
if R(a,b) > 0.1*Rmax && R(a,b) > R(a-1,b-1) && R(a,b) > R(a-1,b) && R(a,b) > R(a-1,b+1) && R(a,b) > R(a,b-1) && R(a,b) > R(a,b+1) && R(a,b) > R(a+1,b-1) && R(a,b) > R(a+1,b) && R(a,b) > R(a+1,b+1)
res(a,b) = 1;
count = count+1;
end;

end;
end;
[posc, posr] = find(res == 1);
count;
imshow(A);
hold on;
plot(posr,posc,'r.');



Output:


            Original Image                          Result




            Original Image                                             Result

No comments:

Post a Comment