Thursday, January 5, 2012

Gaussian Smoothing


This code takes image name and the kernel size as arguments and does Gaussian smoothing by creating a Gaussian kernel of specified size and apply the kernel to the image.


The output will be a blurred image.


Matlab Code

function gaussianSmoothing(name,w)
    h = gaussianKernel(w); % w x w Gaussian kernel    
    imrgb = imread(name);
    im = im2double(rgb2gray(imrgb));
    result = conv2(im,h,'same'); % Apply the kernel
    subplot(1 ,2 ,1)
    imshow(im);
    title('Original');
    subplot(1 ,2 ,2)
    imshow(result)
    title('Result');
end


function h=gaussianKernel(w)
    x=-floor(w/2):floor(w/2);
    y=-floor(w/2):floor(w/2);
    [X,Y]=meshgrid(x,y);        
    Z=(1/2*pi)*exp(-(X.^2+Y.^2)/2); % sigma=1
    h=Z/sum(sum(Z)); % normalize
end

Output


No comments:

Post a Comment