Skip to content

Instantly share code, notes, and snippets.

@inkwisit
Created June 24, 2016 19:14
Show Gist options
  • Save inkwisit/f9ffa4184f088bed4be6d66b4c6d1d06 to your computer and use it in GitHub Desktop.
Save inkwisit/f9ffa4184f088bed4be6d66b4c6d1d06 to your computer and use it in GitHub Desktop.
Computer vision basics (Harris Corner detector)
I = imread('check2.jpg');
%it is already in binary format
%I = rgb2gray(I);
I = I.*255;
%default values are 0,1 in binary so to show the image all 1's are
%multipled by 255
gaussian = fspecial('gaussian',3,1);
%derivative in x direction
dx = [1 0 -1;1 0 -1;1 0 -1];
%derivative in y direction
dy = dx';
%I = conv2(double(I),gaussian,'same');
%no noise in check2.jpg so no smoothing is done
Ix = conv2(I,double(dx),'same');
Iy = conv2(I,double(dy),'same');
gaussian_2 = fspecial('gaussian',5,1);
imshow(uint8(I));
%filter2 does the correlation and conv2 does the convolution , here all the
%filter are circular symmetric so correlation is same as convolution
Ix2 = conv2(Ix.^2,gaussian_2,'same');
Iy2 = conv2(Iy.^2,gaussian_2,'same');
Ixy = conv2(Ix.*Iy,gaussian_2,'same');
detM = Ix2.*Iy2 - Ixy.^2;
% here the detM is the image sized matrix having each pixel storing the
% deteminant of the corresponding the pixel in Ix2, Iy2 and Ixy
traceM = Ix2 + Iy2;
R = detM - 0.04*traceM.^2;
% the above method is just empirical method of determing the corners and
% edges because determining the eigenvalues separately is computationally
% heavy task
[height width] = size(R);
corners = zeros(height,width);
%finding the local maxima by traversing in each pixel
for i = 2:height-1
for j = 2:width-1
if R(i,j)>0.0 && R(i,j)>R(i-1,j-1)&& R(i,j)>R(i+1,j+1)&&R(i,j)>R(i,j+1)&&R(i,j)>R(i,j-1)&&R(i,j)>R(i+1,j)&&R(i,j)>R(i-1,j)&&R(i,j)>R(i+1,j-1)&&R(i,j)>R(i-1,j+1)
corners(i,j) = 1;
end
end
end
[x y] = find(corners == 1);
hold on;
plot(y,x,'r.');
%figure,surf(double(corners));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment