{"id":35,"date":"2006-02-07T07:00:57","date_gmt":"2006-02-07T12:00:57","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=35"},"modified":"2019-10-21T17:42:11","modified_gmt":"2019-10-21T21:42:11","slug":"spatial-transformations-affine","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2006\/02\/07\/spatial-transformations-affine\/","title":{"rendered":"Spatial transformations: Affine"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>To explore spatial transformations of images, we need a simple, nontrivial, and useful transformation.  The affine transformation\r\n      fits the bill.\r\n   <\/p>\r\n   <p>Here's the basic affine equation:<\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/35\/affine_background_eq1070453.png\"> <\/p>\r\n   <p>or:<\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/35\/affine_background_eq10016.gif\"> <\/p>\r\n   <p>Already we've encountered another common notational confusion.  Sometimes a user will contact us and say \"imtransform shrank\r\n      my image in the horizontal direction, when it should have shrunk it in the vertical direction. Is this a bug in imtransform?\"\r\n       No, it's not a bug.  The cause is that the user tried an A matrix from a source that defines the affine equation this way:\r\n   <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/35\/affine_background_eq586704.png\"> <\/p>\r\n   <p>The A matrices in these two formulas are not the same; one is the transpose of the other.<\/p>\r\n   <p>Several common spatial transformations applied to images can be expressed in terms of an affine equation, including scaling:<\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/35\/affine_background_eq416680.png\"> <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/35\/affine_background_eq63377.png\"> <\/p>\r\n   <p>Rotation:<\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/35\/affine_background_eq849782.png\"> <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/35\/affine_background_eq303645.png\"> <\/p>\r\n   <p>And translation:<\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/35\/affine_background_eq489800.png\"> <\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/35\/affine_background_eq101687.png\"> <\/p>\r\n   <p>OK, enough background notation and equations.  Next time we'll jump into MATLAB, construct some affine transformations, and\r\n      apply them to points.\r\n   <\/p>\r\n <script language=\"JavaScript\"> \r\n<!--\r\n    function grabCode_35() {\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='35 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 35';\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_35()\"><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\n35 ##### SOURCE BEGIN #####\r\n%%\r\n% To explore spatial transformations of images, we need a simple, \r\n% nontrivial, and useful transformation.  The affine transformation fits the\r\n% bill.\r\n%\r\n% Here's the basic affine equation:\r\n%\r\n% $$[x\\ y\\ 1] = [u\\ v\\ 1] \\left[ \\begin{array}{ccc} a_{11}\\ & a_{12}\\ & 0\\ \\\\ a_{12}\\ & a_{22}\\ & 0\\ \\\\\r\n% a_{31}\\ & a_{32}\\ & 1\\ \\end{array} \\right]$$\r\n%\r\n% or:\r\n%\r\n% $$[x\\ y\\ 1] = [u\\ v\\ 1]\\ A$$\r\n%\r\n% Already we've encountered another common notational confusion.  Sometimes\r\n% a user will contact us and say \"imtransform shrank my image in the\r\n% horizontal direction, when it should have shrunk it in the vertical\r\n% direction. Is this a bug in imtransform?\"  No, it's not a bug.  The\r\n% cause is that the user tried an A matrix from a source that\r\n% defines the affine equation this way:\r\n%\r\n% $$\\left[ \\begin{array}{c} x \\\\ y \\\\ 1 \\end{array} \\right] = \r\n% A \\left[ \\begin{array}{c} u \\\\ v \\\\ 1 \\end{array} \\right]$$\r\n%\r\n% The A matrices in these two formulas are not the same; one is the\r\n% transpose of the other.\r\n%\r\n% Several common spatial transformations applied to images can be expressed\r\n% in terms of an affine equation, including scaling:\r\n%\r\n% $$A = \\left[ \\begin{array}{ccc} \r\n% s_{x}\\ & 0\\ & 0\\ \\\\\r\n% 0\\ & s_{y}\\ & 0\\ \\\\\r\n% 0\\ & 0\\ & 1\\ \r\n% \\end{array} \\right]$$\r\n%\r\n% $$\\begin{array}{l}\r\n% x = s_x u \\\\\r\n% y = s_y v\r\n% \\end{array}$$\r\n%\r\n% Rotation:\r\n%\r\n% $$A = \\left[ \\begin{array}{ccc} \r\n% \\cos\\theta\\ & \\sin\\theta\\ & 0\\ \\\\\r\n% -\\sin\\theta\\ & \\cos\\theta\\ & 0\\ \\\\\r\n% 0\\ & 0\\ & 1\\ \r\n% \\end{array} \\right]$$\r\n%\r\n% $$\\begin{array}{l}\r\n% x = u\\cos\\theta - v\\sin\\theta \\\\\r\n% y = u\\sin\\theta + v\\cos\\theta\r\n% \\end{array}$$\r\n%\r\n% And translation:\r\n%\r\n% $$A = \\left[ \\begin{array}{ccc} \r\n% 1\\ & 0\\ & 0\\ \\\\\r\n% 0\\ & 1\\ & 0\\ \\\\\r\n% \\delta_x\\ & \\delta_y\\ & 1\\ \r\n% \\end{array} \\right]$$\r\n%\r\n% $$\\begin{array}{l}\r\n% x = u+\\delta_x \\\\\r\n% y = v+\\delta_y\r\n% \\end{array}$$\r\n%\r\n% OK, enough background notation and equations.  Next time we'll jump into\r\n% MATLAB, construct some affine transformations, and apply them to points.\r\n\r\n\r\n##### SOURCE END ##### 35\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   To explore spatial transformations of images, we need a simple, nontrivial, and useful transformation.  The affine transformation\r\n      fits the bill.\r\n   \r\n   Here's the basic affine... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2006\/02\/07\/spatial-transformations-affine\/\">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":[44],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/35"}],"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=35"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/35\/revisions"}],"predecessor-version":[{"id":3486,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/35\/revisions\/3486"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=35"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=35"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=35"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}