N4LN
N4LN

Reputation: 95

Harris Corner Detection

I'm doing a program to detect corners using Harris method. The output of the method shows the corners. What I want to do is to apply thresholding to the image. This is my code:

 % Create a mask for deteting the vertical edges 
 verticalMask = [-1 0 1;
            -2 0 2;
            -1 0 1]* 0.25;

% Create a mask for deteting the horizontal edges 
horisontalMask = [-1 -2 -1;
              0 0 0;
              1 2 1]* 0.25;

% Create a mask for Gaussian filter(is used to improve the result)

gaussianFilter= [1 4 1;
             4 7 4;
             1 4 1].*(1/27);


K = 0.04; % The sensitivity factor used in the Harris detection algorithm (Used to detect
            sharp corners).



% Get the gradient of the image [Ix,Iy], using the convulation function
Ix = conv2(grayImage,verticalMask);
Iy = conv2(grayImage,horisontalMask);


% get the input arguments of the harris formula

Ix2 = Ix.* Ix; % get Ix to the power of two
Iy2 = Iy.* Iy; % get Iy to the power of two
Ixy = Ix .* Iy; %get the Ixy by multiply Ix and Iy

% Apply the gaussian filter to the the arguments
Ix2 = conv2(Ix2,gaussianFilter);
Iy2 = conv2(Iy2,gaussianFilter);
Ixy = conv2(Ixy,gaussianFilter);

% Enetr the arguments into the formula
C = (Ix2 .* Iy2) - (Ixy.^2) - K * ( Ix2 + Iy2 ).^ 2;

Now, I want to apply the thresholding to C which is the output of the formula. I found a code that I tried, It works perfectly, but I want to understand it first if someone can please explain.(For the thresh and radius variables, I changed their values so It can work with my image).

thresh = 0.000999;
radius = 1;
sze = 2*radius + 1;                   % Size of dilation mask
mx = ordfilt2(cim, sze^2, ones(sze));      % Grey-scale dilate

% Make mask to exclude points on borders
bordermask = zeros(size(cim));
bordermask(radius+1:end-radius, radius+1:end-radius) = 1;

% Find maxima, threshold, and apply bordermask
cimmx = (cim==mx) & (cim>thresh) & bordermask;
[r, c] = find(cimmx);     % Return coordinates of corners

figure, imshow(im),
hold on;
plot(c, r, '+');
hold off;

Upvotes: 3

Views: 8333

Answers (1)

vipers36
vipers36

Reputation: 300

first,each pixel of the image cim is replaced with the value of its largest neighbor. The neighbor hood is define as a square of the size sze. This dilates the image, i.e. bright image regions get thicker. see matlab doc

 mx = ordfilt2(cim, sze^2, ones(sze));      % Grey-scale dilate

cim==mx means that you only accept pixels that are the same in the original and the dialted image. This includes only pixels that are maxima in their neigborhood of size sze.

cim>thresh means that you only take pixels into account that have a value greater than thresh. Hence, all darker pixels cannot be edges.

The border mask makes sure you accept only pixels that have a distance greater than radius to the border of the image.

[r, c] = find(cimmx) gives you row and column of the corner pixels.

Upvotes: 2

Related Questions