Implementation of "Harris Corner Detector"(By Me) - 2007/10/23 20:02
Recently, I implemented Harris corner detector in Matlab. It's so simple, so I will extend it into multiscales, according to schmid's paper. The code is listed as follows:
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% Step one, detect the points of interest using Harris corner dector.
% Later, I will use Multisalce-Harris corner detector to get such property of
% invariace of scale.
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
function poiMark = StyxDetectFeature(filename)
imgOrigin = rgb2gray(imread(filename));
[height, width] = size(imgOrigin);
imgTmp = double(imgOrigin)/255;
% point-of-interest marking matrix, 1 for interesting points
poiMark = zeros([height, width]);
K = 0.06;
% Integration Gaussion Window
itgGaus = [2, 4, 5, 4, 2;...
4, 9, 12, 9, 4;...
5, 12, 15, 12, 5;...
4, 9, 12, 9, 4;...
2, 4, 5, 4, 2] ./ 115;
IxIx = zeros([height, width]);
IxIy = zeros([height, width]);
IyIy = zeros([height, width]);
for i = 1 : height - 1
for j = 1 : width - 1
IxIx(i, j) = (imgTmp(i, j) - imgTmp(i+1, j))^2;
IxIy(i, j) = (imgTmp(i, j) - imgTmp(i+1, j))*(imgTmp(i, j)-imgTmp(i, j+1));
IyIy(i, j) = (imgTmp(i, j) - imgTmp(i, j+1))^2;
end
end
IxIx(:,end) = IxIx(:, width-1);
IxIx(end, :) = IxIx(height-1, :);
IxIy(:,end) = IxIy(:, width-1);
IxIy(end, :) = IxIy(height-1, :);
IyIy(:,end) = IyIy(:, width-1);
IyIy(end, :) = IyIy(height-1, :);
M = size([2, 2]);
R = zeros([height, width]);
% plot the result
% figure(2);
% subplot(2, 1, 1);
% imshow(imgOrigin);
% subplot(2, 1, 2);
% imshow(imgOrigin);
% hold on
% select the candidate points
for i = 3 : height-2
for j = 3 : width-2
M(1,1) = sum(sum(IxIx(i-2:i+2, j-2:j+2).*itgGaus));
M(1,2) = sum(sum(IxIy(i-2:i+2, j-2:j+2).*itgGaus));
M(2,1) = M(1,2);
M(2,2) = sum(sum(IyIy(i-2:i+2, j-2:j+2).*itgGaus));
% get the eigenvalue of the Hessian Matrix
R(i, j) = det(M) - K * (M(1,1) + M(2,2))^2;
if R(i, j) > 0.0005
poiMark(i, j) = 0.1;
%plot(i, j, 'or');
end
end
end
% select the 8-way local maximum
for i = 2 : height-1
for j = 2 : width - 1
if poiMark(i, j) == 0.1 && R(i, j) == max(max(R(i-1:i+1, j-1:j+1)));
poiMark(i, j) = 1;
end
end
end
% hold off
% figure(1);
% subplot(1,2,1);
% imshow(imgOrigin);
% subplot(1,2,2);
% imshow(poiMark);