{"id":37,"date":"2006-02-14T07:00:34","date_gmt":"2006-02-14T12:00:34","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=37"},"modified":"2019-10-21T17:44:18","modified_gmt":"2019-10-21T21:44:18","slug":"spatial-transformations-maketform-tformfwd-and-tforminv","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2006\/02\/14\/spatial-transformations-maketform-tformfwd-and-tforminv\/","title":{"rendered":"Spatial transformations: maketform, tformfwd, and tforminv"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>Several Image Processing Toolbox functions related to spatial transformations use \"tform\" structures.  A tform structure has\r\n         data and function handles needed for applying a spatial transformation in the forward or the inverse direction, and sometimes\r\n         in both directions.\r\n      <\/p>\r\n   <\/introduction>\r\n   <h2>Contents<\/h2>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">maketform<\/a><\/li>\r\n         <li><a href=\"#2\">tformfwd<\/a><\/li>\r\n         <li><a href=\"#3\">tforminv<\/a><\/li>\r\n         <li><a href=\"#4\">Introducing George<\/a><\/li>\r\n         <li><a href=\"#6\">Scaling example<\/a><\/li>\r\n         <li><a href=\"#7\">Rotation example<\/a><\/li>\r\n         <li><a href=\"#8\">Translation example<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h2>maketform<a name=\"1\"><\/a><\/h2>\r\n   <p>Function <tt>maketform<\/tt> has several syntaxes for constructing different types of tform structures.  For example, you can construct an affine transform\r\n      by providing an A matrix:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">A = [2 0 0; 0 2 0; 0 0 1];  <span style=\"color: #228B22\">% Stretch by a factor of 2 in both directions.<\/span>\r\ntform = maketform(<span style=\"color: #A020F0\">'affine'<\/span>, A);<\/pre><h2>tformfwd<a name=\"2\"><\/a><\/h2>\r\n   <p>Toolbox function <tt>tformfwd<\/tt> transforms points from input space to output space.  For example, to apply the affine transform above to the point (u,v)\r\n      = (2,3), do this:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">uv = [2 3];\r\nxy = tformfwd(tform, uv)<\/pre><pre style=\"font-style:oblique\">\r\nxy =\r\n\r\n     4     6\r\n\r\n<\/pre><h2>tforminv<a name=\"3\"><\/a><\/h2>\r\n   <p>Function <tt>tforminv<\/tt> transforms points from output space to input space. If we apply the inverse transformation to the point <tt>xy<\/tt> computed above, we should get back the original point.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">uvp = tforminv(tform, xy)<\/pre><pre style=\"font-style:oblique\">\r\nuvp =\r\n\r\n     2     3\r\n\r\n<\/pre><h2>Introducing George<a name=\"4\"><\/a><\/h2>\r\n   <p>The spatial transformation functions <tt>imtransform<\/tt>, <tt>tformarray<\/tt>, <tt>tformfwd<\/tt>, <tt>tforminv<\/tt>, etc., were introduced to Image Processing Toolbox version 3 in 2001.  While designing these functions, we almost always\r\n      had input-space output-space diagrams on our whiteboards so we could keep our notation straight.\r\n   <\/p>\r\n   <p>Because I have no drawing skills, I always drew a crude head-and-shoulders outline on these diagrams as the \"image\" being\r\n      transformed.  I started calling this figure \"George.\"  Here's what he looks like:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">% https:\/\/blogs.mathworks.com\/images\/steve\/37\/george.mat<\/span>\r\nload <span style=\"color: #A020F0\">george<\/span>\r\nplot(x,y), axis <span style=\"color: #A020F0\">ij<\/span>, axis <span style=\"color: #A020F0\">equal<\/span>, axis([-1 1 -1 1]), grid <span style=\"color: #A020F0\">on<\/span><\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/37\/maketform_tformfwd_01.png\"> <p>Why \"George\"?  Well, any graduate of the Georgia Institute of Technology will recognize George's full name:  George P. Burdell.\r\n       If you don't know George and are curious, I'm sure Google will turn something up for you.\r\n   <\/p>\r\n   <p>I like to use George to illustrate different affine transforms.<\/p>\r\n   <h2>Scaling example<a name=\"6\"><\/a><\/h2><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">A1 = [2 0 0; 0 2 0; 0 0 1];\r\ntform1 = maketform(<span style=\"color: #A020F0\">'affine'<\/span>, A1);\r\nuv1 = tformfwd(tform1, [x y]);\r\n\r\nsubplot(1,2,1)\r\nplot(x,y), axis <span style=\"color: #A020F0\">ij<\/span>, axis <span style=\"color: #A020F0\">equal<\/span>, axis([-2 2 -2 2]), grid <span style=\"color: #A020F0\">on<\/span>, title(<span style=\"color: #A020F0\">'George'<\/span>)\r\n\r\nsubplot(1,2,2)\r\nplot(uv1(:,1), uv1(:,2)), axis <span style=\"color: #A020F0\">ij<\/span>, axis <span style=\"color: #A020F0\">equal<\/span>, axis([-2 2 -2 2])\r\ngrid <span style=\"color: #A020F0\">on<\/span>\r\ntitle(<span style=\"color: #A020F0\">'Scaled by 2'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/37\/maketform_tformfwd_02.png\"> <h2>Rotation example<a name=\"7\"><\/a><\/h2><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">theta = pi\/4;\r\nA2 = [cos(theta) sin(theta) 0; -sin(theta) cos(theta) 0; 0 0 1];\r\ntform2 = maketform(<span style=\"color: #A020F0\">'affine'<\/span>, A2);\r\nuv2 = tformfwd(tform2, [x y]);\r\n\r\nsubplot(1,2,1)\r\nplot(x,y), axis <span style=\"color: #A020F0\">ij<\/span>, axis <span style=\"color: #A020F0\">equal<\/span>, axis([-2 2 -2 2]), grid <span style=\"color: #A020F0\">on<\/span>, title(<span style=\"color: #A020F0\">'George'<\/span>)\r\n\r\nsubplot(1,2,2)\r\nplot(uv2(:,1), uv2(:,2)), axis <span style=\"color: #A020F0\">ij<\/span>, axis <span style=\"color: #A020F0\">equal<\/span>, axis([-2 2 -2 2])\r\ngrid <span style=\"color: #A020F0\">on<\/span>\r\ntitle(<span style=\"color: #A020F0\">'Rotated by 45\\circ'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/37\/maketform_tformfwd_03.png\"> <h2>Translation example<a name=\"8\"><\/a><\/h2><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">A3 = [1 0 0; 0 1 0; 1 -1 1];\r\ntform3 = maketform(<span style=\"color: #A020F0\">'affine'<\/span>, A3);\r\nuv3 = tformfwd(tform3, [x y]);\r\n\r\nsubplot(1,2,1)\r\nplot(x,y), axis <span style=\"color: #A020F0\">ij<\/span>, axis <span style=\"color: #A020F0\">equal<\/span>, axis([-2 2 -2 2]), grid <span style=\"color: #A020F0\">on<\/span>, title(<span style=\"color: #A020F0\">'George'<\/span>)\r\n\r\nsubplot(1,2,2)\r\nplot(uv3(:,1), uv3(:,2)), axis <span style=\"color: #A020F0\">ij<\/span>, axis <span style=\"color: #A020F0\">equal<\/span>, axis([-2 2 -2 2])\r\ngrid <span style=\"color: #A020F0\">on<\/span>\r\ntitle(<span style=\"color: #A020F0\">'Translated'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/37\/maketform_tformfwd_04.png\"> <script language=\"JavaScript\"> \r\n<!--\r\n    function grabCode_37() {\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='37 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 37';\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_37()\"><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.1<br><\/p>\r\n<\/div>\r\n<!--\r\n37 ##### SOURCE BEGIN #####\r\n%%\r\n% Several Image Processing Toolbox functions related to spatial \r\n% transformations use \"tform\" structures.  A tform structure has data and\r\n% function handles needed for applying a spatial transformation in the\r\n% forward or the inverse direction, and sometimes in both directions.\r\n%\r\n%% maketform\r\n% Function |maketform| has several syntaxes for constructing different\r\n% types of tform structures.  For example, you can construct an affine\r\n% transform by providing an A matrix:\r\n\r\nA = [2 0 0; 0 2 0; 0 0 1];  % Stretch by a factor of 2 in both directions.\r\ntform = maketform('affine', A);\r\n\r\n%% tformfwd\r\n% Toolbox function |tformfwd| transforms points from input space to output\r\n% space.  For example, to apply the affine transform above to the point\r\n% (u,v) = (2,3), do this:\r\n\r\nuv = [2 3];\r\nxy = tformfwd(tform, uv)\r\n\r\n%% tforminv\r\n% Function |tforminv| transforms points from output space to input space.\r\n% If we apply the inverse transformation to the point |xy| computed above,\r\n% we should get back the original point.\r\n\r\nuvp = tforminv(tform, xy)\r\n\r\n%% Introducing George\r\n% The spatial transformation functions |imtransform|, |tformarray|,\r\n% |tformfwd|, |tforminv|, etc., were introduced to Image Processing Toolbox\r\n% version 3 in 2001.  While designing these functions, we almost always had\r\n% input-space output-space diagrams on our whiteboards so we could keep our\r\n% notation straight.  \r\n%\r\n% Because I have no drawing skills, I always drew a crude\r\n% head-and-shoulders outline on these diagrams as the \"image\" being\r\n% transformed.  I started calling this figure \"George.\"  Here's what he\r\n% looks like:\r\n\r\n% https:\/\/blogs.mathworks.com\/steve\/wp-content\/images\/37\/george.mat\r\nload george \r\nplot(x,y), axis ij, axis equal, axis([-1 1 -1 1]), grid on\r\n\r\n%%\r\n% Why \"George\"?  Well, any graduate of the Georgia Institute of Technology\r\n% will recognize George's full name:  George P. Burdell.  If you don't know \r\n% George and are curious, I'm sure Google will turn something up for you.\r\n%\r\n% I like to use George to illustrate different affine transforms.\r\n\r\n%% Scaling example\r\n\r\nA1 = [2 0 0; 0 2 0; 0 0 1];\r\ntform1 = maketform('affine', A1);\r\nuv1 = tformfwd(tform1, [x y]);\r\n\r\nsubplot(1,2,1)\r\nplot(x,y), axis ij, axis equal, axis([-2 2 -2 2]), grid on, title('George')\r\n\r\nsubplot(1,2,2)\r\nplot(uv1(:,1), uv1(:,2)), axis ij, axis equal, axis([-2 2 -2 2])\r\ngrid on\r\ntitle('Scaled by 2')\r\n\r\n%% Rotation example\r\n\r\ntheta = pi\/4;\r\nA2 = [cos(theta) sin(theta) 0; -sin(theta) cos(theta) 0; 0 0 1];\r\ntform2 = maketform('affine', A2);\r\nuv2 = tformfwd(tform2, [x y]);\r\n\r\nsubplot(1,2,1)\r\nplot(x,y), axis ij, axis equal, axis([-2 2 -2 2]), grid on, title('George')\r\n\r\nsubplot(1,2,2)\r\nplot(uv2(:,1), uv2(:,2)), axis ij, axis equal, axis([-2 2 -2 2])\r\ngrid on\r\ntitle('Rotated by 45\\circ')\r\n\r\n%% Translation example\r\n\r\nA3 = [1 0 0; 0 1 0; 1 -1 1];\r\ntform3 = maketform('affine', A3);\r\nuv3 = tformfwd(tform3, [x y]);\r\n\r\nsubplot(1,2,1)\r\nplot(x,y), axis ij, axis equal, axis([-2 2 -2 2]), grid on, title('George')\r\n\r\nsubplot(1,2,2)\r\nplot(uv3(:,1), uv3(:,2)), axis ij, axis equal, axis([-2 2 -2 2])\r\ngrid on\r\ntitle('Translated')\r\n##### SOURCE END ##### 37\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      Several Image Processing Toolbox functions related to spatial transformations use \"tform\" structures.  A tform structure has\r\n         data and function handles needed for applying a... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2006\/02\/14\/spatial-transformations-maketform-tformfwd-and-tforminv\/\">read more >><\/a><\/p>","protected":false},"author":42,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[10],"tags":[50,74,70,62,68,34,72,64,66,52],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/37"}],"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=37"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/37\/revisions"}],"predecessor-version":[{"id":3488,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/37\/revisions\/3488"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=37"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=37"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=37"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}