lakshmen
lakshmen

Reputation: 29064

Laplacian of Gaussian (LOG) edge detector in MATLAB

I intend to peform Laplacian of Gaussian edge operator in matlab..

This is the knowledge i have

LOG operators are second-order deriatives operator. Second order deriatives operator result in zero-crossing. At the step, position where 1st deriative is maximum is where the second deriative has zero crossing.

The mask i used is mask = [0 1 0; 1 -4 1; 0 1 0];

The original image is

enter image description here

The output i get is from the original image

enter image description here

My question is why does the edges in the image appear white instead of black(=0). Should it be black? Am i right or wrong? can anyone explain?

Convolution function:

function [ I2 ] = image_convolution(I,w,G)
m= (w-1)/2;
N= size(I,1);
M=size(I,2);
for i=1:N
    for j=1:M
        if (i > N-m-1 || j > M-m-1 || i<m+1 || j <m+1)
            I2(i,j) = 0;
            continue;
        end
        sum1 = 0;
        for u=1:w
            for v=1:w
                sum1 = sum1+I(i+u-m-1,j+v-m-1)*G(u,v);
            end
        end
        I2(i,j)=sum1;
    end
end

end

Upvotes: 1

Views: 25787

Answers (3)

meJayu
meJayu

Reputation: 85

Just a simple solution: Use

imshow(image,[])

Instead of

imshow(image)

Upvotes: 0

Castilho
Castilho

Reputation: 3187

A simple test can answer all your questions:

log_mask = [0 1 0; 1 -4 1; 0 1 0];

vertical_bar = zeros(11);
vertical_bar(:,5) = 1;
bar_filtered = image_convolution(vertical_bar, 3, log_mask)

box = zeros(11);
box(3:7,3:7) = 1;
box_filtered = image_convolution(box, 3, log_mask)

figure;
subplot(2,2,1); imshow(vertical_bar,[]); title('Vertical Bar');
subplot(2,2,2); imshow(bar_filtered,[]);title('Vertical Bar LoG Filtered');
subplot(2,2,3); imshow(box,[]);title('Box');
subplot(2,2,4); imshow(box_filtered,[]);title('Box LoG Filtered');


# Output:
#
# bar_filtered =
# 0     0     0     0     0     0     0     0     0     0     0
# 0     0     0     1    -2     1     0     0     0     0     0
# 0     0     0     1    -2     1     0     0     0     0     0
# 0     0     0     1    -2     1     0     0     0     0     0
# 0     0     0     1    -2     1     0     0     0     0     0
# 0     0     0     1    -2     1     0     0     0     0     0
# 0     0     0     1    -2     1     0     0     0     0     0
# 0     0     0     1    -2     1     0     0     0     0     0
# 0     0     0     1    -2     1     0     0     0     0     0
# 0     0     0     0     0     0     0     0     0     0     0
# 0     0     0     0     0     0     0     0     0     0     0

#box_filtered =

# 0     0     0     0     0     0     0     0     0     0     0
# 0     0     1     1     1     1     1     0     0     0     0
# 0     1    -2    -1    -1    -1    -2     1     0     0     0
# 0     1    -1     0     0     0    -1     1     0     0     0
# 0     1    -1     0     0     0    -1     1     0     0     0
# 0     1    -1     0     0     0    -1     1     0     0     0
# 0     1    -2    -1    -1    -1    -2     1     0     0     0
# 0     0     1     1     1     1     1     0     0     0     0
# 0     0     0     0     0     0     0     0     0     0     0
# 0     0     0     0     0     0     0     0     0     0     0
# 0     0     0     0     0     0     0     0     0     0     0

The filtering results displayed graphically: enter image description here

See? The pixels exactly on the border have indeed negative values, as you expected. The pixels right next to the border, on the other hand, have positive values! Bigger values than the ones on the region where the signal is constant. These are the "white" values you see on your result.

Mathematically, that's also easy to explain. Take a look at the mask you used

LoG mask used

I've plotted it so it's easier to see the little peaks around the massive valley. Simply speaking, they make the filtered values around the borders have a greater magnitude than the rest of the pixels, thus having this effect of "border recognition".

I've plotted the mask created with the matlab function fspecial('log'). In this maks, the peaks are even easier to spot. enter image description here

Best regards

Upvotes: 9

jpimentel
jpimentel

Reputation: 704

This has to do with the way the convolution is computed. When your kernel (your mask) is convolved in the borders, the kernel reaches an area outside the original image. There are some options about what to do there:

  • you can assume the values in that outside region to be 0,
  • to have the same values as the border,
  • or to be the same as the opposite side of the image, as if the image was periodical (circular).

When the area outside the image is assumed to be zero and the border values are high (such as in your image), there will be an edge detected, since you are stepping from a high value to zero.

If you use imfilter, the function by default assumes this region to be 0. You can use the option 'replicate' (so the outside region is the same as the border), and it should solve this problem.

You can read more about it in the official docs: http://www.mathworks.com/help/toolbox/images/ref/imfilter.html

Also, your resulting image has a different size, because the outside region is included in the result. If you use imfilter, this region is cropped by default.

I assume you are using the conv2 function, which has both these problems by default.

PS: I haven't used this in some time. Let me know if imfilter works exactly like I said or you needed anything else.

Upvotes: 0

Related Questions