{"id":1403,"date":"2015-08-21T07:00:54","date_gmt":"2015-08-21T11:00:54","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=1403"},"modified":"2019-11-01T11:46:44","modified_gmt":"2019-11-01T15:46:44","slug":"moving-the-origin-of-a-structuring-element","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2015\/08\/21\/moving-the-origin-of-a-structuring-element\/","title":{"rendered":"Moving the origin of a structuring element"},"content":{"rendered":"<div class=\"content\"><p>A customer contacted Tech Support recently to ask the following question about structuring elements and dilation:<\/p><p><i>If I created a structuring element from the matrix [1 1; 1 1], then the origin of the structuring element is the (1,1) or upper-left element. How do I create a structuring element so that the origin is at (1,2) or (2,1)?<\/i><\/p><p>There are two equivalent ways to accomplish this.<\/p><p>In the first way, you add a row or column (or both) to the matrix that you pass to <tt>strel<\/tt> or <tt>imdilate<\/tt> so that the center of the new matrix is the desired structuring element origin. For example:<\/p><pre class=\"codeinput\">se = strel([0 1 1; 0 1 1; 0 0 0])\r\n<\/pre><pre class=\"codeoutput\"> \r\nse =\r\n \r\nFlat STREL object containing 4 neighbors.\r\n\r\nNeighborhood:\r\n     0     1     1\r\n     0     1     1\r\n     0     0     0\r\n\r\n \r\n<\/pre><p>In the second way, you pass the original 2-by-2 matrix to <tt>strel<\/tt> and then call the function <tt>translate<\/tt> to \"move\" the origin to the desired location.<\/p><pre class=\"codeinput\">se = strel([1 1; 1 1])\r\n<\/pre><pre class=\"codeoutput\"> \r\nse =\r\n \r\nFlat STREL object containing 4 neighbors.\r\n\r\nNeighborhood:\r\n     1     1\r\n     1     1\r\n\r\n \r\n<\/pre><pre class=\"codeinput\">se2 = translate(se,[-1 0])\r\n<\/pre><pre class=\"codeoutput\"> \r\nse2 =\r\n \r\nFlat STREL object containing 4 neighbors.\r\n\r\nNeighborhood:\r\n     0     1     1\r\n     0     1     1\r\n     0     0     0\r\n\r\n \r\n<\/pre><script language=\"JavaScript\"> <!-- \r\n    function grabCode_37b4afec9fac44c390d74d9a3aeb9da4() {\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='37b4afec9fac44c390d74d9a3aeb9da4 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 37b4afec9fac44c390d74d9a3aeb9da4';\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        copyright = 'Copyright 2015 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 copyright line at the bottom if specified.\r\n        if (copyright.length > 0) {\r\n            d.writeln('');\r\n            d.writeln('%%');\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     --> <\/script><p style=\"text-align: right; font-size: xx-small; font-weight:lighter;   font-style: italic; color: gray\"><br><a href=\"javascript:grabCode_37b4afec9fac44c390d74d9a3aeb9da4()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n      the MATLAB code <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; R2015a<br><\/p><\/div><!--\r\n37b4afec9fac44c390d74d9a3aeb9da4 ##### SOURCE BEGIN #####\r\n%%\r\n% A customer contacted Tech Support recently to ask the following question about\r\n% structuring elements and dilation:\r\n%\r\n% _If I created a structuring element from the matrix [1 1; 1 1], then the\r\n% origin of the structuring element is the (1,1) or upper-left element. How do I\r\n% create a structuring element so that the origin is at (1,2) or (2,1)?_\r\n%\r\n% There are two equivalent ways to accomplish this.\r\n%\r\n% In the first way, you add a row or column (or both) to the matrix that you\r\n% pass to |strel| or |imdilate| so that the center of the new matrix is the\r\n% desired structuring element origin. For example:\r\n\r\nse = strel([0 1 1; 0 1 1; 0 0 0])\r\n\r\n%%\r\n% In the second way, you pass the original 2-by-2 matrix to |strel| and then\r\n% call the function |translate| to \"move\" the origin to the desired location.\r\n\r\nse = strel([1 1; 1 1])\r\n\r\n%%\r\n\r\nse2 = translate(se,[-1 0])\r\n\r\n\r\n##### SOURCE END ##### 37b4afec9fac44c390d74d9a3aeb9da4\r\n-->","protected":false},"excerpt":{"rendered":"<p>A customer contacted Tech Support recently to ask the following question about structuring elements and dilation:If I created a structuring element from the matrix [1 1; 1 1], then the origin of the... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2015\/08\/21\/moving-the-origin-of-a-structuring-element\/\">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":[106,1123],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/1403"}],"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=1403"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/1403\/revisions"}],"predecessor-version":[{"id":1404,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/1403\/revisions\/1404"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=1403"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=1403"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=1403"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}