4

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 here enter image description here enter image description here

Buck Thorn
  • 5,024
  • 2
  • 17
  • 27
StuckInPhDNoMore
  • 2,507
  • 4
  • 41
  • 73
  • 1
    Examples that don't take up so much space would be sufficient and would be appreciated. You could just crop these images and re-upload. – horchler Aug 07 '13 at 14:59
  • Also, please specify, which values of `hsize` and `sigma` you used to generate them. – Schorsch Aug 07 '13 at 14:59

2 Answers2

8

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);
Buck Thorn
  • 5,024
  • 2
  • 17
  • 27
  • Thank you. ``imerode`` is excatly what I was looking for. I used ``imerode`` with different ``strel`` such as ``square`` in place of ``ball`` but got the same result. So why did you use ``ball``? Also why is the result of ``average`` and ``gaussian`` the same? What function does ``average`` follow? Thank you – StuckInPhDNoMore Aug 09 '13 at 19:58
  • 1
    `average` performs a uniformly weighted box average. This means that the intensity at each pixel in the filtered image is computed as the average of the surrounding pixels in a box of size `hsize`. In the case of `gaussian` the pixels are not weighted uniformly before averaging but rather using a gaussian weighting function centered on the central pixel. I used a ball shape but disk or other probably work as well. Please check the documentation for the available `strel` choices. – Buck Thorn Aug 09 '13 at 20:02
1

Fspecial's Manual

h = fspecial('gaussian', hsize, sigma) returns a rotationally symmetric Gaussian lowpass filter of size hsize with standard deviation sigma (positive). hsize can be a vector specifying the number of rows and columns in h, or it can be a scalar, in which case h is a square matrix. The default value for hsize is [3 3]; the default value for sigma is 0.5. Not recommended. Use imgaussfilt or imgaussfilt3 instead.

where they say that fspecial - gaussian is not recommended. In deciding the standard deviation (sigma), you need still decide hsize which affects the blurring. In imgaussfilt, you decide the standard deviation and the system considers you the rest. I can get much more better tolerance levels with imgaussfilt and imgaussfilt3 in my systems in Matlab 2016a, example output here in the body

im = im2double( imgGray ); 
sigma = 5;
simulatedPsfImage = imgaussfilt(im, sigma); 
simulatedPsfImage = im2double( simulatedPsfImage );
[ measuredResolution, standardError, bestFitData ] = ...
    EstimateResolutionFromPsfImage( simulatedPsfImage, [1.00 1.00] );

Note that the tolerance levels of fspecial are high [0.70 1.30] by default.

Community
  • 1
  • 1
Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697