I applied a Gaussian low pass filter on an image using MATLAB for different standard deviations and recorded the time each method takes. I saw that implementing the filter in the frequency domain is much more efficient (faster). Does anyone has an explanation for this?
Assuming that you use imfilter, this function performs a convolution of the original image with the kernel (the gaussian filter image).
For going into the frequency domain and back, fast fourier transform (FFT) algorithms are used, and only an image multiplication is performed in the frequency domain.
imfilter will therefore take about N.M operations, being N and M the number of pixels in the image and kernel respectively.
Each of FFT or its inverse have complexity N log_2 N, and the multiplication has complexity N, for a total complexity of approximately N log_2 N, which is much faster than the convolution.
Related
There are multiple ways to do sharpening on image using matlab
for example
im=imsharpen(old_image,'Radius',2,'Amount',1);
im=imfilter(old_image,fspecial('unsharp'));
imshow(im)
how can i undo this operation(sharpening) and return the original image??
You cannot undo the effect of a filter, in general. Filtering, even sharpening filters, combines the values in a neighborhood, reducing information.
For a small class of linear filters, those that do not zero out any frequencies, it is possible to reverse the operation to a certain extent. This requires that no clipping occurred. That is, the result of the filter was saved as floating-point values rather than uint8 or similar. Reversing the operation then involves multiplying in the frequency domain by the point-wise inverse of the filter. The linear filter kernel h convolves the image f, that implies that they are multiplied in the frequency domain, roughly: g = ifftn(fftn(f).*fftn(h)). Then f = ifftn(fftn(g)./fftn(h)).
I say roughly because the above requires padding h to the size of f.
Note that where fftn(h) is 0, the division results in NaN (since you do 0/0), not the original value of f. This puts a strong limit to the class of filters that you can "undo". Furthermore, if the filtered image has noise added (this is likely except for purely theoretical cases) then the noise will be amplified for frequencies where the filter has small values. Basically, even small amounts of noise make this process fail.
The Wiener filter does the above with regularización, such that noise and near-zero filter values don't cause to blow out your answer. There are more complex iterative solvers for the ill-posed inverse transform, but that is a large topic. Start your search with Wiener and you'll eventually discover those too.
On the other hand, if you are looking for a filter that does the opposite --smoothing -- look for example for imgaussfilt.
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...
In MATLAB I need to generate a second derivative of a gaussian window to apply to a vector representing the height of a curve. I need the second derivative in order to determine the locations of the inflection points and maxima along the curve. The vector representing the curve may be quite noise hence the use of the gaussian window.
What is the best way to generate this window?
Is it best to use the gausswin function to generate the gaussian window then take the second derivative of that?
Or to generate the window manually using the equation for the second derivative of the gaussian?
Or even is it best to apply the gaussian window to the data, then take the second derivative of it all? (I know these last two are mathematically the same, however with the discrete data points I do not know which will be more accurate)
The maximum length of the height vector is going to be around 100-200 elements.
Thanks
Chris
I would create a linear filter composed of the weights generated by the second derivative of a Gaussian function and convolve this with your vector.
The weights of a second derivative of a Gaussian are given by:
Where:
Tau is the time shift for the filter. If you are generating weights for a discrete filter of length T with an odd number of samples, set tau to zero and allow t to vary from [-T/2,T/2]
sigma - varies the scale of your operator. Set sigma to a value somewhere between T/6. If you are concerned about long filter length then this can be reduced to T/4
C is the normalising factor. This can be derived algebraically but in practice I always do this numerically after calculating the filter weights. For unity gain when smoothing periodic signals, I will set C = 1 / sum(G'').
In terms of your comment on the equivalence of smoothing first and taking a derivative later, I would say it is more involved than that. As which derivative operator would you use in the second step? A simple central difference would not yield the same results.
You can get an equivalent (but approximate) response to a second derivative of a Gaussian by filtering the data with two Gaussians of different scales and then taking the point-wise differences between the two resulting vectors. See Difference of Gaussians for that approach.
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...
I'm working on a control system that measures the movement of a vibrating robot arm. Because there is some deadtime, I need to look into the future of the somewhat noisy signal.
My idea was to use the frequencies in the sampled signal and produce a fourier function that could be used for extrapolation.
My question: I already have the FFT of the signal vector (containing 60-100 values e.g.) and can see the main frequencies in the amplitude spectrum. Now I want to have a function f(t) which fits to the signal, removes some noise, and can be used to predict the near future of the signal. How do I calculate the coefficients for the sine/cosine functions out of the complex FFT data?
Thank you so much!
AFAIR FFT essentially produces output as a sum of sine functions with different frequencies. The importance of each frequency is the height of each peak. So what you really want to do here is filter out some frequencies (ie. high frequencies for the arm to move gently) and then come back to the time domain.
In matlab this should be like going through the vector of what you got from fft, setting some values to 0 (or doing something more complex to it) and then use ifft to come back to time domain and make the prediction based on what you get.
There's also one thing you should consider while doing this - Nyquist frequency - this means that the highest frequency that you get on your fft is half of the sampling frequency.
If you use an FFT for data that isn't periodic within the FFT aperture length, then you may need to use a window to reduce spurious frequencies due to "spectral leakage". Frequency estimation techniques to better estimate "between bin" frequency content may also be appropriate. The phase of each cosine sinusoid, relative to the edge of the window, is usually atan2(imag[i], real[i]). The frequency depends on the sample rate and bin number versus the length of the FFT.
You might also want to look into using a Kalman filter instead of an FFT.
Added: If your signal isn't exactly integer periodic in the FFT length, then you may want to do an fftshift before the FFT to move the resulting phase measurement reference point to the center of your data vector, instead of a possibly discontinuous circular edge.