{"id":49,"date":"2006-03-28T07:37:20","date_gmt":"2006-03-28T12:37:20","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=49"},"modified":"2021-06-02T22:02:16","modified_gmt":"2021-06-03T02:02:16","slug":"image-overlays","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2006\/03\/28\/image-overlays\/","title":{"rendered":"Image overlays"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n\r\n<div class=\"alert alert-info\">\r\n<span class=\"alert_icon icon-alert-info-reverse\"><\/span>\r\n<p class=\"alert_heading\"><strong>Note<\/strong><\/p>\r\n<p>For new or updated information about this topic, see: <a href=\"https:\/\/blogs.mathworks.com\/steve\/2019\/12\/03\/how-to-overlay-a-color-on-an-image-using-a-mask\/\">How to Overlay a Color on an Image Using a Mask (03-Dec-2019)<\/a><\/p>\r\n<\/div>\r\n   <h3>Contents<\/h3>\r\n   <div>\r\n      <ul>\r\n         <li><a href=\"#1\">Image Overlay<\/a><\/li>\r\n         <li><a href=\"#4\">Procedure<\/a><\/li>\r\n         <li><a href=\"#7\">File Exchange - imoverlay.m<\/a><\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <h3>Image Overlay<a name=\"1\"><\/a><\/h3>\r\n   <p>In response my post on <a href=\"https:\/\/blogs.mathworks.com\/steve\/?p=36\">\"Tracing George\"<\/a>, blog reader Riccardo asked me how to overlay the thinned binary image on top of the original image, using a different color.  That is,\r\n      given George:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">url = <span style=\"color: #A020F0\">'https:\/\/blogs.mathworks.com\/images\/steve\/36\/george.jpg'<\/span>;\r\nI = imread(url);\r\nimshow(I)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/49\/image_overlay_01.png\"> <p>and thinned George, using the method given in my previous post:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">threshold = graythresh(I);\r\nbw = im2bw(I, threshold);\r\nbw2 = ~bw;\r\nbw3 = bwmorph(bw2, <span style=\"color: #A020F0\">'thin'<\/span>, inf);\r\n\r\nimshow(bw3)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/49\/image_overlay_02.png\"> <p>how do you produce a color overlay like this:<\/p>\r\n   <p><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/49\/sample_overlay.png\"> <\/p>\r\n   <h3>Procedure<a name=\"4\"><\/a><\/h3>\r\n   <p>We want to produce an RGB (truecolor) image.  Everywhere the binary image mask is zero, it should look like the original image.\r\n       Everywhere the binary image mask is nonzero, it should have the specified color.\r\n   <\/p>\r\n   <p>Start by making three copies of the original image, one for each channel of the desired RGB image.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">out_red   = I;\r\nout_green = I;\r\nout_blue  = I;<\/pre><p>Second, replace the masked pixels in each channel image with the desired color value.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">out_red(bw3)   = 255;   <span style=\"color: #228B22\">% Class of out_red is uint8, so use 255 instead of 1.<\/span>\r\nout_green(bw3) = 255;\r\nout_blue(bw3)  = 0;<\/pre><p>Finally, concatenate the channel images along the third dimension to produce the desired overlay image.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">out = cat(3, out_red, out_green, out_blue);\r\nimshow(out)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/49\/image_overlay_03.png\"> <h3>File Exchange - imoverlay.m<a name=\"7\"><\/a><\/h3>\r\n   <p>I've often thought that this image overlay operation would be a good utility for the <a title=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadCategory.do (link no longer works)\">MATLAB File Exchange<\/a>.  Riccardo's question finally pushed me to write and submit <a title=\"https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadFile.do?objectId=10502&amp;objectType=file (link no longer works)\">imoverlay.m<\/a>. I hope some of you find it useful.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">help <span style=\"color: #A020F0\">imoverlay<\/span><\/pre><pre style=\"font-style:oblique\"> IMOVERLAY Create a mask-based image overlay.\r\n    OUT = IMOVERLAY(IN, MASK, COLOR) takes an input image, IN, and a binary\r\n    image, MASK, and produces an output image whose pixels in the MASK\r\n    locations have the specified COLOR.\r\n \r\n    IN should be a grayscale or an RGB image of class uint8, uint16, int16,\r\n    logical, double, or single.\r\n \r\n    MASK should be a two-dimensional logical matrix.\r\n \r\n    COLOR should be a 1-by-3 vector of values in the range [0, 1].  [0 0 0]\r\n    is black, and [1 1 1] is white.\r\n \r\n    OUT is a uint8 RGB image.\r\n \r\n    Example\r\n    -------\r\n    Overlay edge detection result in green over the original image.\r\n        \r\n        I = imread('cameraman.tif');\r\n        bw = edge(I, 'canny');\r\n        rgb = imoverlay(I, bw, [0 1 0]);\r\n        imshow(rgb)\r\n\r\n<\/pre><script language=\"JavaScript\"> \r\n<!--\r\n    function grabCode_49() {\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='49 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 49';\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_49()\"><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.2<br><\/p>\r\n<\/div>\r\n<!--\r\n49 ##### SOURCE BEGIN #####\r\n%% Image Overlay\r\n% In response my post on <https:\/\/blogs.mathworks.com\/steve\/?p=36 \"Tracing\r\n% George\">, Riccardo asked me how to overlay the thinned binary image on top of\r\n% the original image, using a different color.  That is, given George:\r\n\r\nurl = 'https:\/\/blogs.mathworks.com\/steve\/wp-content\/images\/36\/george.jpg';\r\nI = imread(url);\r\nimshow(I)\r\n\r\n%% \r\n% and thinned George, using the method given in my previous post:\r\n\r\nthreshold = graythresh(I);\r\nbw = im2bw(I, threshold);\r\nbw2 = ~bw;\r\nbw3 = bwmorph(bw2, 'thin', inf);\r\n\r\nimshow(bw3)\r\n\r\n%%\r\n% how do you produce a color overlay like this:\r\n%\r\n% <<https:\/\/blogs.mathworks.com\/images\/steve\/49\/sample_overlay.png>>\r\n\r\n%% Procedure\r\n% We want to produce an RGB (truecolor) image.  Everywhere the binary image\r\n% mask is zero, it should look like the original image.  Everywhere the\r\n% binary image mask is nonzero, it should have the specified color.\r\n%\r\n% Start by making three copies of the original image, one for each channel\r\n% of the desired truecolor image.\r\n\r\nout_red   = I;\r\nout_green = I;\r\nout_blue  = I;\r\n\r\n%%\r\n% Second, replace the masked pixels in each channel image with the desired\r\n% color value.\r\n\r\nout_red(bw3)   = 255;   % Class of out_red is uint8, so use 255 instead of 1.\r\nout_green(bw3) = 255; \r\nout_blue(bw3)  = 0;\r\n\r\n%%\r\n% Finally, concatenate the channel images along the third dimension to\r\n% produce the desired overlay image.\r\n\r\nout = cat(3, out_red, out_green, out_blue);\r\nimshow(out)\r\n\r\n%% File Exchange - imoverlay.m\r\n% I've often thought that this image overlay operation would be a good\r\n% utility for the <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadCategory.do \r\n% MATLAB File Exchange>.  Riccardo's question finally pushed me to write\r\n% and submit\r\n% <https:\/\/www.mathworks.com\/matlabcentral\/fileexchange\/loadFile.do?objectId=10502&objectType=file \r\n% imoverlay.m>. I hope some of you find it useful.\r\n\r\nhelp imoverlay\r\n\r\n\r\n\r\n##### SOURCE END ##### 49\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n\r\n\r\n\r\nNote\r\nFor new or updated information about this topic, see: How to Overlay a Color on an Image Using a Mask (03-Dec-2019)\r\n\r\n   Contents\r\n   \r\n      \r\n         Image Overlay\r\n      ... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2006\/03\/28\/image-overlays\/\">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":[86,46,82,84,76,36],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/49"}],"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=49"}],"version-history":[{"count":4,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/49\/revisions"}],"predecessor-version":[{"id":4614,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/49\/revisions\/4614"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=49"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=49"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=49"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}