Gaussian filter in MATLAB - matlab

Does the 'gaussian' filter in MATLAB convolve the image with the Gaussian kernel? Also, how do you choose the parameters hsize (size of filter) and sigma? What do you base it on?

You first create the filter with fspecial and then convolve the image with the filter using imfilter (which works on multidimensional images as in the example).
You specify sigma and hsize in fspecial.
Code:
%%# Read an image
I = imread('peppers.png');
%# Create the gaussian filter with hsize = [5 5] and sigma = 2
G = fspecial('gaussian',[5 5],2);
%# Filter it
Ig = imfilter(I,G,'same');
%# Display
imshow(Ig)

#Jacob already showed you how to use the Gaussian filter in Matlab, so I won't repeat that.
I would choose filter size to be about 3*sigma in each direction (round to odd integer). Thus, the filter decays to nearly zero at the edges, and you won't get discontinuities in the filtered image.
The choice of sigma depends a lot on what you want to do. Gaussian smoothing is low-pass filtering, which means that it suppresses high-frequency detail (noise, but also edges), while preserving the low-frequency parts of the image (i.e. those that don't vary so much). In other words, the filter blurs everything that is smaller than the filter.
If you're looking to suppress noise in an image in order to enhance the detection of small features, for example, I suggest to choose a sigma that makes the Gaussian just slightly smaller than the feature.

In MATLAB R2015a or newer, it is no longer necessary (or advisable from a performance standpoint) to use fspecial followed by imfilter since there is a new function called imgaussfilt that performs this operation in one step and more efficiently.
The basic syntax:
B = imgaussfilt(A,sigma) filters image A with a 2-D Gaussian smoothing kernel with standard deviation specified by sigma.
The size of the filter for a given Gaussian standard deviation (sigam) is chosen automatically, but can also be specified manually:
B = imgaussfilt(A,sigma,'FilterSize',[3 3]);
The default is 2*ceil(2*sigma)+1.
Additional features of imgaussfilter are ability to operate on gpuArrays, filtering in frequency or spacial domain, and advanced image padding options. It looks a lot like IPP... hmmm. Plus, there's a 3D version called imgaussfilt3.

Related

deleting noise from eye retina

I have a gray image with noise. I am new in deleting noise from an image so I don't know the type of noise and how I can remove it from image. My aim is to convert image to binary mode by using local threshold after removing the noise.
Is there anybody who has any idea about type of noise and has a method to remove this noise?
The image:
Typically in microscopy noise comes from 2 sources:
1) Gaussian/electronic noise
This type of noise come from the fluctuations in the detector due to quantum effects in the electronics. It is randomly generated and follows a gaussian distribution. Therefore in that case using a gaussian filter might be optimal to remove it.
2) Shot noise
Photons arriving at the detector are converted to electric signal through the photoelectric effect, and the fluctuations in the number of photons arriving at the detector create shot noise, which you can hardly eliminate and is usually predominant during acquisition. It follows a Poisson distribution which looks like a Gaussian, so in this case a gaussian filter might be the appropriate as well.
So to come back to your question, it does look like a gaussian filter would be the most intuitive choice, although an average filter could be used as well. Here is a sample code that you could try and play around with:
clear
close all
clc
A = imread('http://i.stack.imgur.com/IlqAi.jpg');
BW = im2bw(A,.9); %//Treshold image
h = fspecial('gaussian', [5 5],.8); %// Create gaussian filter
BW2 = imfilter(BW,h); %// Apply filter
imshow(BW2); %// Display image
which results in the following:
You can change the filter parameters (i.e. the size of the kernel and the sigma value) and see how they affect the outcome. Here are other filters you can use as well:
Median:
BW2 = medfilt2(BW,[3 3]); %// Median filter
or average:
h = fspecial('average', 3) %//average filter
BW2 = imfilter(BW,h);
You might be interested in this link on the Mathworks website that talks about removing noise in images.
Hope that helps!

how to find standard deviation of noise in an image using matlab?

