Mixture Noise on Image with Matlab - matlab

With some requirement it needs to add mixture noise to image in Matlab. Mixture will have Salt & Pepper Noise varying from 5% to 23% with the fix 5% of Gaussian Noise. I am trying to add said mixture to Image with following code:
I = imread('lena_gray_512.tif');
figure, imshow(I);title('ORIGINAL');
J = imnoise(I,'salt & pepper', 0.23);
figure, imshow(J);title('salty');
K = imnoise(J,'gaussian',0,0.0005);
figure, imshow(K);title('both');
First thing is to verify whether the code will add said required noise to image of 23% Salt& Peeper and 5% Gaussian Noise?
Further, median filter is used to remove the mixed Noise from Image, it works good having PSNR 35. Is there any way to get PSNR around 50 for the same Image with this same mixed Noise?

Related

Power spectral density of gussin white noise

I am trying to generate Gussin white noise with standard deviation 7% and sample frequency 2000 Hz. Then calculate the power spectral density of it (PSD). The problem is when I plotted PSD I know that I have to get flat PSD. however, I did not
I used below code
clear;
clc;
x =7*randn(1,10000);
N=10000;
sample_frequency=2000;
fax_bin=[0 :N/2-1];
fax_Hz=fax_bin*sample_frequency/N;
FFT_A=fft(x);
spectra=FFT_A.*conj(FFT_A);
figure
plot(fax_Hz,spectra(1,1:5000));
It is a random process so you will never get it truly flat, it gets better with more samples. Additionally, plotting it on a dB-scale makes more sense.
plot(fax_Hz,10*log10(spectra(1,1:N/2)));

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 with cut-off frequency [duplicate]

I'm playing around with hybrid images, and wanted to use a gaussian filter to low pass filter an image. However, to make hybrid images, 2 filters are supposed to be used on the 2 images being combined with different cut off frequencies.
Does fspecial() allow us to specify cut off frequencies when we employ it to make a gaussian filter? (I know that we can specify the filter size and sigma and that there is some relation between sigma and cut off frequency). If we can only specify cut off frequencies using sigma, then what sigma would I need to set to get a cut off frequency of say 0.2 Hz.
I'll first answer regarding 1D and the rest will follow. It may look trivial, but bear with me for a while. Lets assume the following code:
t=linspace(0,20,2^10); %time vector in seconds
w=0.2; %in Hz
signal=cos(2*pi*w*t)+rand(1,length(t))-0.5; % signal in seconds
dt=t(2)-t(1) ;
N=length(signal);
df=1/(N*dt); % the frequency resolution (df=1/max_T)
if mod(N,2)==0
f_vec= df*((1:N)-1-N/2); % for EVEN length vectors
else
f_vec= df*((1:N)-0.5-N/2);
end
So, we have created a noisy signal of a specific frequency. f_vec is the frequency vector that stretches from f =[-f_max,-f_max+df,...,0,...,f_max], with f_max=1/(2*dt). If we now design a 1D Gaussian filter (in the fourier space) as follows:
f_vec0=0;
sigma=1;
filter=exp( -(f_vec-f_vec0).^2./(2*sigma^2));
and then filtering in the fourier doamin:
f_signal=fftshift(fft(signal));
filt_signal=fftshift(ifft(f_signal.*filter));
So, from the filter we applied, sigma=1 means the the cut off frequency (that I decided is 1% of the filter's maximum (which is 1)) is approximately at 3 Hz:
cutoff_freq=f_vec(find(filter>=0.01,1,'last'))
Taking this to 2D is trivial, just be careful with units. For images, there are pixels as the position unit, and 1/pixels as the spacial frequency. The fspecial function generates a 2D matrix of a predefined filter. The usage of fspecial is usually like this:
PSF = fspecial('gaussian',hsize,sigma);
Blurred = imfilter(Image,PSF,'symmetric','conv');
Using convolution is just like multiplying in the Fourier domain. The sigma in the Fourier domain is proportional to 1/sigma of the position domain, etc...

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.

Low pass gaussian filter with a specified cut off frequency

I'm playing around with hybrid images, and wanted to use a gaussian filter to low pass filter an image. However, to make hybrid images, 2 filters are supposed to be used on the 2 images being combined with different cut off frequencies.
Does fspecial() allow us to specify cut off frequencies when we employ it to make a gaussian filter? (I know that we can specify the filter size and sigma and that there is some relation between sigma and cut off frequency). If we can only specify cut off frequencies using sigma, then what sigma would I need to set to get a cut off frequency of say 0.2 Hz.
I'll first answer regarding 1D and the rest will follow. It may look trivial, but bear with me for a while. Lets assume the following code:
t=linspace(0,20,2^10); %time vector in seconds
w=0.2; %in Hz
signal=cos(2*pi*w*t)+rand(1,length(t))-0.5; % signal in seconds
dt=t(2)-t(1) ;
N=length(signal);
df=1/(N*dt); % the frequency resolution (df=1/max_T)
if mod(N,2)==0
f_vec= df*((1:N)-1-N/2); % for EVEN length vectors
else
f_vec= df*((1:N)-0.5-N/2);
end
So, we have created a noisy signal of a specific frequency. f_vec is the frequency vector that stretches from f =[-f_max,-f_max+df,...,0,...,f_max], with f_max=1/(2*dt). If we now design a 1D Gaussian filter (in the fourier space) as follows:
f_vec0=0;
sigma=1;
filter=exp( -(f_vec-f_vec0).^2./(2*sigma^2));
and then filtering in the fourier doamin:
f_signal=fftshift(fft(signal));
filt_signal=fftshift(ifft(f_signal.*filter));
So, from the filter we applied, sigma=1 means the the cut off frequency (that I decided is 1% of the filter's maximum (which is 1)) is approximately at 3 Hz:
cutoff_freq=f_vec(find(filter>=0.01,1,'last'))
Taking this to 2D is trivial, just be careful with units. For images, there are pixels as the position unit, and 1/pixels as the spacial frequency. The fspecial function generates a 2D matrix of a predefined filter. The usage of fspecial is usually like this:
PSF = fspecial('gaussian',hsize,sigma);
Blurred = imfilter(Image,PSF,'symmetric','conv');
Using convolution is just like multiplying in the Fourier domain. The sigma in the Fourier domain is proportional to 1/sigma of the position domain, etc...