how to find standard deviation of noise in an image using matlab? - 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.

Related

Calculate Expectation and Variance of Histogram in matlab

I want to compute the standard deviation and expected value of the given histogram.
Which matlab function will help me to do that?
My code:
I = imread('download.bmp');
imshow(I);title('Input Image');
imhist(I(:));title('Histogram of input image');
Not quite sure, whether I am getting your question properly.
By expected value of the histogram, do you mean the mean intensity value of the image, so basically which intensity you are most likely to draw if you draw a random pixel?
This you could simply get by doing
m_wholeImage = mean(I(:));
s_wholeImage = std(double(I(:)));
s_wholeImage then gives you the standard deviation of all pixel values.
I would suggest this reference :
Gonzalez., R., Eddins., S. and Woods, R. (2009). Digital image processing using MATLAB. 2nd ed. Gatesmark Publishing, pp.644-654.
It mentions code for quantifying texture in an image, and that involves finding mean and standard deviation of the image histogram.
I found the code online :
http://fourier.eng.hmc.edu/e161/dipum/statxture.m
http://fourier.eng.hmc.edu/e161/dipum/statmoments.m

Gaussian derivative - Matlab

I have an RGB image and I am trying to calculate its Gaussian derivative.
Image is a greyscale image and the Gaussian window is 5x5,st is the standard deviation
This is the code i am using in order to find a 2D Gaussian derivative,in Matlab:
N=2
[X,Y]=meshgrid(-N:N,-N:N)
G=exp(-(x.^2+y.^2)/(2*st^2))/(2*pi*st)
G_x = -x.*G/(st^2);
G_x_s = G_x/sum(G_x(:));
G_y = -y.*G/(st^2);
G_y_s = G_y/sum(G_y(:));
where st is the standard deviation i am using. Before I proceed to the convolution of the Image using G_x_s and G_y_s, i have the following problem. When I use a standard deviation that is an even number(2,4,6,8) the program works and gives results as expected. But when i use an odd number for standard deviation (3 or 5) then the G_y_s value becomes Inf because sum(G_y(:))=0. I do not understand that behavior and I was wondering if there is some problem with the code or if in the above formula the standard deviation can only be an even number. Any help will be greatly appreciated.
Thank you.
Your program doesn't work at all. The results you find when using an even number is just because of some numerical errors.
Your G will be a matrix symmetrical to the center. x and y are both point symmetrical to the center. So the multiplication (G times x or y) will result in a matrix with a sum of zero. So a division by that sum is a division by zero. Everything else you observe is because of some roundoff errors. Here, I see a sum og G_xof about 1.3e-17.
I think your error is in the multiplication x.*G and y.*G. I can not figure out why you would do that.
I assume you want to do edge detection rigth? You can use fspecialto create several edge filters. Laplace of gaussian for instance. You could also create two gaussian filters with different standard deviations and subtract them from another to get an edge filter.

Sum of MATLAB Gaussian distribution of an image is greater than 1

I am using the below code to calculate the probabilities of pixel intensities for the image given below. However, the total sum of probabilities sum(sum(probOfPixelIntensities)) is greater than 1.
I'm not sure where the mistake may be. Any help in figuring this out would be greatly appreciated. Thanks in advance.
clear all
clc
close all
I = imread('Images/cameraman.jpg');
I = rgb2gray(I);
imshow(I)
muHist = 134;
sigmaHist = 54;
Iprob = normpdf(double(I), muHist, sigmaHist);
sum(sum(Iprob))
What you are doing is computing the PDF values for every pixel in the image. Iprob is not a normal distribution but you are simply using the image pixels to sample from the distribution of a known mean and standard deviation.
Essentially, you are just performing a data transformation where the image pixel intensities get mapped to values on a normal PDF with a known mean and standard deviation. This is not the same as a PDF and that's why the sum is not 1. On top of this, the image pixel intensities don't even follow a normal distribution itself so there wouldn't be any way that the sum of the distribution is 1.
Not much more to say other than the output of normpdf is not what you are expecting it to be. You should opt to read the documentation of normpdf more carefully: http://www.mathworks.com/help/stats/normpdf.html
If it is your desire to determine the actual PDF of the image, what you need to do is find the histogram of the image, and not do a data transformation. You can do that with imhist. Once you do that, assuming that encountering the intensities is equiprobable, you would divide each histogram entry by the total size of the image and then sum along all bins. You should get the sum to be 1 in this case.
Just to verify, let's use the image you provided in your post. We'll read this in from StackOverflow. Once we do that, compute the PDF and then sum over all bins:
%// Load in image
im = rgb2gray(imread('http://i.stack.imgur.com/0XiU5.jpg'));
%// Compute PDF
h = imhist(im) / numel(im);
%// Sum over all bins
fprintf('Total sum over all bins is: %f\n', sum(h));
We get:
Total sum over all bins is: 1.000000
Just to be absolutely sure you understand, this is the PDF of the image. What you did before was perform a data transformation where you transformed all image pixel intensities that conforms to a Gaussian distribution with a known mean and standard deviation. This will not give you a sum of 1 as you expect.
Remember that PDF is only the probability density function $p(x)$. Function which is restricted to range $[0, 1]$ is the integral over all domain of that function $\int_D p(x)dx$.
Refer to the Matlab manual, Y = normpdf(X,mu,sigma) computes the pdf at each of the values in X using the normal distribution with mean mu and standard deviation sigma.
The sum of the pdf is equal to 1.
The sum of the output is not.

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!

Gaussian filter in 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.