As a part of my project, initially find low resolution of the input image. Then as a second step i need find the noise in the low-resolution image. How to find noise in an image and its standard deviation using matlab?
You can use the std matlab function which returns the standard deviation of a matrix.
std_deviation = std(image);
This will give you the standard deviation of the whole image. However you cannot calculate the noise std since you don't have the original filtered image.
Possible solution: (Not accurate) : This suppose thaht your noise is gaussian
Well, you can render several Noise matrices and test them:
(choose your mean_vector and std_vector)
for i = 1 : length(mean_vector) % or length(std_vector)
Noise(:,:,i) = mean_vector(i) + std_vector(i).*randn(size(your_image))
% extracting the possibly filtered image
filtered_img(:,:,i) = your_image - Noise(:,:,i);
end
Then display every filtered_img and choose the one that looks the less noisy.
You can denoise the image, compute the difference between the raw image and the denoised version, and then compute the standard deviation of the difference.
For instance:
a=imread('input');
a=double(a);
b=imsharpen(a); %you may need to tune the parameters
diff=b-a;
noise=std2(diff);
You can find the variance of noise in the image assuming that you know the distribution. You will know this if you have read some of the great works in image denoising field by Donoho & John.
To find the noise std. dev. of noise in an image with Gaussian contamination (additive), you can use the Median Absolute Deviation (MAD) estimator on the derivative of the image using the following kernel:
I am writing the python code for this, you can easily write it in Matlab:
def find_stddevs(img):
k = np.asmatrix([[-1.0/9, -1.0/9 ,-1.0/9],
[-1.0/9, 8.0/9 , -1.0/9],
[-1.0/9, -1.0/9 ,-1.0/9]])
filtered = convolve(img,k,mode='reflect')
median_a = np.median(filtered)
stddev = np.median(np.absolute(filtered - median_a))/0.67449 #Gaussian noise assumption
return stddev
You can see the derivation and the logic Here on Wikipedia.
Again, most people think it can be done. This is true iff you do not have any prior about the type of contamination in the image.

Discrete Approximation to Gaussian smoothing

I am trying to find a discrete approximation to the Gaussian smoothing operation, as shown in the link :
http://bit.ly/1cSgkwt
G is some discrete smoothing kernel, a Gaussian in this case and * is the convolution operation. Basically, I want to apply a smoothing kernel to each pixel in the image. I am doing this in MATLAB and using the following code to create the matrix G, which is naive and hence painfully slow :
z = rgb2gray(imread('train_02463_1.bmp'));
im_sz = size(z);
ksize = 5;
% Gaussian kernel of size ksize*ksize
gw_mat = g_sigma(1,2*ksize+1)'*g_sigma(1,2*ksize+1);
G = sparse(length(ksize+1:im_sz(1)-ksize),prod(im_sz));
for i = ksize+1:im_sz(1)-ksize
for j = ksize+1:im_sz(2)-ksize
[x,y] = meshgrid(i-ksize:i+ksize,j-ksize:j+ksize);
row_num = sub2ind(im_sz,i,j);
colnums = sub2ind(im_sz,x,y);
G(row_num,colnums(:)) = gw_mat(:)';
end
end
Is there a more efficient way to do this ?
EDIT: I should apologize for not complete specifying the problem. Most of the answers below are valid ones, but the issue here is the above approximation is part of a optimization objective where the variable is z. The whole problem looks something like this :
http://goo.gl/rEE02y
Thus, I have to pre-generate a matrix G which approximates a smoothing function in order to feed this objective to a solver. I am using cvx, if that helps.
Typically people do it by applying two 1D Gaussian functions sequentially instead of one 2D function (idea of separable filters). You can approximate 1D horizontal Gaussian pretty fast, apply it to each pixel, save result in temp, and then apply a vertical Gaussian to temp. The expected speed up is quite significant: O(ksize*ksize) -> O(ksize+ksize)
You can use approximate Gaussian smoothing via box filters, e.g see integgaussfilt.m:
This function approximates Gaussian filtering by repeatedly applying
averaging filters. The averaging is performed via integral images which
results in a fixed and very low computational cost that is independent of
the Gaussian size.
The fastest way to apply Spatially Invariant Filter on an image using MATLAB would be using imfilter.
imfilter will automatically notice "Seperable" Kernel and will apply it in 2 steps (Vertically / Horizontally).
For creating some known and useful kernels you may look at fspecial.

