I_rgb=imread('Image_2.
jpg');
I=rgb2gray(I_rgb);
% Using Prewitt approximation for image gradients Ix, Iy
dx=[-1 0 1;-1 0 1; -1 0 1];
dy=[1 1 1;0 0 0; -1 -1 -1];
% Image Gradients of each pixel
Ix=conv2(double(I),dx,'same');
Iy=conv2(double(I),dy,'same');
% 6x6 Gaussian Filter for Harris
sigma=1;
G=fspecial('gaussian', max(1,fix(6*sigma)),sigma);
% Structure Tensor elements
Ix2=conv2(Ix.*Ix,G,'same');
Ixy=conv2(Ix.*Iy,G,'same');
Iy2=conv2(Iy.*Iy,G,'same');
% Threshold for decision
threshold=0.36;
% Trace of Structure Tensor Matrix
trace=Ix2+Iy2;
% Determinant of Structure Tensor Matrix
det=((Ix2.*Iy2)-(Ixy.^2));
% Eigen values of Structure Tensor Matrix
Lambda0=abs(0.5*((trace)+sqrt((trace.^2)-(4*det))));
Lambda1=abs(0.5*((trace)-sqrt((trace.^2)-(4*det))));
% Harris corner detector
R1=det-(0.04*(trace.^2));
minr = min(min(R1));
maxr = max(max(R1));
R1 = (R1 - minr) / (maxr - minr);%scaling to 0-1 range
maxima1 = ordfilt2(R1, 25, ones(5));%finding maxima
mask1 = (R1 == maxima1) & (R1 > threshold);%applying threshold
maxima1 = mask1.*R1;%extracting the intensities of corner points
[r1,c1]=find(maxima1);%finding the indices of corner points
subplot(221)
imshow(I)
hold on
plot(c1,r1,'r+')
title('Harris')
% Triggs corner detector
R2=Lambda0-(0.05*Lambda1);
minr = min(min(R2));
maxr = max(max(R2));
R2 = (R2 - minr) / (maxr - minr);%scaling to 0-1 range
maxima2 = ordfilt2(R2, 25, ones(5));%finding maxima
mask2 = (R2 == maxima2) & (R2 > threshold);%applying threshold
maxima2 = mask2.*R2;%extracting the intensities of corner points
[r2,c2]=find(maxima2);%finding the indices of corner points
subplot(222)
imshow(I)
hold on
plot(c2,r2,'r+')
title('Triggs')
%Szeliski corner detector
R3 = det ./ trace;
minr = min(min(R3));
maxr = max(max(R3));
R3 = (R3 - minr) / (maxr - minr);%scaling to 0-1 range
maxima3 = ordfilt2(R3, 25, ones(5));%finding maxima
mask3 = (R3 == maxima3) & (R3 > threshold);%applying threshold
maxima3 = mask3.*R3;%extracting the intensities of corner points
[r3,c3]=find(maxima3);%finding the indices of corner points
subplot(223)
imshow(I)
hold on
plot(c3,r3,'r+')
title('Szleiski')
%Shi-Tomasi corner detector
R4=min(Lambda0,Lambda1);
minr = min(min(R4));
maxr = max(max(R4));
R4 = (R4 - minr) / (maxr - minr);%scaling to 0-1 range
maxima4 = ordfilt2(R4, 25, ones(5));%finding maxima
mask4 = (R4 == maxima4) & (R4 > threshold);%applying threshold
maxima4 = mask4.*R4;%extracting the intensities of corner points
[r4,c4]=find(maxima4);%finding the indices of corner points
subplot(224)
imshow(I)
hold on
plot(c4,r4,'r+')
title('Shi-Tomasi')