Skip to Main Content Skip to Search
File Exchange
MATLAB Newsgroup
Link Exchange
  Blogs  
 Contest 
MathWorks.com

Steve on Image Processing

November 28th, 2006

Separable convolution: Part 2

Back in October I introduced the concept of filter separability. A two-dimensional filter s is said to be separable if it can be written as the convolution of two one-dimensional filters v and h:

I said then that "next time" I would explain how to determine whether a given filter is separable. Well, I guess I got side-tracked, but I'm back on topic now.

This question gave me one of earliest opportunities at The MathWorks to wander down to company co-founder Cleve's office and ask for advice. I asked, "How can I determine if a matrix is an outer product of two vectors?" Cleve was very helpful, as he always is, although I was a little embarrassed afterward that I hadn't figured it out myself. "Go look at the rank function," Cleve told me.

Of course. If a matrix is an outer product of two vectors, its rank is 1. Here are the key lines of code in rank:

dbtype 15:19 rank
15    s = svd(A);
16    if nargin==1
17       tol = max(size(A)') * eps(max(s));
18    end
19    r = sum(s > tol);

So the test is this: The rank of A is the number of nonzero singular values of A, with some numerical tolerance based on eps and the size of A.

Let's try it with a few common filters.

An averaging filter should be obvious:

averaging = ones(5,5) / 25;
rank(averaging)
ans =

     1

The Sobel kernel:

sobel = [-1 0 1; -2 0 2; -1 0 1];
rank(sobel)
ans =

     1

The two-dimensional Gaussian is the only radially symmetric function that is also separable:

gaussian = fspecial('gaussian');
rank(gaussian)
ans =

     1

A disk is not separable:

disk = fspecial('disk');
rank(disk)
ans =

     5

So how can we determine the outer product vectors? The answer is to go back to the svd function. Here's a snippet from the doc: [U,S,V] = svd(X) produces a diagonal matrix S of the same dimension as X, with nonnegative diagonal elements in decreasing order, and unitary matrices U and V so that X = U*S*V'.

A rank 1 matrix has only one nonzero singular value, so U*S*V' becomes U(:,1) * S(1,1) * V(:,1)'. This is basically the outer product we were seeking. Therefore, we want the first columns of U and V. (We have to remember also to use the nonzero singular value as a scale factor.)

Let's try this with the Gaussian filter.

[U,S,V] = svd(gaussian)
U =

   -0.1329    0.9581   -0.2537
   -0.9822   -0.1617   -0.0959
   -0.1329    0.2364    0.9625


S =

    0.6420         0         0
         0    0.0000         0
         0         0    0.0000


V =

   -0.1329   -0.6945   -0.7071
   -0.9822    0.1880    0.0000
   -0.1329   -0.6945    0.7071

Now get the horizontal and vertical vectors from the first columns of U and V.

v = U(:,1) * sqrt(S(1,1))
v =

   -0.1065
   -0.7870
   -0.1065

h = V(:,1)' * sqrt(S(1,1))
h =

   -0.1065   -0.7870   -0.1065

I have chosen, somewhat arbitrarily to split the scale factor, S(1,1), "equally" between v and h.

Now check to make sure this works:

gaussian - v*h
ans =

  1.0e-015 *

   -0.0243   -0.1527   -0.0243
   -0.0139   -0.1110   -0.0139
   -0.0035         0   -0.0035

Except for normal floating-point roundoff differences, gaussian and v*h are equal.

You can find code similar to this in the MATLAB function filter2, as well as in the Image Processing Toolbox function imfilter.


Get the MATLAB code

Published with MATLAB® 7.3

7 Responses to “Separable convolution: Part 2”

  1. Dr Steve Sangwine replied on :

    I have been looking for a method to factor a rank 1 matrix into the outer product of two vectors, and this is the only place I’ve seen it described. The SVD is a bit of overkill for the problem in fact. The definition of a rank 1 matrix is that the columns (or rows) are not linearly independent. This means the columns are multiples of each other. Therefore the matrix can be factored by finding the column multiples using e.g.
    sum(m(:,2))./sum(m(;,1)). The factorization is simply expressed as m(:,1)*[alpha, beta, gamma ….] where the right hand vector is a vector of the scale factors. Obviously you can adjust the values in the two vectors afterwards, and you have to take some care if one column has a very small sum. This seems so simple when you see it, it perhaps explains why no-one bothers to describe it.

  2. Steve replied on :

    Steve—Thanks for your comment. I’ll look into it when I get a little more time.

  3. Tsega replied on :

    Dear Steve
    You have any suggestions on using SVD for noise filtering in images.
    Thank you. Great stuff!

  4. Steve replied on :

    Tsega—No, not really.

  5. Steve replied on :

    Steve—Well, I guess it depends on your perspective. From my point of view, I’m pretty happy just calling svd. No worries about writing and testing new numerical codes, adjusting values, worrying about low column sums, etc. And svd crunches a 33-by-33 Gaussian filter on my computer in just 90 microseconds. Also, the output from svd is used to test for rank equal 1, not just for computing the resulting outer product vectors of a known rank-1 matrix.

  6. spiele replied on :

    interesting method but I don’t think that this algorythm can be used for variety of tasks

  7. Steve replied on :

    Spiele—On the contrary—separable convolution is extremely common in image processing. The MATLAB function filter2 and the Image Processing Toolbox function imfilter both test for separability using the technique described here.

Leave a Reply


Steve Eddins manages the Image & Geospatial development team at The MathWorks and coauthored Digital Image Processing Using MATLAB. He writes here about image processing concepts, algorithm implementations, and MATLAB.

  • Mikr: I look for answers before asking people… “But we still can’t see the coordinates!...
  • Steve: Mikr—You might want to take a look at the Getting Started section of the MATLAB documentation in order...
  • Mikr: thanks but is it possible to see and write to file (Excel ?) that matrix of pixel coordinates ? instead of...
  • Steve: Mikr—An image in MATLAB is simply a matrix of pixel values. It can be saved (exported) to several common...
  • Mikr: thanks, Steve just started to learn matlab and to clarify matlab saves image files as a matrix of pixel...
  • Steve: Mikr—As far as I know, the commonly used image file formats such as TIFF, JPEG, PNG, etc., do not...
  • Mikr: how to write pixel coordinates in file ?
  • Steve: M.S.—Code for the bwtraceboundaries function ships with the Image Processing Toolbox.
  • M.S.Cheema: i need to know the detailed algorithm for bwtraceboundaries. i want how that function works. so please...
  • Steve: Wagas—It depends on how much memory you have on your computer. You should be able to load a 94 MB TIFF...

These postings are the author's and don't necessarily represent the opinions of The MathWorks.

Related Topics