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
Best Answer
hsize
refers to the size of the filter. Specifically, a filter that is Nxx Ny pixels uses a pixel region Nx x Ny in size centered around eachpixel when computing the response of the filter. The response is just how the pixels in that region are combined together. In the case of agaussian filter, the intensity at each pixel around the central one isweighted according to a gaussian function prior to performing a box average over the region.sigma
refers to the standard deviation of the gaussian (see documentationfor fspecial
) with units in pixels. As you increase sigma
(keeping thesize of the filter the same) eventually you approach a simple box average with uniform weightingover 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 boxaverage are shown in the left and middle images below. The right image showsthe results of eroding the image, which is probably what you want.
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);
Fspecial's Manual
h = fspecial('gaussian', hsize, sigma) returns a rotationallysymmetric Gaussian lowpass filter of size hsize with standarddeviation sigma (positive). hsize can be a vector specifying thenumber of rows and columns in h, or it can be a scalar, in which caseh is a square matrix. The default value for hsize is [3 3]; thedefault value for sigma is 0.5. Not recommended. Use imgaussfilt orimgaussfilt3 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.