The theme of this week is edge detection. First I experiment with edge detection in noisy images, then I detail a direction selective edge detector.
Activity 1: Edge detection in noisy images
Here I add Gaussian noise to a lena image and study the effect of noise on edge detection. As we know any edge detection uses some kind of high pass filtering at its core. Therefore if we try to detect edges of a noisy image, the high frequency noise component gets amplified and edge detection algorithms give poor performance, due to detection of spurious edges. A simple way to correct that would be to apply a low pass filter before we feed the image to the edge detection algorithm.
In the images below we see the original image, its spectrum, and its edges. Then if we compare it to the noisy image, we see that the spectrum looks more "whitened" that is in the original image the high frequency components (towards the corners) were darker, but in the noisy image the corners look brighter, due to the introduction of high frequency noise. Also the edges detected in the noisy image look worse than the original edges. Note that all spectrums displayed are centered and show as log of magnitude.
Original image
Spectrum of original image
Edges detected on original
Noisy image
Spectrum of noisy image
Edges detected on noisy image
Next I used a Gaussian low pass filter of size 5 and variance 1 and then I passed the processed image to a Canny edge detector. The results are below. We can clearly see that the high frequency portions of the spectrum look darkened after the low pass filtering operation. Also the edges detected are much less noisy and comparable to the original edges detected.
LPF image
Spctrum of LPF image
Edges detected on LPF image
Finally I tried median filtering with window size 5 (same as the Gaussian filter). Median fitering is good for removing salt and pepper type noise, but not Gaussian noise. In the images below we see that it removes too much details and the edges detected after median filtering is even less than the edges detected in the original image
Here is the MATLAB code for this
function filtering_edgedetect()
close all;
img = rgb2gray(imread('lena.jpg'));
figure; imshow(img); dispfft(img);
noisylena = imnoise(img, 'gaussian'); %add gaussian noise to the image
figure; imshow(noisylena); dispfft(noisylena);
im0 = edge(img, 'canny');
figure; imshow(im0); %edge detection on original image
im1 = edge(noisylena, 'canny'); %perform edge detection on noisy image
figure; imshow(im1);
filter2 = fspecial('gaussian', 5, 1);
im2 = imfilter(noisylena, filter2); %perform LPF
figure; imshow(im2); dispfft(im2);
im3 = edge(im2, 'canny'); %perform edge detection on LPF image
figure; imshow(im3);
im4 = medfilt2(noisylena, [5 5]);
figure; imshow(im4); dispfft(im4);
im5 = edge(im4, 'canny'); %perform edge detection on median filtered image
figure; imshow(im5);
end
%function to display fft spectrum
function dispfft(h)
temp = log(abs(fftshift(fft2(h))));
figure; imshow(temp/max(max(temp)));
end
Activity 2: Direction selective edge detection
In this section I will attempt to develop a rough direction sensitive edge detection code in MATLAB. The basic idea is first I use Prewitt operators to get horizontal and vertical edges, then estimate the gradient at each point. Then if the direction of gradient matches the desired direction in which we are looking for edges and if magnitude of gradient is above a certain threshold, we select that point as a valid edge along the desired direction.
The table below summarizes the Prewitt filter.
Prewitt operator in x and y directions
Prewitt operator as a separable filter
Magnitude of gradient
Direction of gradient
function directionaledge()
close all;
img = rgb2gray(imread('stripes1.jpg'));
figure; imshow(img);
filt1 = [1 1 1 ; 0 0 0 ; -1 -1 -1]; %horizontal edges
filt2 = filt1'; %vertical edges
%apply the vertical and horizontal filters using convolution
im1 = conv2(double(img), double(filt1));
figure; imshow(normalize(im1));
im2 = conv2(double(img), double(filt2));
figure; imshow(normalize(im2));
%calculate magnitude and direction of the estimated gradient
sz = size(img); mag = zeros(sz); angle = zeros(sz);
for i = 1:sz(1)
for j = 1:sz(2)
mag(i,j) = sqrt(double(im1(i,j)^2 + im2(i,j)^2));
angle(i,j) = atan(double(im2(i,j)/im1(i,j)));
end
end
figure; imshow(normalize(mag));
ang = pi/4 ; %set desired angle here
angleedge = zeros(sz);
mag = normalize(mag);
for i = 1:sz(1)
for j = 1:sz(2)
%if the magnitude is strong enough and if the direction is correct
if (mag(i,j) > 0.2 && abs(angle(i,j) - ang) < 0.15)
angleedge(i,j) = mag(i,j);
end
end
end
figure; imshow(normalize(angleedge));
end
function normimg = normalize(img)
normimg = img - min(min(img)); %gets rid of negative values
normimg = normimg/max(max(normimg)); %scales in the range 0 to 1
end
Let us first examine this image and see the direction selective edge detection in action
In the above image there are 4 images that are passed through direction sensitive edge filters (0o, 90o, 45o, -45o and 20o).
Figure 1
The original image has stripes at about 20o. The horizontal Prewitt operator gives larger values than the vertical Prewitt operator because the angle is close to the horizontal. The magnitude clearly shows the diagonal lines. Now consider the direction selective edge detector results. The 0o detector shows no edge, except at the top edge (which is circled). The 90o detector likewise shows no edges except an artifact at the left edge. The 45o detector shows a faint edge (marked in green) as 45 is quite close to the 20o edges present in the image. The -45o detector is almost wholly empty. But the 20o detector shows the strongest edges, like the one present in the image.
Figure 2
In this geometric image the edges in the different directions are detected by appropriate filters. Notice for the 45o detector, some edges (marked in red) are brighter than others (marked in green). Thats because in the original image, some white 45o lines are embedded in black background (hence they give a stronger and brighter edge), while others are embedded in grey background (hence they give a weaker and darker edge). Thats because the white line has more difference from a black background than from a grey one. Note that the -45o detector returns edges of same strength as all the lines in that direction are embedded in the same gray shade.
Figure 3
Another geometric figure showing correct edges detected by appropriate filters.
Figure 4
There are strong horizontal stripes in the upper half of the dress, which is well captured by the 0o filter, while the 45o stripes in the lower part are captured selectively by the 45o filter. A few vertical edges in the border of the woman are captured by the 90o filter.