Code covered by the BSD License  

Highlights from
Separable N-Dimensional Convolution

4.0

4.0 | 1 rating Rate this file 9 Downloads (last 30 days) File Size: 3.3 KB File ID: #27957

Separable N-Dimensional Convolution

by Igor Solovey

 

19 Jun 2010 (Updated 26 Feb 2011)

N-dimensional convolution for separable kernels, similar to functionality of "conv2(hcol, hrow, A)"

| Watch this File

File Information
Description

This function is an implementation of N-dimensional convolution for the special case when the convolution kernel is separable.

A continuous function f(x1, x2, ... xN) is considered separable if there exist N functions f1, f2, ... fN such that f(x1, x2, ... xN) = f1(x1)f2(x2)f3(x3)...fN(xN).

In two dimensions, a discrete version of the separability condition is as follows: a 2D matrix is separable if it can be expressed as an outer product of two 1D vectors:

a=[-1 0 1]; b=[1 0 -1]; H=a'*b

In this case, supplying Matlab's conv2 function with the two vectors a & b (one to convolve the rows with, the second to convolve the columns with) results in a faster computation than supplying conv2 with the outer product H.

Convnsep.m extends this functionality to an arbitrary number of dimensions.

This may be useful for applications such as:
1. Smoothing a 3D image with separable filters
2. Bilateral filtering (requires 4D convolution for filtering a 3D image)

Notes and caveats:

- As the total size of the variable being convolved increases towards the largest size allowable by Matlab (platform/architecture dependent), performance significantly degrades due to memory limitations, and may become worse than that of convn. Because Matlab requires a contiguous block of memory to allocate space for a variable, the maximum size for which performance is reasonable may be significantly lower than the "theoretical" size limit (which is 1.5 GB on my platform).

- For convolution types other than 'full' (no discarding of any samples), only odd-sized kernels are currently working properly.

Acknowledgements

This file inspired Fast Bilateral Filter For 3 D Images.

MATLAB release MATLAB 7.10 (R2010a)
Other requirements see Note above on memory performance
Tags for This File  
Everyone's Tags
3d, 4d, convolution, nd, separability, signal processing
Tags I've Applied
Add New Tags Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (5)
03 Jun 2011 Taemin

Now, we can modify the interface of convn to provide separable convolutions like separable conv2.

29 May 2011 Igor Solovey

Thanks Taemin, you're right. I wasn't aware that you can just supply a 1D vector as one of the arguments of convn.

%normalized gaussian kernel
a=[.0003 .1065 .7866 .1065 .0003];
n=length(a);
%make a test matrix
A = randn(64,64,64,64);
tic
b=a;
B3=convn(b,A);
b=a';
B3=convn(b,B3);
b=reshape(a,[1 1 n]);
B3=convn(b,B3);
b=reshape(a,[1 1 1 n]);
B3=convn(b,B3);
toc

28 May 2011 Taemin

My suggestion is to apply convn with for all dimensions separately. It is more than 3 times faster than full dimensional convn and yours with your test set.

26 Feb 2011 Igor Solovey

Thank you, Maxime. Sorry for not including n2s - it's just an alias function I made for num2str. I submitted an update which simply replaces n2s with num2str.

As for trimarray, perhaps the way I wrote it is a little weird, but the goal is to trim pixels away from the edges of the result, to replicate the behaviour of convn with shape set to 'valid' or 'same'.

26 Feb 2011 Maxime

I only used it with a all-1's 3-D kernel on a volume image (150x200x200) and it works fine. The time decreased from 5s down to 1s (although I was using a MEX version of 3d convolution). Good job!

However, the trimarray part seems a little weird and does not work since n2s does not exist. Any help on that would be appreciated.

Updates
26 Feb 2011

Fixed a reference to num2str

Contact us