Smoothing of histogram with a low-pass filter in MATLAB

I have an image and my aim is to binarize the image. I have filtered the image with a low pass Gaussian filter and have computed the intensity histogram of the image.
I now want to perform smoothing of the histogram so that I can obtain the threshold for binarization. I used a low pass filter but it did not work. This is the filter I used.
h = fspecial('gaussian', [8 8],2);
Can anyone help me with this? What is the process with respect to smoothing of a histogram?
imhist(Ig);
Thanks a lot for all your help.
I've been working on a very similar problem recently, trying to compute a threshold in order to exclude noisy background pixels from MRI data prior to performing other computations on the images. What I did was fit a spline to the histogram to smooth it while maintaining an accurate fit of the shape. I used the splinefit package from the file exchange to perform the fitting. I computed a histogram for a stack of images treated together, but it should work similarly for an individual image. I also happened to use a logarithmic transformation of my histogram data, but that may or may not be a useful step for your application.
[my_histogram, xvals] = hist(reshape(image_volume), 1, []), number_of_bins);
my_log_hist = log(my_histogram);
my_log_hist(~isfinite(my_log_hist)) = 0; % Get rid of NaN values that arise from empty bins (log of zero = NaN)
figure(1), plot(xvals, my_log_hist, 'b');
hold on
breaks = linspace(0, max_pixel_intensity, numberofbreaks);
xx = linspace(0, max_pixel_intensity, max_pixel_intensity+1);
pp = splinefit(xvals, my_log_hist, breaks, 'r');
plot(xx, ppval(pp, xx), 'r');
Note that the spline is differentiable and you can use ppdiff to get the derivative, which is useful for finding maxima and minima to help pick an appropriate threshold. The numberofbreaks is set to a relatively low number so that the spline will smooth the histogram. I used linspace in the example to pick the breaks, but if you know that some portion of the histogram exhibits much greater curvature than elsewhere, you'd want to have more breaks in that region and less elsewhere in order to accurately capture the shape of the histogram.
To smooth the histogram you need to use a 1-D filter. This is easily done using the filter function. Here is an example:
I = imread('pout.tif');
h = imhist(I);
smooth_h = filter(normpdf(-4:4, 0,1),1,h);
Of course you can use any smoothing function you choose. The mean would simply be ones(1,8).
Since your goal here is just to find the threshold to binarize an image you could just use the graythresh function which uses Otsu's method.

Gaussian smoothing in MATLAB

For an m x n array of elements with some noisy images, I want to perform Gaussian smoothing. How do I do that in MATLAB?
I've read the math involves smoothing everything with a kernel at a certain scale, but I have no idea how to do this in MATLAB.
Hopefully, you have the Image Processing toolbox. If so, then you can create a Gaussian filter with the fspecial function like so:
myfilter = fspecial('gaussian',[3 3], 0.5);
I have used the default values for hsize ([3 3]) and sigma (0.5) here, but you might want to play around with them. hsize is just the size of the filter, in this case it is a 3 x 3 matrix. Sigma is the sigma of the gaussian function (see the bottom of the fspecial function page).
Now you can use imfilter to filter your image:
myfilteredimage = imfilter(unfilteredimage, myfilter, 'replicate');
here I have simply passed in the unfilteredimage, the filter, and a parameter that says how the filter should handle the boundaries. In this case, I've chosen replicate which sets input array values outside the bounds of the array to the nearest array border value, but you can try some other values (or leaving off that option sets all outside of image values to 0).