{"id":19,"date":"2006-01-13T07:00:37","date_gmt":"2006-01-13T12:00:37","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=19"},"modified":"2019-10-21T17:38:46","modified_gmt":"2019-10-21T21:38:46","slug":"synthesizing-images-using-simple-equations","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2006\/01\/13\/synthesizing-images-using-simple-equations\/","title":{"rendered":"Synthesizing images using simple equations"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <introduction>\r\n      <p>In 1988, Gerard Holzmann of AT&amp;T Bell Labs (back when there was such a thing) published a fun book called Beyond Photography:\r\n         The Digital Darkroom (<a href=\"\"><\/a>. The book shows how some simple math on pixel values and coordinates can transform images in fascinating ways.\r\n      <\/p>\r\n      <p>In the first part of the book, Holzmann shows how to synthesize interesting images using functions of both Cartesian and polar\r\n         coordinates.  Let's see how to do that in MATLAB.\r\n      <\/p>\r\n      <p>MATLAB functions featured: <tt>meshgrid<\/tt>, <tt>cart2pol<\/tt><\/p>\r\n      <p>Image Processing Toolbox functions featured: <tt>imshow<\/tt><\/p>\r\n   <\/introduction>\r\n   <h2>Contents<\/h2>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Start with meshgrid<\/a><\/li>\r\n         <li><a href=\"#2\">Concentric rings<\/a><\/li>\r\n         <li><a href=\"#3\">Using polar coordinates<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h2>Start with meshgrid<a name=\"1\"><\/a><\/h2>\r\n   <p>The MATLAB function <tt>meshgrid<\/tt> is extremely useful for computing a function of two Cartesian coordinates, and you can make some interesting images this\r\n      way.\r\n   <\/p>\r\n   <p><tt>meshgrid<\/tt> is kind of hard to explain in words.  It's easier to just look at what it does.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">x = 1:3;\r\ny = 10:14;\r\n[xx, yy] = meshgrid(x, y)<\/pre><pre style=\"font-style:oblique\">\r\nxx =\r\n\r\n     1     2     3\r\n     1     2     3\r\n     1     2     3\r\n     1     2     3\r\n     1     2     3\r\n\r\n\r\nyy =\r\n\r\n    10    10    10\r\n    11    11    11\r\n    12    12    12\r\n    13    13    13\r\n    14    14    14\r\n\r\n<\/pre><h2>Concentric rings<a name=\"2\"><\/a><\/h2>\r\n   <p>Let's make an image out of the equation:<\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/19\/image_synthesis_eq4879.png\"\/> <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">x = linspace(-pi, pi, 201);\r\n<span style=\"color: #228B22\">% If you pass meshgrid only one vector, it uses that vector for both the x<\/span>\r\n<span style=\"color: #228B22\">% and the y coordinates.<\/span>\r\n[xx,yy] = meshgrid(x);\r\nA = 10;\r\nI = sin(A*(xx.^2 + yy.^2));\r\n\r\n<span style=\"color: #228B22\">% Specify the range -1 to 1 when displaying the image.<\/span>\r\nimshow(I, [-1 1])<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/19\/image_synthesis_01.png\"\/> <h2>Using polar coordinates<a name=\"3\"><\/a><\/h2>\r\n   <p>If you want to construct an image from a function of polar coordinates, use <tt>cart2pol<\/tt> in conjunction with <tt>meshgrid<\/tt>.\r\n   <\/p>\r\n   <p>(The interesting patterns you see in the center of the image below result from ''aliasing,'' but that's a topic for another\r\n      day.)\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">[xx,yy] = meshgrid(-125:125);\r\n[theta,R] = cart2pol(xx,yy);\r\nI = sin(50*theta);\r\nimshow(I, [-1 1])<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/19\/image_synthesis_02.png\"\/> <script language=\"JavaScript\"> \r\n<!--\r\n    function grabCode_19() {\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='19 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 19';\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_19()\"><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\n19 ##### SOURCE BEGIN #####\r\n%% Synthesizing images using simple equations\r\n% In 1988, Gerard Holzmann of AT&T Bell Labs (back when there was such a\r\n% thing) published a fun book called Beyond Photography: The Digital\r\n% Darkroom (. The book shows how some simple\r\n% math on pixel values and coordinates can transform images in fascinating\r\n% ways.\r\n%\r\n% In the first part of the book, Holzmann shows how to synthesize interesting\r\n% images using functions of both Cartesian and polar coordinates.  Let's see how\r\n% to do that in MATLAB.\r\n%\r\n% MATLAB functions featured: |meshgrid|, |cart2pol|\r\n%\r\n% Image Processing Toolbox functions featured: |imshow|\r\n\r\n%% Start with meshgrid\r\n% The MATLAB function |meshgrid| is extremely useful for computing a\r\n% function of two Cartesian coordinates, and you can make some interesting\r\n% images this way.\r\n%\r\n% |meshgrid| is kind of hard to explain in words.  It's easier to just look \r\n% at what it does.\r\n\r\nx = 1:3;\r\ny = 10:14;\r\n[xx, yy] = meshgrid(x, y)\r\n\r\n%% Concentric rings\r\n% Let's make an image out of the equation:\r\n%\r\n% $$sin(A(x^2 + y^2))$$\r\n% \r\n\r\nx = linspace(-pi, pi, 201);\r\n% If you pass meshgrid only one vector, it uses that vector for both the x\r\n% and the y coordinates.\r\n[xx,yy] = meshgrid(x);\r\nA = 10;\r\nI = sin(A*(xx.^2 + yy.^2));\r\n\r\n% Specify the range -1 to 1 when displaying the image.\r\nimshow(I, [-1 1])\r\n\r\n%% Using polar coordinates\r\n% If you want to construct an image from a function of polar coordinates,\r\n% use |cart2pol| in conjunction with |meshgrid|.\r\n%\r\n% (The interesting patterns you see in the center of the image below result\r\n% from ''aliasing,'' but that's a topic for another day.)\r\n\r\n[xx,yy] = meshgrid(-125:125);\r\n[theta,R] = cart2pol(xx,yy);\r\nI = sin(50*theta);\r\nimshow(I, [-1 1])\r\n\r\n##### SOURCE END ##### 19\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   \r\n      In 1988, Gerard Holzmann of AT&amp;T Bell Labs (back when there was such a thing) published a fun book called Beyond Photography:\r\n         The Digital Darkroom (. The book shows how... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2006\/01\/13\/synthesizing-images-using-simple-equations\/\">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":[38,36,32,30,34],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/19"}],"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=19"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/19\/revisions"}],"predecessor-version":[{"id":3480,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/19\/revisions\/3480"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=19"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=19"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=19"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}