{"id":1635,"date":"2016-03-21T14:54:22","date_gmt":"2016-03-21T18:54:22","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=1635"},"modified":"2019-11-01T16:35:00","modified_gmt":"2019-11-01T20:35:00","slug":"matlab-image-display-autoscaling-values-with-imshow","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2016\/03\/21\/matlab-image-display-autoscaling-values-with-imshow\/","title":{"rendered":"MATLAB image display \u2013 autoscaling values with imshow"},"content":{"rendered":"\r\n<div class=\"content\"><p>Last week I talked about displaying gray-scale and binary images. In that post, I showed how to control the grayscale range. For example, the call <tt>imshow(I,[0.4 0.6])<\/tt> displays the matrix I as a gray-scale image so that the value 0.4 gets displayed as black, and the value 0.6 gets displayed as white.<\/p><p><a href=\"https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/845693-brett-shoelson\">Brett<\/a>, a MathWorks application engineer and frequent File Exchange contributor, correctly pointed out that I neglected to discuss a common and useful syntax: <tt>imshow(I,[])<\/tt>. This syntax automatically determines the grayscale range of the display based on the minimum and maximum values of <tt>I<\/tt>. It is equivalent to <tt>imshow(I,[min(I(:)) max(I(:))])<\/tt>.<\/p><p>Here's an example. I have a matrix of terrain elevation values, in meters, near Mt. Monadnock in New Hampshire, USA. The peak of Mt. Monadnock is about 960 meters.<\/p><pre class=\"codeinput\">load <span class=\"string\">mt_monadnock<\/span>\r\n<\/pre><p>The matrix is stored in the MAT-file using the variable name <tt>Zc<\/tt>.<\/p><pre class=\"codeinput\">whos <span class=\"string\">Zc<\/span>\r\n<\/pre><pre class=\"codeoutput\">  Name         Size                 Bytes  Class     Attributes\r\n\r\n  Zc        3042x3042            37015056  single              \r\n\r\n<\/pre><pre class=\"codeinput\">min(Zc(:))\r\n<\/pre><pre class=\"codeoutput\">\r\nans =\r\n\r\n  141.5285\r\n\r\n<\/pre><pre class=\"codeinput\">max(Zc(:))\r\n<\/pre><pre class=\"codeoutput\">\r\nans =\r\n\r\n  963.1366\r\n\r\n<\/pre><p>To display these elevation values as a gray-scale image with autoscaling, just call <tt>imshow(Zc,[])<\/tt>:<\/p><pre class=\"codeinput\">imshow(Zc,[])\r\n<\/pre><pre class=\"codeoutput\">Warning: Image is too big to fit on screen; displaying at 33% \r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/imshow_scaling_01.jpg\" alt=\"\"> <p>And, while we're here, I'll point out that you can install any colormap you like once a gray-scale image is displayed. Want some color?<\/p><pre class=\"codeinput\">colormap(parula)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/imshow_scaling_02.jpg\" alt=\"\"> <script language=\"JavaScript\"> <!-- \r\n    function grabCode_832cb0a48d154e2f8430654c17a8fd85() {\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='832cb0a48d154e2f8430654c17a8fd85 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 832cb0a48d154e2f8430654c17a8fd85';\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 2016 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_832cb0a48d154e2f8430654c17a8fd85()\"><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; R2016a<br><\/p><\/div><!--\r\n832cb0a48d154e2f8430654c17a8fd85 ##### SOURCE BEGIN #####\r\n%%\r\n% Last week I talked about displaying gray-scale and binary images. In that\r\n% post, I showed how to control the grayscale range. For example, the call\r\n% |imshow(I,[0.4 0.6])| displays the matrix I as a gray-scale image so that\r\n% the value 0.4 gets displayed as black, and the value 0.6 gets displayed as\r\n% white.\r\n%\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/profile\/authors\/845693-brett-shoelson\r\n% Brett>, a MathWorks application engineer and frequent File Exchange\r\n% contributor, correctly pointed out that I neglected to discuss a common\r\n% and useful syntax: |imshow(I,[])|. This syntax automatically determines\r\n% the grayscale range of the display based on the minimum and maximum values\r\n% of |I|. It is equivalent to |imshow(I,[min(I(:)) max(I(:))])|.\r\n%\r\n% Here's an example. I have a matrix of terrain elevation values, in meters,\r\n% near Mt. Monadnock in New Hampshire, USA. The peak of Mt. Monadnock is\r\n% about 960 meters.\r\n\r\nload mt_monadnock\r\n\r\n%%\r\n% The matrix is stored in the MAT-file using the variable name |Zc|.\r\n\r\nwhos Zc\r\n\r\n%%\r\nmin(Zc(:))\r\n\r\n%%\r\nmax(Zc(:))\r\n\r\n%%\r\n% To display these elevation values as a gray-scale image with autoscaling,\r\n% just call |imshow(Zc,[])|:\r\n\r\nimshow(Zc,[])\r\n\r\n%%\r\n% And, while we're here, I'll point out that you can install any colormap\r\n% you like once a gray-scale image is displayed. Want some color?\r\n\r\ncolormap(parula)\r\n\r\n##### SOURCE END ##### 832cb0a48d154e2f8430654c17a8fd85\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img src=\"https:\/\/blogs.mathworks.com\/steve\/files\/imshow_scaling_02.jpg\" class=\"img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image\" alt=\"\" decoding=\"async\" loading=\"lazy\" \/><\/div><p>\r\nLast week I talked about displaying gray-scale and binary images. In that post, I showed how to control the grayscale range. For example, the call imshow(I,[0.4 0.6]) displays the matrix I as a... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2016\/03\/21\/matlab-image-display-autoscaling-values-with-imshow\/\">read more >><\/a><\/p>","protected":false},"author":42,"featured_media":1638,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[13],"tags":[58,36,362,122,380,1093,306],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/1635"}],"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=1635"}],"version-history":[{"count":2,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/1635\/revisions"}],"predecessor-version":[{"id":1639,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/1635\/revisions\/1639"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media\/1638"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=1635"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=1635"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=1635"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}