{"id":98,"date":"2006-11-28T11:23:47","date_gmt":"2006-11-28T16:23:47","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=98"},"modified":"2019-10-22T16:35:53","modified_gmt":"2019-10-22T20:35:53","slug":"separable-convolution-part-2","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2006\/11\/28\/separable-convolution-part-2\/","title":{"rendered":"Separable convolution: Part 2"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p><a href=\"https:\/\/blogs.mathworks.com\/steve\/?p=78\">Back in October<\/a> I introduced the concept of <i>filter separability<\/i>. A two-dimensional filter <i>s<\/i> is said to be separable if it can be written as the convolution of two one-dimensional filters <i>v<\/i> and <i>h<\/i>:\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/98\/determining_separability_eq1622.png\"> <\/p>\r\n   <p>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,\r\n      but I'm back on topic now.\r\n   <\/p>\r\n   <p>This question gave me one of earliest opportunities at The MathWorks to wander down to company co-founder <a href=\"https:\/\/www.mathworks.com\/company\/aboutus\/founders\/clevemoler.html\">Cleve's<\/a> office and ask for advice.  I asked, \"How can I determine if a matrix is an outer product of two vectors?\" Cleve was very\r\n      helpful, as he always is, although I was a little embarrassed afterward that I hadn't figured it out myself.  \"Go look at\r\n      the <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/rank.html\"><tt>rank<\/tt><\/a> function,\" Cleve told me.\r\n   <\/p>\r\n   <p>Of course.  If a matrix is an outer product of two vectors, its rank is 1.  Here are the key lines of code in <tt>rank<\/tt>:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">dbtype <span style=\"color: #A020F0\">15:19<\/span> <span style=\"color: #A020F0\">rank<\/span><\/pre><pre style=\"font-style:oblique\">\r\n15    s = svd(A);\r\n16    if nargin==1\r\n17       tol = max(size(A)') * eps(max(s));\r\n18    end\r\n19    r = sum(s &gt; tol);\r\n\r\n<\/pre><p>So the test is this:  The rank of <tt>A<\/tt> is the number of nonzero singular values of <tt>A<\/tt>, with some numerical tolerance based on <tt>eps<\/tt> and the size of <tt>A<\/tt>.\r\n   <\/p>\r\n   <p>Let's try it with a few common filters.<\/p>\r\n   <p>An averaging filter should be obvious:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">averaging = ones(5,5) \/ 25;\r\nrank(averaging)<\/pre><pre style=\"font-style:oblique\">\r\nans =\r\n\r\n     1\r\n\r\n<\/pre><p>The Sobel kernel:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">sobel = [-1 0 1; -2 0 2; -1 0 1];\r\nrank(sobel)<\/pre><pre style=\"font-style:oblique\">\r\nans =\r\n\r\n     1\r\n\r\n<\/pre><p>The two-dimensional Gaussian is the only radially symmetric function that is also separable:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">gaussian = fspecial(<span style=\"color: #A020F0\">'gaussian'<\/span>);\r\nrank(gaussian)<\/pre><pre style=\"font-style:oblique\">\r\nans =\r\n\r\n     1\r\n\r\n<\/pre><p>A disk is not separable:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">disk = fspecial(<span style=\"color: #A020F0\">'disk'<\/span>);\r\nrank(disk)<\/pre><pre style=\"font-style:oblique\">\r\nans =\r\n\r\n     5\r\n\r\n<\/pre><p>So how can we determine the outer product vectors? The answer is to go back to the <tt>svd<\/tt> function.  Here's a snippet from the doc:  <tt>[U,S,V] = svd(X)<\/tt> produces a diagonal matrix <tt>S<\/tt> of the same dimension as <tt>X<\/tt>, with nonnegative diagonal elements in decreasing order, and unitary matrices <tt>U<\/tt> and <tt>V<\/tt> so that <tt>X = U*S*V'<\/tt>.\r\n   <\/p>\r\n   <p>A rank 1 matrix has only one nonzero singular value, so <tt>U*S*V'<\/tt> becomes <tt>U(:,1) * S(1,1) * V(:,1)'<\/tt>.  This is basically the outer product we were seeking.  Therefore, we want the first columns of <tt>U<\/tt> and <tt>V<\/tt>. (We have to remember also to use the nonzero singular value as a scale factor.)\r\n   <\/p>\r\n   <p>Let's try this with the Gaussian filter.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">[U,S,V] = svd(gaussian)<\/pre><pre style=\"font-style:oblique\">\r\nU =\r\n\r\n   -0.1329    0.9581   -0.2537\r\n   -0.9822   -0.1617   -0.0959\r\n   -0.1329    0.2364    0.9625\r\n\r\n\r\nS =\r\n\r\n    0.6420         0         0\r\n         0    0.0000         0\r\n         0         0    0.0000\r\n\r\n\r\nV =\r\n\r\n   -0.1329   -0.6945   -0.7071\r\n   -0.9822    0.1880    0.0000\r\n   -0.1329   -0.6945    0.7071\r\n\r\n<\/pre><p>Now get the horizontal and vertical vectors from the first columns of <tt>U<\/tt> and <tt>V<\/tt>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">v = U(:,1) * sqrt(S(1,1))<\/pre><pre style=\"font-style:oblique\">\r\nv =\r\n\r\n   -0.1065\r\n   -0.7870\r\n   -0.1065\r\n\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">h = V(:,1)' * sqrt(S(1,1))<\/pre><pre style=\"font-style:oblique\">\r\nh =\r\n\r\n   -0.1065   -0.7870   -0.1065\r\n\r\n<\/pre><p>I have chosen, somewhat arbitrarily to split the scale factor, <tt>S(1,1)<\/tt>, \"equally\" between <tt>v<\/tt> and <tt>h<\/tt>.\r\n   <\/p>\r\n   <p>Now check to make sure this works:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">gaussian - v*h<\/pre><pre style=\"font-style:oblique\">\r\nans =\r\n\r\n  1.0e-015 *\r\n\r\n   -0.0243   -0.1527   -0.0243\r\n   -0.0139   -0.1110   -0.0139\r\n   -0.0035         0   -0.0035\r\n\r\n<\/pre><p>Except for normal floating-point roundoff differences, <tt>gaussian<\/tt> and <tt>v*h<\/tt> are equal.\r\n   <\/p>\r\n   <p>You can find code similar to this in the MATLAB function <a href=\"https:\/\/www.mathworks.com\/help\/matlab\/ref\/filter2.html\"><tt>filter2<\/tt><\/a>, as well as in the Image Processing Toolbox function <tt>imfilter<\/tt>.\r\n   <\/p><script language=\"JavaScript\"> \r\n<!--\r\n    function grabCode_e3f6bb290d5240d2b0b100bce7dae1a4() {\r\n        \/\/ Remember the title so we can use it in the new page\r\n        title = document.title;\r\n\r\n        \/\/ Break up these strings so that their presence\r\n        \/\/ in the Javascript doesn't mess up the search for\r\n        \/\/ the MATLAB code.\r\n        t1='e3f6bb290d5240d2b0b100bce7dae1a4 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' e3f6bb290d5240d2b0b100bce7dae1a4';\r\n    \r\n        b=document.getElementsByTagName('body')[0];\r\n        i1=b.innerHTML.indexOf(t1)+t1.length;\r\n        i2=b.innerHTML.indexOf(t2);\r\n \r\n        code_string = b.innerHTML.substring(i1, i2);\r\n        code_string = code_string.replace(\/REPLACE_WITH_DASH_DASH\/g,'--');\r\n\r\n        \/\/ Use \/x3C\/g instead of the less-than character to avoid errors \r\n        \/\/ in the XML parser.\r\n        \/\/ Use '\\x26#60;' instead of '<' so that the XML parser\r\n        \/\/ doesn't go ahead and substitute the less-than character. \r\n        code_string = code_string.replace(\/\\x3C\/g, '\\x26#60;');\r\n\r\n        author = 'Steve Eddins';\r\n        copyright = 'Copyright 2006 The MathWorks, Inc.';\r\n\r\n        w = window.open();\r\n        d = w.document;\r\n        d.write('<pre>\\n');\r\n        d.write(code_string);\r\n\r\n        \/\/ Add author and copyright lines at the bottom if specified.\r\n        if ((author.length > 0) || (copyright.length > 0)) {\r\n            d.writeln('');\r\n            d.writeln('%%');\r\n            if (author.length > 0) {\r\n                d.writeln('% _' + author + '_');\r\n            }\r\n            if (copyright.length > 0) {\r\n                d.writeln('% _' + copyright + '_');\r\n            }\r\n        }\r\n\r\n        d.write('<\/pre>\\n');\r\n      \r\n      d.title = title + ' (MATLAB code)';\r\n      d.close();\r\n      }   \r\n-->\r\n      <\/script>\r\n<noscript>\r\n<em>A JavaScript-enabled browser is required to use the \"Get the MATLAB code\" link.<\/em>\r\n<\/noscript>\r\n<p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_e3f6bb290d5240d2b0b100bce7dae1a4()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code<\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.3<br><\/p>\r\n<\/div>\r\n<!--\r\ne3f6bb290d5240d2b0b100bce7dae1a4 ##### SOURCE BEGIN #####\r\n%% Determining filter separability\r\n% <https:\/\/blogs.mathworks.com\/steve\/?p=78 Back in October>\r\n% I introduced the concept of _filter separability_. A\r\n% two-dimensional filter _s_ is said to be separable if it can be written as the\r\n% convolution two one-dimensional filters _v_ and _h_:\r\n%\r\n% $$ s = v*h $$\r\n%\r\n% I said then that \"next time\" I would explain how to determine whether a\r\n% given filter is separable.  Well, I guess I got side-tracked, but I'm\r\n% back on topic now.\r\n%\r\n% This question gave me one of earliest opportunities at The MathWorks to \r\n% wander down to company co-founder \r\n% <https:\/\/www.mathworks.com\/company\/aboutus\/founders\/clevemoler.html \r\n% Cleve's> office and ask for advice.  I asked, \"How can I determine if a \r\n% matrix is an outer product of two vectors?\" Cleve was very helpful, as he\r\n% always is, although I was a little embarrassed afterward that I hadn't\r\n% figured it out myself.  \"Go look at the \r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/rank.html \r\n% |rank|> function,\" Cleve told me.\r\n%\r\n% Of course.  If a matrix is an outer product of two vectors, its rank is\r\n% 1.  Here are the key lines of code in |rank|:\r\n\r\ndbtype 15:19 rank\r\n\r\n%% \r\n% So the test is this:  The rank of |A| is the number of nonzero singular \r\n% values of |A|, with some numerical tolerance based on |eps| and the size\r\n% of |A|.\r\n%\r\n% Let's try it with a few common filters.\r\n%\r\n% An averaging filter should be obvious:\r\n\r\naveraging = ones(5,5) \/ 25;\r\nrank(averaging)\r\n\r\n%%\r\n% The Sobel kernel:\r\n\r\nsobel = [-1 0 1; -2 0 2; -1 0 1];\r\nrank(sobel)\r\n\r\n%%\r\n% The two-dimensional Gaussian is the only radially symmetric function that\r\n% is also separable:\r\n\r\ngaussian = fspecial('gaussian');\r\nrank(gaussian)\r\n\r\n%%\r\n% A disk is not separable:\r\n\r\ndisk = fspecial('disk');\r\nrank(disk)\r\n\r\n%%\r\n% So how can we determine the outer product vectors? The answer is to go back\r\n% to the <<https:\/\/www.mathworks.com\/access\/helpdesk\/help\/techdoc\/ref\/svd.html \r\n% |svd|> function.  Here's a snippet from the doc:  |[U,S,V] = svd(X)| \r\n% produces a diagonal matrix |S| of the same dimension as |X|, with nonnegative\r\n% diagonal elements in decreasing order, and unitary matrices |U| and |V| so\r\n% that |X = U*S*V'|.  \r\n%\r\n% A rank 1 matrix has only one nonzero singular value, so |U*S*V'| becomes\r\n% |U(:,1) * S(1,1) * V(:,1)'|.  This is basically the outer product we were\r\n% seeking.  Therefore, we want the first columns of |U| and |V|. (We have\r\n% to remember also to use the nonzero singular value as a scale factor.)\r\n%\r\n% Let's try this with the Gaussian filter.\r\n\r\n[U,S,V] = svd(gaussian)\r\n\r\n%%\r\n% Now get the horizontal and vertical vectors from the first columns of |U|\r\n% and |V|. \r\n\r\nv = U(:,1) * sqrt(S(1,1))\r\n\r\n%%\r\n\r\nh = V(:,1)' * sqrt(S(1,1))\r\n\r\n%%\r\n% I have chosen, somewhat arbitrarily to split the scale factor, |S(1,1)|,\r\n% \"equally\" between |v| and |h|.\r\n%\r\n% Now check to make sure this works:\r\n\r\ngaussian - v*h\r\n\r\n%%\r\n% Except for normal floating-point roundoff differences, |gaussian| and\r\n% |v*h| are equal.\r\n%\r\n% You can find code similar to this in the MATLAB function \r\n% <https:\/\/www.mathworks.com\/help\/matlab\/ref\/filter2.html\r\n% |filter2|>, as\r\n% well as in the Image Processing Toolbox function \r\n% <https:\/\/www.mathworks.com\/help\/images\/index.htmlimfilter.html \r\n% |imfilter|>.\r\n##### SOURCE END ##### e3f6bb290d5240d2b0b100bce7dae1a4\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   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... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2006\/11\/28\/separable-convolution-part-2\/\">read more >><\/a><\/p>","protected":false},"author":42,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[1],"tags":[280,286,294,292,122,284,288,290,190,194,126,282],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/98"}],"collection":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/users\/42"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/comments?post=98"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/98\/revisions"}],"predecessor-version":[{"id":2230,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/98\/revisions\/2230"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=98"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=98"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=98"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}