{"id":3897,"date":"2019-12-03T18:08:12","date_gmt":"2019-12-03T23:08:12","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=3897"},"modified":"2019-12-04T15:43:09","modified_gmt":"2019-12-04T20:43:09","slug":"how-to-overlay-a-color-on-an-image-using-a-mask","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2019\/12\/03\/how-to-overlay-a-color-on-an-image-using-a-mask\/","title":{"rendered":"How to Overlay a Color on an Image Using a Mask"},"content":{"rendered":"<div class=\"content\"><p>In my first year of writing this blog (2006!), I <a href=\"https:\/\/blogs.mathworks.com\/steve\/2006\/03\/28\/image-overlays\/\">showed how to overlay a color onto an image based on a mask<\/a>. This was the example I gave back then:<\/p><p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/image-overlay-operation-600w.png\" alt=\"\"> <\/p><p>I wrote a function called <tt>imoverlay<\/tt> to handle all the details, and I <a href=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/10502-image-overlay\">submitted it to the File Exchange<\/a>.<\/p><p>About three and a half years ago, in R2016a, the Image Processing Toolbox development team shipped a similar function, with the same name. I want to encourage everyone to use that new function.<\/p><p>Since my File Exchange version is still being frequently downloaded, I decided not to remove it (yet). Instead, I updated the submission to change the function name to <tt>imoverlay_old<\/tt> and to include a note about the new function in the Image Processing Toolbox.<\/p><p>Here's a new example. I'm going to use <tt>imoverlay<\/tt> (the new one) to overlay the output of the Canny edge detector on the peppers.png image. So that the overlay is visible everywhere, I will use <tt>imoverlay<\/tt> twice, with contrasting colors.<\/p><pre class=\"codeinput\">A = imread(<span class=\"string\">'peppers.png'<\/span>);\r\nimshow(A,<span class=\"string\">'InitialMagnification'<\/span>,200)\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/toolbox_image_overlay_01.png\" alt=\"\"> <pre class=\"codeinput\">E = edge(rgb2gray(A),<span class=\"string\">'canny'<\/span>);\r\nimshow(E,<span class=\"string\">'InitialMagnification'<\/span>,200);\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/toolbox_image_overlay_02.png\" alt=\"\"> <p>Make a dilated (thickened) version of the edge mask and use it for the first overlay (in yellow).<\/p><pre class=\"codeinput\">Ed = imdilate(E,ones(3,3));\r\nB = imoverlay(A,Ed,<span class=\"string\">'y'<\/span>);\r\n<\/pre><p>Now use the original edge detection output for the second overlay (in blue).<\/p><pre class=\"codeinput\">C = imoverlay(B,E,<span class=\"string\">'b'<\/span>);\r\nimshow(C,<span class=\"string\">'InitialMagnification'<\/span>,200);\r\n<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/toolbox_image_overlay_03.png\" alt=\"\"> <p>If you've been using my version of <tt>imoverlay<\/tt> and you have R2016a or later, consider switching to the one that is now included with the Image Processing Toolbox.<\/p><script language=\"JavaScript\"> <!-- \r\n    function grabCode_4d6035cd221d475dadc745ef8d5d45d5() {\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='4d6035cd221d475dadc745ef8d5d45d5 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 4d6035cd221d475dadc745ef8d5d45d5';\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 2019 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_4d6035cd221d475dadc745ef8d5d45d5()\"><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; R2019b<br><\/p><\/div><!--\r\n4d6035cd221d475dadc745ef8d5d45d5 ##### SOURCE BEGIN #####\r\n%%\r\n% In my first year of writing this blog (2006!), I <https:\/\/blogs.mathworks.com\/steve\/2006\/03\/28\/image-overlays\/ \r\n% showed how to overlay a\r\n% color onto an image based on a mask>. This was the example I gave back\r\n% then:\r\n%\r\n% <<https:\/\/blogs.mathworks.com\/steve\/files\/image-overlay-operation-600w.png>>\r\n%\r\n% I wrote a function called |imoverlay| to handle all the details, and I\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/10502-image-overlay \r\n% submitted it to the File Exchange>.\r\n%\r\n% About three and a half years ago, in R2016a, the Image Processing Toolbox\r\n% development team shipped a similar function, with the same name. I want\r\n% to encourage everyone to use that new function.\r\n%\r\n% Since my File Exchange version is still being frequently downloaded, I\r\n% decided not to remove it (yet). Instead, I updated the submission to\r\n% change the function name to |imoverlay_old| and to include a note about\r\n% the new function in the Image Processing Toolbox.\r\n%\r\n% Here's a new example. I'm going to use |imoverlay| (the new one) to\r\n% overlay the output of the Canny edge detector on the peppers.png image.\r\n% So that the overlay is visible everywhere, I will use |imoverlay| twice,\r\n% with contrasting colors.\r\n\r\nA = imread('peppers.png');\r\nimshow(A,'InitialMagnification',200)\r\n\r\n%%\r\nE = edge(rgb2gray(A),'canny');\r\nimshow(E,'InitialMagnification',200);\r\n\r\n%%\r\n% Make a dilated (thickened) version of the edge mask and use it for the\r\n% first overlay (in yellow).\r\n\r\nEd = imdilate(E,ones(3,3));\r\nB = imoverlay(A,Ed,'y');\r\n\r\n%%\r\n% Now use the original edge detection output for the second overlay (in\r\n% blue).\r\n\r\nC = imoverlay(B,E,'b');\r\nimshow(C,'InitialMagnification',200);\r\n\r\n%%\r\n% If you've been using my version of |imoverlay| and you have R2016a or\r\n% later, consider switching to the one that is now included with the Image\r\n% Processing Toolbox.\r\n\r\n##### SOURCE END ##### 4d6035cd221d475dadc745ef8d5d45d5\r\n-->","protected":false},"excerpt":{"rendered":"<div class=\"overview-image\"><img decoding=\"async\"  class=\"img-responsive\" src=\"https:\/\/blogs.mathworks.com\/steve\/files\/toolbox_image_overlay_03.png\" onError=\"this.style.display ='none';\" \/><\/div><p>In my first year of writing this blog (2006!), I showed how to overlay a color onto an image based on a mask. This was the example I gave back then: I wrote a function called imoverlay to handle all... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2019\/12\/03\/how-to-overlay-a-color-on-an-image-using-a-mask\/\">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":[228,124,737,76,36,288,104],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/3897"}],"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=3897"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/3897\/revisions"}],"predecessor-version":[{"id":3899,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/3897\/revisions\/3899"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=3897"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=3897"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=3897"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}