Edge detection in images is achieved by passing it through a high pass filter. Many edge operators like Roberts, Prewitt and Sobel have been proposed. In the spatial domain, edge detection is just a convolution, while in frequency domain, it involves multiplying the spectrums of the image and the edge operator. Here we consider the Prewitt operator.
-1 0 1
-1 0 1
-1 0 1
x direction edges
-1 -1 -1
0 0 0
1 1 1
y direction edges
Prewitt edge operators
In matlab, we can apply the edge operators using the imfilter
or conv2
functions. Once edges in the X and Y direction are calculated, we can calculate the magnitude and angle of the edges as follows:
Iy = imfilter(double(img), [-1 -1 -1; 0 0 0; 1 1 1]);
Ix = imfilter(double(img), [-1 0 1; -1 0 1; -1 0 1]);
mag = sqrt(double(Ix.*Ix + Iy.*Iy));
ang = atan2(double(Ix), double(Iy));
Note that since Ix
, Iy
and mag
are not real images, we must scale it between 0 and 1, before displaying. Hence the following function is used.
function normdisplay(img)
figure; i = img-min(img(:)); imshow(i/max(i(:)));
end
Ix
Iy
mag
Now to find directional edges, we scan the ang
matrix and retain only those edges which fall within a certain band.
function angleedges = directionaledge(mag, ang, th1, th2)
angleedges = zeros(size(ang));
for i = 1:size(ang,1)
for j = 1:size(ang,2)
if ang(i,j)>=th1 && ang(i,j)<=th2
angleedges(i,j) = mag(i,j);
end
end
end
end
angleedges = directionaledge(mag, ang, 0.75, 0.8); %find edges that are around 45 degrees
The following images show some directional edges in the image.
0 degree
45 degree
90 degree
Corners are interest points in the images, because they usually occur at very discernible, unique parts of the image. In plain patches of images, if we move a small window around, then there is no change at all in any direction. If we are near an edge, then there is one direction along which there is no change (direction of the edge), but a large change in the direction perpendicular to it. Corners are however such points, which have large changes in any direction we move. This criteria can be used to identify corners.
Characteristics of flat regions, edges and corners
The Harris Corner detector is summarized by the algorithm below:
Summary of Harris corner detector
Iy = imfilter(double(img), [-1 -1 -1; 0 0 0; 1 1 1]);
Ix = imfilter(double(img), [-1 0 1; -1 0 1; -1 0 1]);
k=0.05;
g = fspecial('gaussian',3, 1);
Ix2 = imfilter(Ix.^2, g);
Iy2 = imfilter(Iy.^2, g);
Ixy = imfilter(Ix.*Iy, g);
R = (Ix2.*Iy2 - Ixy.^2) - k*(Ix2 + Iy2).^2;
Corners detected by Harris corner detector for threshold R>100000000