I now want to use asymmetric Gaussian filter kernel to smooth an image using MATLAB, because I don't want the equal smoothness in vertical and horizontal(with different size of Gaussian mode and different standard deviation). But I cannot find a system function to do this job. It seems that the function fspecial()
doesn't support this.
So, how can I implement this filter?
Thanks a lot.
You can apply horizontal and vertical filtering separately.
v = fspecial( 'gaussian', [11 1], 5 ); % vertical filter
h = fspecial( 'gaussian', [1 5], 2 ); % horizontal
img = imfilter( imfilter( img, h, 'symmetric' ), v, 'symmetric' );
Moreover, you can "compose" the two filters using an outer product
f = v * h; % this is NOT a dot product - this returns a matrix!
img = imfilter( img, f, 'symmetric' );
PS
if you are looking for a directional filtering, you might want to consider fspecial('motion'...)
You can use fspecial
with a twist, for example:
H= fspecial('gaussian',15,2) ;
H2=imresize(H,[1.5*size(H,1) size(H,2)]);
Img=conv2(Img,H2,'same');
Using imresize
on the filter allows to control the x vs y axis asymmetry of the gaussian. Similarly you can use any type of image transformation (see imtransform
) you can imagine to skew stretch etc...
You can approximate a Gaussian filter by applying a Box filter multiple times. Since the Gaussian is separable, you can do this separately in both dimensions. A box filter in one dimension is a simple average over a linear segment of pixels. I don't know anything about matlab but I assume it can do this. If matlab can do rectangular filters you wouldn't even need to separate it.
For more details about approximating a Gaussian see http://nghiaho.com/?p=1159
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With