Matlab functions    from  BioméCardio    

 
   polydeg
simps   

sffilt

Scalar function-based filtering

Contents

Download

sffilt.m

Syntax

   J = sffilt(func,I)
   J = sffilt(func,I,m)
   J = sffilt(func,I,[m n])
   J = sffilt(func,I,[m n p])
   J = sffilt(func,I,domain)
   J = sffilt(func,I,...,padopt)

Description

sffilt(func,I,...) replaces each element in the 1D, 2D or 3D array I by the scalar issued from the function func applied on the elements in the neighborhood around the corresponding pixel. func must be a scalar function.

func can be a string that represents a scalar function ('max', 'min', 'mean', 'median', 'std', 'var', 'sum', 'prod', 'nanmean',...), a function handle (@max, @median, @sum,...) or an inline function object.

You can use some typical filters:

or you can create your own filter:

  1. rms = @(x) sqrt(mean(x.^2)) % root-mean-square-based filter
  2. B = sffilt(rms,A)

B = sffilt(func,A,[m m p]) performs scalar-function filtering of the 3-D array A. Each output pixel contains scalar func value in the m by n by p neighborhood around the corresponding pixel in the input array.

J = sffilt(func,I,[m n]) performs scalar-function filtering of the image I. Each output pixel contains the scalar func value in the m by n neighborhood around the corresponding pixel.

z = sffilt(func,y,m) performs scalar-function filtering of the signal y. Each output pixel contains the scalar func value in the m neighborhood around the corresponding pixel.

B = sffilt(func,A) performs scalar-function filtering using a 3 or 3x3 or 3x3x3 neighborhood according to the size of A.

B = sffilt(func,A,domain) replaces each element in A by the scalar calculated from the set of neighbors specified by the nonzero elements in domain. domain is equivalent to the structuring element used for binary image operations. It is a matrix containing only 1's and 0's; the 1's define the neighborhood for the filtering operation. For example, B = sffilt('max',A,[0 1 0; 1 0 1; 0 1 0]) replaces each element in A by the maximum of its north, east, south, and west neighbors. The size of the domain must be odd in each direction.

B = sffilt(func,A,...,padopt) pads array A using padopt option. String values for padopt (default = 'replicate'):

Moving average

This examples uses a moving average filter with a span of 15 points.

x = 0:100;
y = cos(x/10)+(x/50).^2 + 0.2*randn(size(x));
ys = sffilt('mean',y,15);

Plot the original data and the smoothed data:

plot(x,y,'.-',x,ys,'k')

Grayscale image dilation

This examples uses a "maximum" filter with a [5 5] neighborhood. This is equivalent to imdilate(image,strel('square',5)) (with Image Processing Toolbox).

A = imread('snowflakes.png');
B = sffilt('max',A,[5 5]);
subplot(211), imshow(A), title('Original picture')
subplot(212), imshow(B), title('Dilated image')

2-D median filtering

Let us read an image and make it noisy with salt-and-pepper noise...

I = imread('eight.tif');
I(rand(size(I))<0.01) = 255;
I(rand(size(I))<0.01) = 0;
imshow(I), title('Noisy image')

And let us reconstruct the image using a 3x3 median filter:

J = sffilt('median',I);
imshow(J), title('Filtered image')

3-D median filtering

This examples uses a median filter with a 3x3x3 neighborhood. You could also use medfilt3.

First create a 3-D flow data and add some white noise:

[x,y,z,V] = flow(50);
noisyV = V + 0.1*double(rand(size(V))>0.95);

Filter the data with a 3-D median filter:

filteredV = sffilt('median',noisyV);

And display the result:

subplot(121)
hpatch = patch(isosurface(x,y,z,noisyV,0));
isonormals(x,y,z,noisyV,hpatch)
set(hpatch,'FaceColor','red','EdgeColor','none')
daspect([1,4,4]), view([-65,20]), axis tight off
camlight left; lighting phong
subplot(122)
hpatch = patch(isosurface(x,y,z,filteredV,0));
isonormals(x,y,z,filteredV,hpatch)
set(hpatch,'FaceColor','red','EdgeColor','none')
daspect([1,4,4]), view([-65,20]), axis tight off
camlight left; lighting phong

Morphological closing (dilation-erosion)

Use morphological closing to join the circles in the image together by filling in the gaps between them and by smoothing their outer edges. Read the image and display it.

BW = imread('circles.png');
imshow(BW)

Create a disk-shaped structuring element. Use a disk structuring element to preserve the circular nature of the object. Specify a radius of 7 pixels so that the largest gap gets filled.

domain = logical(fspecial3('ellipsoid',[13 13 1]));

Dilate then erode the image:

BW = sffilt('max',BW,domain);
BW = sffilt('min',BW,domain);
imshow(BW)

Information

Morphology Fundamentals: Dilation and Erosion, Moving average from Wolfram MathWorld, Median filter from Wikipedia.

See also

medfilt3 medfilt2, ordfilt2, imdilate, imerode, fspecial3

About the author

Damien Garcia, Eng., Ph.D.
Assistant professor, Department of radiology
CRCHUM, University of Montreal Hospital
Montreal, QC, Canada
Damien.Garcia.REMOVE-THIS@BiomeCardio.com

www.biomecardio.com