3D Gaussian Filter in MATLAB - matlab

Is there a 3D eqivalent of imfilter available for MATLAB? I wish to apply Gaussian filtering to a 3D histogram. I was going to implement it myself, by creating a (3D) Gaussian filter, then looping over each element in my histogram, and summing up the corresponding data entries.
However, I didn't want to implement it myself in a slow and inefficient way if there's something already out there, or a cleverer way of doing it.

There are two ways to solve this in order to do the filtering in an efficient manner:
(1) Use CONVN three times to filter your data with three 1D Gaussians, one x-by-1-by-1, one 1-by-y-by-1, and one 1-by-1-by-z.
(2) If you have the signal processing toolbox, use FFTFILT to perform filtering in inverse space (or use any one of the fft-convolution algorithms on the file exchange).
[(3) Send me an email and I'll send you my fftFilterImage, which does 3D Gauss filtering.]

imfilter can already do 3D filtering, as long as the data matrix and the filter you give it are 3D. See the imfilter page.

This task can be handled with the new (as of R2015a) imgaussfilt3 function.
The basic syntax is as follows:
B = imgaussfilt3(A,sigma)
There are also a number of name-value pair arguments:
'FilterSize': Size of the Gaussian filter, defaulting to a cube of size 2*ceil(2*sigma)+1.
'Padding': Type of padding ('replicate' (default) | 'circular' | 'symmetric').
'FilterDomain': Perform convolution in domain: 'frequency' or 'spatial' (default auto).

Related

How can i undo sharpening operation on image using matlab?

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.

MATLAB: How to slide a window over a matrix without padding?

I'll illustrate my problem on an example
I have a 3x100 matrix and I want to slide a 3x3 filter over it.
However, I do not want any of the padding that imfilter would use, such as X, symmetric, replicate, etc., which would yield a 3x100 output.
I rather want the sliding window to be only applied when there is real data, such that my output would be 1x (100 - 4).
What would be the most elegant (loopless) way to do this in matlab?
You can use the built-in conv2 function with 'Valid' as the shape parameter which will only provide results when there is a complete overlap between the filter and the data.
filtered = conv2(data, filter, 'valid');

Matlab image centroid simulation

I was given this task, I am a noob and need some pointers to get started with centroid calculation in Matlab:
Instead of an image first I was asked to simulate a Gaussian distribution(2 dimensional), add noise(random noise) and plot the intensities, now the position of the centroid changes due to noise and I need to bring it back to its original position by
-clipping level to get rid of the noise, noise reduction by clipping or smoothing, sliding average (lpf) (averaging filter 3-5 samples ), calculating the means or using Convolution filter kernel - which does matrix operations which represent the 2-D images
Since you are a noob, even if we wrote down the answer verbatim you probably won't understand how it works. So instead I'll do what you asked, give you pointers and you'll have to read the related documentation :
a) to produce a 2-d Gaussian use meshgrid or ndgrid
b) to add noise to the image look into rand ,randn or randi, depending what exactly you need.
c) to plot the intensities use imagesc
d) to find the centroid there are several ways, try to further search SO, you'll find many discussions. Also you can check TMW File exchange for different implementations for that.

Matlab dwt across specified dimension

I have a dataset Sig of size 65536 x 192 in Matlab. If I want to take the one-dimensional fft along the second dimension, I could either do a for loop:
%pre-allocate ect..
for i=1:65536
F(i,:) = fft(Sig(i,:));
end
or I could specify the dimension and do it without the for loop:
F = fft(Sig,[],2);
which is about 20 times faster for my dataset.
I have looked for something similar for the discrete wavelet transform (dwt), but been unable to find it. So I was wondering if anyone knows a way to do dwt across a specified dimension in Matlab? Or do I have to use for loops?
In your loop FFT example, it seems you operate on lines. Matlab use a Column-major order. It may explain the difference of performance. Is the performance the same if you operate on columns ?
If this is the right explanation, you could use dwt in a loop.
A solution if you really need performance is to do your own MEX calling a C discrete wavelet transform library the way you want.
I presume you're using the function from the Wavelet Toolbox: http://www.mathworks.co.uk/help/toolbox/wavelet/ref/dwt.html
The documentation doesn't seem to describe acting on an array, so it's probably not supported. If it does allow you to input an array, then it will operate on the first non-singleton dimension or it will ignore the shape and treat it as a vector.

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.