{"id":178,"date":"2007-10-19T07:00:58","date_gmt":"2007-10-19T11:00:58","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/2007\/10\/19\/finding-bright-objects\/"},"modified":"2019-10-23T15:22:08","modified_gmt":"2019-10-23T19:22:08","slug":"finding-bright-objects","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2007\/10\/19\/finding-bright-objects\/","title":{"rendered":"Finding bright objects"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n   <p>In a blog comment earlier this summer, someone asked how to find \"which labeled regions have a bright spot greater than some\r\n      threshold?\" That's a good question. It can be done efficiently and compactly using the Image Processing Toolbox, but the techniques\r\n      may not be widely known.  (Have you ever used the <tt>ismember<\/tt> function to do image processing before?) Also, the techniques apply to other questions of the form \"which regions satisfy\r\n      some criterion?\"\r\n   <\/p>\r\n   <p>Let's work with the rice image.<\/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\/178\/bright_objects_01.jpg\"> <p>Threshold it.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">bw = im2bw(I, graythresh(I));\r\nimshow(bw)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/178\/bright_objects_02.jpg\"> <p>Clean it up a bit.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">bw = bwareaopen(bw, 50);\r\nimshow(bw)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/178\/bright_objects_03.jpg\"> <p>Now label connected components and compute the PixelIdxList property for each labeled region.  The PixelIdxList is useful\r\n      for the extracting pixels from a grayscale image that correspond to each labeled region in the binary image. (See my post\r\n      <a href=\"https:\/\/blogs.mathworks.com\/steve\/2007\/08\/21\/gray-scale-pixel-values-in-labeled-regions\/\">\"Grayscale pixel values in labeled regions<\/a>.\")\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">L = bwlabel(bw);\r\ns = regionprops(L,<span style=\"color: #A020F0\">'PixelIdxList'<\/span>);<\/pre><p>Compute the maximum pixel value for each labeled region.<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\"><span style=\"color: #228B22\">% Initialize vector containing max values.<\/span>\r\nmax_value = zeros(numel(s), 1);\r\n\r\n<span style=\"color: #228B22\">% Loop over each labeled object, grabbing the gray scale pixel values using<\/span>\r\n<span style=\"color: #228B22\">% PixelIdxList and computing their maximum.<\/span>\r\n<span style=\"color: #0000FF\">for<\/span> k = 1:numel(s)\r\n    max_value(k) = max(I(s(k).PixelIdxList));\r\n<span style=\"color: #0000FF\">end<\/span>\r\n\r\n<span style=\"color: #228B22\">% Show all the maximum values as a bar chart.<\/span>\r\nbar(max_value)<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/178\/bright_objects_04.jpg\"> <p>And we'll finish by using <tt>find<\/tt> to determine which objects have a maximum value greater than 200.  Then we'll display those objects using <tt>ismember<\/tt>.\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">bright_objects = find(max_value &gt; 200)<\/pre><pre style=\"font-style:oblique\">\r\nbright_objects =\r\n\r\n    22\r\n    28\r\n    33\r\n    35\r\n    40\r\n    42\r\n    49\r\n    51\r\n    60\r\n    65\r\n\r\n<\/pre><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">imshow(ismember(L, bright_objects))<\/pre><img decoding=\"async\" vspace=\"5\" hspace=\"5\" src=\"https:\/\/blogs.mathworks.com\/images\/steve\/178\/bright_objects_05.jpg\"> <script language=\"JavaScript\">\r\n<!--\r\n\r\n    function grabCode_6b6d54609fc64db5aceaa7c2a080abc5() {\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='6b6d54609fc64db5aceaa7c2a080abc5 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 6b6d54609fc64db5aceaa7c2a080abc5';\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 2007 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_6b6d54609fc64db5aceaa7c2a080abc5()\"><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.5<br><\/p>\r\n<\/div>\r\n<!--\r\n6b6d54609fc64db5aceaa7c2a080abc5 ##### SOURCE BEGIN #####\r\n%%\r\n% In a blog comment earlier this summer, someone asked how to find \"which\r\n% labeled regions have a bright spot greater than some threshold?\" That's a\r\n% good question. It can be done efficiently and compactly using the Image\r\n% Processing Toolbox, but the techniques may not be widely known.  (Have\r\n% you ever used the |ismember| function to do image processing before?)\r\n% Also, the techniques apply to other questions of the form \"which regions\r\n% satisfy some criterion?\"\r\n%\r\n% Let's work with the rice image.\r\n\r\nI = imread('rice.png');\r\nimshow(I)\r\n\r\n%%\r\n% Threshold it.\r\nbw = im2bw(I, graythresh(I));\r\nimshow(bw)\r\n\r\n%%\r\n% Clean it up a bit.\r\nbw = bwareaopen(bw, 50);\r\nimshow(bw)\r\n\r\n%%\r\n% Now label connected components and compute the PixelIdxList property for\r\n% each labeled region.  The PixelIdxList is useful for the extracting pixels\r\n% from a grayscale image that correspond to each labeled region in the\r\n% binary image. (See my post\r\n% <https:\/\/blogs.mathworks.com\/steve\/2007\/08\/21\/gray-scale-pixel-values-in-labeled-regions\/ \r\n% \"Grayscale pixel values in labeled regions>.\")\r\n\r\nL = bwlabel(bw);\r\ns = regionprops(L,'PixelIdxList');\r\n\r\n%%\r\n% Compute the maximum pixel value for each labeled region.  \r\n\r\n% Initialize vector containing max values.\r\nmax_value = zeros(numel(s), 1);\r\n\r\n% Loop over each labeled object, grabbing the gray scale pixel values using\r\n% PixelIdxList and computing their maximum.\r\nfor k = 1:numel(s)\r\n    max_value(k) = max(I(s(k).PixelIdxList));\r\nend\r\n\r\n% Show all the maximum values as a bar chart.\r\nbar(max_value)\r\n\r\n%%\r\n% And we'll finish by using |find| to determine which objects have\r\n% a maximum value greater than 200.  Then we'll display those objects \r\n% using |ismember|.\r\n\r\nbright_objects = find(max_value > 200)\r\n\r\n%%\r\nimshow(ismember(L, bright_objects))\r\n\r\n##### SOURCE END ##### 6b6d54609fc64db5aceaa7c2a080abc5\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n   In a blog comment earlier this summer, someone asked how to find \"which labeled regions have a bright spot greater than some\r\n      threshold?\" That's a good question. It can be done efficiently... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2007\/10\/19\/finding-bright-objects\/\">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":[138,166,348,82,84,76,36,422,122,420,168,130],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/178"}],"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=178"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/178\/revisions"}],"predecessor-version":[{"id":3570,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/178\/revisions\/3570"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=178"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=178"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=178"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}