{"id":223,"date":"2008-08-20T15:34:19","date_gmt":"2008-08-20T19:34:19","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/2008\/08\/20\/image-visualization-using-transparency\/"},"modified":"2019-10-28T09:39:53","modified_gmt":"2019-10-28T13:39:53","slug":"image-visualization-using-transparency","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2008\/08\/20\/image-visualization-using-transparency\/","title":{"rendered":"Image visualization using transparency"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>Transparent graphics objects can be used effectively to visualize image processing concepts.  Two particularly useful techniques\r\n      are:\r\n   <\/p>\r\n   <div>\r\n      <ul>\r\n         <li>Highlighting image regions with transparent patches<\/li>\r\n         <li>Displaying one image transparently over another<\/li>\r\n      <\/ul>\r\n   <\/div>\r\n   <p>Today I'll show how to highlight image regions with patches.  For this example I'll use the <tt>'Extrema'<\/tt> measurement returned by regionprops. The extrema for a given object are eight points: the left-most pixel on the bottom, the right-most pixel on the bottom,\r\n      the top-most pixel on the right, the bottom-most pixel on the right, and so on.\r\n   <\/p>\r\n   <p>I'll start with the rice image, segmenting it using techniques I've shown before.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">I = imread(<span style=\"color: #A020F0\">'rice.png'<\/span>);\r\nimshow(I)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2008\/transparency_example_1_01.jpg\"> <p>Even out the illumination with the tophat operator, threshold, and then clean up the thresholded image a bit.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">I2 = imtophat(I, ones(15, 15));\r\nbw = im2bw(I2, graythresh(I2));\r\nbw2 = bwareaopen(bw, 5);\r\nbw3 = imclearborder(bw2);\r\nimshow(bw3)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2008\/transparency_example_1_02.jpg\"> <p>Label the binary objects and compute the extrema.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">L = bwlabel(bw3);\r\ns = regionprops(L, <span style=\"color: #A020F0\">'Extrema'<\/span>);<\/pre><p>Each object has 8 extrema points associated with it.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">s(1).Extrema<\/pre><pre style=\"font-style:oblique\">\r\nans =\r\n\r\n   11.5000   86.5000\r\n   12.5000   86.5000\r\n   34.5000  100.5000\r\n   34.5000  102.5000\r\n   33.5000  103.5000\r\n   27.5000  103.5000\r\n    9.5000   89.5000\r\n    9.5000   87.5000\r\n\r\n<\/pre><p>We can superimpose the extrema-bounded shapes on top of the original rice image by using patch objects.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">imshow(I)\r\n\r\nhold <span style=\"color: #A020F0\">on<\/span>\r\n<span style=\"color: #0000FF\">for<\/span> k = 1:numel(s)\r\n    x = s(k).Extrema(:,1);\r\n    y = s(k).Extrema(:,2);\r\n    patch(x, y, <span style=\"color: #A020F0\">'g'<\/span>)\r\n<span style=\"color: #0000FF\">end<\/span>\r\nhold <span style=\"color: #A020F0\">off<\/span><\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2008\/transparency_example_1_03.jpg\"> <p>The above visualization is pretty clear.  If you zoom in on some of the odd, larger shapes, though, you can't really tell\r\n      what's going on.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">axis([120 200 1 75])<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2008\/transparency_example_1_04.jpg\"> <p>We solve that by displaying the patches transparently.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">imshow(I)\r\n\r\nhold <span style=\"color: #A020F0\">on<\/span>\r\n<span style=\"color: #0000FF\">for<\/span> k = 1:numel(s)\r\n    x = s(k).Extrema(:,1);\r\n    y = s(k).Extrema(:,2);\r\n    patch(x, y, <span style=\"color: #A020F0\">'g'<\/span>, <span style=\"color: #A020F0\">'FaceAlpha'<\/span>, 0.3)\r\n<span style=\"color: #0000FF\">end<\/span>\r\nhold <span style=\"color: #A020F0\">off<\/span><\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2008\/transparency_example_1_05.jpg\"> <p>Now if you zoom in on the same region, we can see exactly what caused the unusual region.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">axis([120 200 1 75])<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/2008\/transparency_example_1_06.jpg\"> <p>Two of the rice grains were touching.<\/p>\r\n   <p>Next time I'll show a couple of techniques for visualizing one image transparently superimposed on another.<\/p><script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_7060fd8ed8854bcda7751cb06b3d4b17() {\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='7060fd8ed8854bcda7751cb06b3d4b17 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 7060fd8ed8854bcda7751cb06b3d4b17';\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 2008 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_7060fd8ed8854bcda7751cb06b3d4b17()\"><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.6<br><\/p>\r\n<\/div>\r\n<!--\r\n7060fd8ed8854bcda7751cb06b3d4b17 ##### SOURCE BEGIN #####\r\n%% Image Visualization Using Transparency\r\n% Transparent graphics objects can be used effectively to\r\n% visualize image processing concepts.  Two particularly useful\r\n% techniques are:\r\n%\r\n% * Highlighting image regions with transparent patches\r\n% * Displaying one image transparently over another\r\n%\r\n% Today I'll show how to highlight image regions with patches.  For\r\n% this example I'll use the |'Extrema'| measurement returned\r\n% by \r\n% <https:\/\/www.mathworks.com\/help\/images\/index.htmlregionprops.html \r\n% regionprops>. The extrema for a given object are eight\r\n% points: the left-most pixel on the bottom, the right-most pixel\r\n% on the bottom, the top-most pixel on the right, the bottom-most\r\n% pixel on the right, and so on.\r\n%\r\n% I'll start with the rice image segment it using techniques I've\r\n% shown before.\r\n\r\nI = imread('rice.png');\r\nimshow(I)\r\n\r\n%%\r\n% Even out the illumination with the tophat operator, threshold,\r\n% and then clean up the thresholded image a bit.\r\n\r\nI2 = imtophat(I, ones(15, 15));\r\nbw = im2bw(I2, graythresh(I2));\r\nbw2 = bwareaopen(bw, 5);\r\nbw3 = imclearborder(bw2);\r\nimshow(bw3)\r\n\r\n%%\r\n% Label the binary objects and compute the extrema.\r\nL = bwlabel(bw3);\r\ns = regionprops(L, 'Extrema');\r\n\r\n%%\r\n% Each object has 8 extrema points associated with it.\r\ns(1).Extrema\r\n\r\n%%\r\n% We can superimpose the extrema-bounded shapes on top of the\r\n% original rice image by using patch objects.\r\n\r\nimshow(I)\r\n\r\nhold on\r\nfor k = 1:numel(s)\r\n    x = s(k).Extrema(:,1);\r\n    y = s(k).Extrema(:,2);\r\n    patch(x, y, 'g')\r\nend\r\nhold off\r\n\r\n%%\r\n% The above visualization is pretty clear.  If you zoom in on\r\n% some of the odd, larger shapes, though, you can't really tell\r\n% what's going on.  \r\n\r\naxis([120 200 1 75])\r\n\r\n%%\r\n% We solve that by displaying the patches transparently.\r\n\r\nimshow(I)\r\n\r\nhold on\r\nfor k = 1:numel(s)\r\n    x = s(k).Extrema(:,1);\r\n    y = s(k).Extrema(:,2);\r\n    patch(x, y, 'g', 'FaceAlpha', 0.3)\r\nend\r\nhold off\r\n\r\n%%\r\n% Now if you zoom in on the same region, we can see exactly what\r\n% caused the unusual region.\r\n\r\naxis([120 200 1 75])\r\n\r\n%%\r\n% Two of the rice grains were touching.\r\n%\r\n% Next time I'll show a couple of techniques for visualizing one\r\n% image transparently superimposed on another.\r\n\r\n##### SOURCE END ##### 7060fd8ed8854bcda7751cb06b3d4b17\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   Transparent graphics objects can be used effectively to visualize image processing concepts.  Two particularly useful techniques\r\n      are:\r\n   \r\n   \r\n      \r\n         Highlighting image... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2008\/08\/20\/image-visualization-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":[50,138,166,82,90,84,404,76,36,514,162,210,168],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/223"}],"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=223"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/223\/revisions"}],"predecessor-version":[{"id":2222,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/223\/revisions\/2222"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=223"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=223"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=223"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}