{"id":249,"date":"2009-02-18T02:48:38","date_gmt":"2009-02-18T07:48:38","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/2009\/02\/18\/image-overlay-using-transparency\/"},"modified":"2019-10-28T15:26:47","modified_gmt":"2019-10-28T19:26:47","slug":"image-overlay-using-transparency","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2009\/02\/18\/image-overlay-using-transparency\/","title":{"rendered":"Image overlay using transparency"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>Last August I <a href=\"https:\/\/blogs.mathworks.com\/steve\/2008\/08\/20\/image-visualization-using-transparency\/\">posted an example<\/a> showing how to display patch objects transparently over an image. I meant to follow that up with another post showing a couple\r\n      of ways to display one image transparently over another. I was embarrassed to discover recently that I had completely forgotten\r\n      to post the follow-up.\r\n   <\/p>\r\n   <p>So here it is!<\/p>\r\n   <p>Handle Graphics image objects can be displayed transparently. In fact, each individual pixel can be assigned a different level\r\n      of transparency.  This can be used in various ways to view one image on top of another.  In my first example for today, I'll\r\n      use a \"checkerboard\" transparency pattern to view a gray-scale image on top of the original color image.\r\n   <\/p>\r\n   <p>First, display the color image and the gray-scale image together, in the same place.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">rgb = imread(<span style=\"color: #A020F0\">'peppers.png'<\/span>);\r\nimshow(rgb);\r\nI = rgb2gray(rgb);\r\nhold <span style=\"color: #A020F0\">on<\/span>\r\nh = imshow(I); <span style=\"color: #228B22\">% Save the handle; we'll need it later<\/span>\r\nhold <span style=\"color: #A020F0\">off<\/span><\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2009\/transparency_example_01.jpg\"> <p>Not too surprisingly, only the gray-scale is visible.  That's because it's \"covering up\" the color image.  Let's give it a\r\n      \"checkerboard\" transparency pattern, so that some of the pixels are fully opaque, and others are fully transparent.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">[M,N] = size(I);\r\nblock_size = 50;\r\nP = ceil(M \/ block_size);\r\nQ = ceil(N \/ block_size);\r\nalpha_data = checkerboard(block_size, P, Q) &gt; 0;\r\nalpha_data = alpha_data(1:M, 1:N);\r\nset(h, <span style=\"color: #A020F0\">'AlphaData'<\/span>, alpha_data);<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2009\/transparency_example_02.jpg\"> <p>Now we can see some of both images.  This visualization technique is often used to evaluate image fusion algorithms.<\/p>\r\n   <h3>Using image data to control transparency<a name=\"4\"><\/a><\/h3>\r\n   <p>My second transparency example gets a bit more creative.  I'll display an image that's a solid color, but I'll use another\r\n      data set to vary the solid color image's transparency on a pixel-by-pixel basis.\r\n   <\/p>\r\n   <p>Here's a digital elevation model (DEM) of Peppercorn Hill and North Pond in Massachusetts.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">E = imread(<span style=\"color: #A020F0\">'peppercorn_hill.png'<\/span>);\r\nimshow(E, <span style=\"color: #A020F0\">'InitialMag'<\/span>, <span style=\"color: #A020F0\">'fit'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2009\/transparency_example_03.jpg\"> <p>The bright blob at the upper left is Peppercorn Hill, and the flat, dark plateau in the upper middle is North Pond.<\/p>\r\n   <p>Below is an \"influence map.\" This is a visualization of down-hill water flow, starting from the peak of Peppercorn Hill.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">I = imread(<span style=\"color: #A020F0\">'peppercorn_hill_influence_map.png'<\/span>);\r\nimshow(I, <span style=\"color: #A020F0\">'InitialMag'<\/span>, <span style=\"color: #A020F0\">'fit'<\/span>)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2009\/transparency_example_04.jpg\"> <p>It's difficult to interpret the influence map image on its own, apart from the original DEM.  Let's visualize the two images\r\n      together as follows:\r\n   <\/p>\r\n<ol>\r\n   <li>Display the original DEM image.<\/li>\r\n   <li>Display a solid green \"image\" on top of the original image.<\/li>\r\n   <li>Use the influence map pixels to control the transparency of   each pixel of the green image.<\/li>\r\n<\/ol>\r\n<pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">imshow(E, <span style=\"color: #A020F0\">'InitialMag'<\/span>, <span style=\"color: #A020F0\">'fit'<\/span>)\r\n\r\n<span style=\"color: #228B22\">% Make a truecolor all-green image.<\/span>\r\ngreen = cat(3, zeros(size(E)), ones(size(E)), zeros(size(E)));\r\nhold <span style=\"color: #A020F0\">on<\/span>\r\nh = imshow(green);\r\nhold <span style=\"color: #A020F0\">off<\/span><\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2009\/transparency_example_05.jpg\"> <pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">% Use our influence map image as the AlphaData for the solid<\/span>\r\n<span style=\"color: #228B22\">% green image.<\/span>\r\nset(h, <span style=\"color: #A020F0\">'AlphaData'<\/span>, I)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2009\/transparency_example_06.jpg\"> <p>Now it's easy to understand the water flow in the context of the original DEM image. We can see that the water flows from\r\n      the peak into the pond, then out the southern end of the pond.\r\n   <\/p>\r\n   <p>So there you go, better late than never.  Two more image visualization techniques to add to your bag of tricks.<\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_35456df206184f8f889acff7a74616f1() {\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='35456df206184f8f889acff7a74616f1 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 35456df206184f8f889acff7a74616f1';\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 = '';\r\n        copyright = 'Copyright 2009 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-->\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_35456df206184f8f889acff7a74616f1()\"><span style=\"font-size: x-small;        font-style: italic;\">Get \r\n            the MATLAB code \r\n            <noscript>(requires JavaScript)<\/noscript><\/span><\/a><br><br>\r\n      Published with MATLAB&reg; 7.7<br><\/p>\r\n<\/div>\r\n<!--\r\n35456df206184f8f889acff7a74616f1 ##### SOURCE BEGIN #####\r\n%%\r\n% Last August I \r\n% <https:\/\/blogs.mathworks.com\/steve\/2008\/08\/20\/image-visualization-using-transparency\/ \r\n% posted an example> showing how to display patch objects\r\n% transparently over an image. I meant to follow that up with another post\r\n% showing a couple of ways to display one image transparently over another. I\r\n% was embarrassed to discover recently that I had completely forgotten to post\r\n% the follow-up.\r\n%\r\n% So here it is!\r\n%\r\n% Handle Graphics image objects can be displayed transparently. In fact, each\r\n% individual pixel can be assigned a different level of transparency.  This can\r\n% be used in various ways to view one image on top of another.  In my first\r\n% example for today, I'll use a \"checkerboard\" transparency pattern to view a\r\n% gray-scale image on top of the original color image.\r\n%\r\n% First, display the color image and the gray-scale image \r\n% together, in the same place.\r\n\r\nrgb = imread('peppers.png');\r\nimshow(rgb);\r\nI = rgb2gray(rgb);\r\nhold on\r\nh = imshow(I); % Save the handle; we'll need it later\r\nhold off\r\n\r\n%%\r\n% Not too surprisingly, only the gray-scale is visible.  That's\r\n% because it's \"covering up\" the color image.  Let's give it a\r\n% \"checkerboard\" transparency pattern, so that some of the pixels\r\n% are fully opaque, and others are fully transparent.\r\n\r\n[M,N] = size(I);\r\nblock_size = 50;\r\nP = ceil(M \/ block_size);\r\nQ = ceil(N \/ block_size);\r\nalpha_data = checkerboard(block_size, P, Q) > 0;\r\nalpha_data = alpha_data(1:M, 1:N);\r\nset(h, 'AlphaData', alpha_data);\r\n\r\n%%\r\n% Now we can see some of both images.  This visualization\r\n% technique is often used to evaluate image fusion algorithms.\r\n\r\n%% Using image data to control transparency\r\n% My second transparency example gets a bit more creative.  I'll display an\r\n% image that's a solid color, but I'll use another data set to vary the solid\r\n% color image's transparency on a pixel-by-pixel basis.\r\n%\r\n% Here's a digital elevation model (DEM) of Peppercorn Hill and\r\n% North Pond in Massachusetts.\r\n\r\nE = imread('peppercorn_hill.png');\r\nimshow(E, 'InitialMag', 'fit')\r\n\r\n%%\r\n% The bright blob at the upper left is Peppercorn Hill, and the\r\n% flat, dark plateau in the upper middle is North Pond.\r\n%\r\n% Below is an \"influence map.\" This is a visualization of down-hill water\r\n% flow, starting from the peak of Peppercorn Hill.\r\n\r\nI = imread('peppercorn_hill_influence_map.png');\r\nimshow(I, 'InitialMag', 'fit')\r\n\r\n%%\r\n% It's difficult to interpret the influence map image on its own,\r\n% apart from the original DEM.  Let's visualize the two images\r\n% together as follows:\r\n%\r\n% # Display the original DEM image.\r\n% # Display a solid green \"image\" on top of the original image.\r\n% # Use the influence map pixels to control the transparency of\r\n%   each pixel of the green image.\r\n\r\nimshow(E, 'InitialMag', 'fit')\r\n\r\n% Make a truecolor all-green image.\r\ngreen = cat(3, zeros(size(E)), ones(size(E)), zeros(size(E)));\r\nhold on\r\nh = imshow(green);\r\nhold off\r\n\r\n%%\r\n\r\n% Use our influence map image as the AlphaData for the solid\r\n% green image.\r\nset(h, 'AlphaData', I)\r\n\r\n%%\r\n% Now it's easy to understand the water flow in the context of the original DEM\r\n% image. We can see that the water flows from the peak into the pond, then out\r\n% the southern end of the pond.\r\n\r\n%%\r\n% So there you go, better late than never.  Two more image visualization\r\n% techniques to add to your bag of tricks.\r\n##### SOURCE END ##### 35456df206184f8f889acff7a74616f1\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   Last August I posted an example showing how to display patch objects transparently over an image. I meant to follow that up with another post showing a couple\r\n      of ways to display one image... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2009\/02\/18\/image-overlay-using-transparency\/\">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":[46,539,182,90,76,36,288,104,190,130],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/249"}],"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=249"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/249\/revisions"}],"predecessor-version":[{"id":3615,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/249\/revisions\/3615"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=249"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=249"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=249"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}