Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to understand implementation of gaussian blurring in matlab

I am trying to blur a scanned text document to the point that the text lines are blurred to black.. I mean the text blends into each other and all I see are black lines.

I'm new to MATLAB and even though I know the basics I cannot get the image to blur properly. I have read this: Gaussian Blurr and according to that the blur is managed/decided by the sigma function. But that is not how it works in the code I wrote.

While trying to learn Gaussian blurring in Matlab I came to find out that its achieved by using this function: fspecial('gaussian',hsize,sigma);

So apparently there are two variables hsize specifies number of rows or columns in the function while sigma is the standard deviation.

Can some one please explain the significance of hsize here and why it has a much deeper effect on the result even more than sigma?

Why is it that even if I increase sigma to a very high value the blurr is not effected but the image is distorted a lot by increasing the hsize

here is my code:

img = imread('c:\new.jpg');

h = fspecial('gaussian',hsize,sigma);

out = imfilter(img,h);

imshow(out);

and the results are attached:

Why is it not only controlled by sigma? What role does hsize play? Why cant I get it to blur the text only rather than distort the entire image?

Thank you

enter image description hereenter image description hereenter image description here

like image 315
StuckInPhDNoMore Avatar asked Jan 12 '23 17:01

StuckInPhDNoMore


1 Answers

hsize refers to the size of the filter. Specifically, a filter that is Nx x Ny pixels uses a pixel region Nx x Ny in size centered around each pixel when computing the response of the filter. The response is just how the pixels in that region are combined together. In the case of a gaussian filter, the intensity at each pixel around the central one is weighted according to a gaussian function prior to performing a box average over the region. sigma refers to the standard deviation of the gaussian (see documentation for fspecial) with units in pixels. As you increase sigma (keeping the size of the filter the same) eventually you approach a simple box average with uniform weighting over the filter area around the central pixel, so you stop seeing an effect from increasing sigma.

The similarity between results obtained with gaussian blur (with large value of sigma) and a box average are shown in the left and middle images below. The right image shows the results of eroding the image, which is probably what you want.

enter image description here

The code:

% gaussian filter:
hsize = 5;
sigma = 10;
h = fspecial('gaussian',hsize,sigma);
out = imfilter(img,h);

% box filter:
h = fspecial('average',hsize);
out = imfilter(img,h);

% erode:
se=strel('ball',4,4); 
out = imerode(img,se);
like image 138
Buck Thorn Avatar answered Jan 29 '23 18:01

Buck Thorn