{"id":52,"date":"2006-04-10T07:00:15","date_gmt":"2006-04-10T11:00:15","guid":{"rendered":"https:\/\/blogs.mathworks.com\/steve\/?p=52"},"modified":"2019-10-22T09:14:04","modified_gmt":"2019-10-22T13:14:04","slug":"quick-tip-determining-uniqueness-of-neighborhood-maximum","status":"publish","type":"post","link":"https:\/\/blogs.mathworks.com\/steve\/2006\/04\/10\/quick-tip-determining-uniqueness-of-neighborhood-maximum\/","title":{"rendered":"Quick tip: Determining uniqueness of local maximum"},"content":{"rendered":"<div xmlns:mwsh=\"https:\/\/www.mathworks.com\/namespace\/mcode\/v1\/syntaxhighlight.dtd\" class=\"content\">\r\n\r\n   <p>A MATLAB user asked me how to determine which image pixels had local maxima that weren't unique.  My answer involves a creative\r\n      use of <tt>ordfilt2<\/tt>.\r\n   <\/p>\r\n   <p>Let's review first the concept of a local maximum filter.  This image operator replaces every pixel value with the maximum\r\n      pixel value found in a neighborhood surrounding the pixel.    For example:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">a = magic(5)<\/pre><pre style=\"font-style:oblique\">\r\na =\r\n\r\n    17    24     1     8    15\r\n    23     5     7    14    16\r\n     4     6    13    20    22\r\n    10    12    19    21     3\r\n    11    18    25     2     9\r\n\r\n<\/pre><p>What should the output be for a local maximum filter using 3-by-3 neighborhoods?  The output in the 3rd row, 2nd column would\r\n      be the maximum value found in this submatrix:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">neighborhood = a(2:4, 1:3)<\/pre><pre style=\"font-style:oblique\">\r\nneighborhood =\r\n\r\n    23     5     7\r\n     4     6    13\r\n    10    12    19\r\n\r\n<\/pre><p>The (3,2) output is 23:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">max(neighborhood(:))<\/pre><pre style=\"font-style:oblique\">\r\nans =\r\n\r\n    23\r\n\r\n<\/pre><p>Local maximum filtering is the same as morphological dilation, so I usually use <tt>imdilate<\/tt> to compute the result for the entire image:\r\n   <\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">b = imdilate(a, ones(3,3))<\/pre><pre style=\"font-style:oblique\">\r\nb =\r\n\r\n    24    24    24    16    16\r\n    24    24    24    22    22\r\n    23    23    21    22    22\r\n    18    25    25    25    22\r\n    18    25    25    25    21\r\n\r\n<\/pre><p>That's the background; now back to the original question.  How do you determine which pixels have multiple maxima in their\r\n      neighborhoods?\r\n   <\/p>\r\n   <p>I suggest using <tt>ordfilt2<\/tt> twice.  This function is for ''two-dimensional order-statistic filtering.''  This operation replaces every pixel value with\r\n      the n-th sorted pixel value in its neighborhood.  It uses an ascending sort order, so if n is the number of pixels in the\r\n      neighborhood, the operation is exactly equivalent to the local maximum filter.\r\n   <\/p>\r\n   <p>The specific procedure is:<\/p>\r\n   <p>1. Call <tt>ordfilt2<\/tt> to get the highest pixel values in each neighborhood.\r\n   <\/p>\r\n   <p>2. Call <tt>ordfilt2<\/tt> again to get the second-highest pixel values in each neighborhood.\r\n   <\/p>\r\n   <p>3. Determine where the outputs of steps 1 and 2 are equal.  Wherever they are equal, the local maximum is not unique.<\/p>\r\n   <p>Let's try an example.  First, modify our original matrix a bit:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">ap = a;\r\nap(3,4) = 22<\/pre><pre style=\"font-style:oblique\">\r\nap =\r\n\r\n    17    24     1     8    15\r\n    23     5     7    14    16\r\n     4     6    13    22    22\r\n    10    12    19    21     3\r\n    11    18    25     2     9\r\n\r\n<\/pre><p>Get the highest pixel values in each 3-by-3 neighborhood:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">highest = ordfilt2(ap, 9, ones(3,3))<\/pre><pre style=\"font-style:oblique\">\r\nhighest =\r\n\r\n    24    24    24    16    16\r\n    24    24    24    22    22\r\n    23    23    22    22    22\r\n    18    25    25    25    22\r\n    18    25    25    25    21\r\n\r\n<\/pre><p>Get the second-highest pixel values:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">second_highest = ordfilt2(ap, 8, ones(3,3))<\/pre><pre style=\"font-style:oblique\">\r\nsecond_highest =\r\n\r\n    23    23    14    15    15\r\n    23    23    22    22    22\r\n    12    19    21    22    22\r\n    12    19    22    22    22\r\n    12    19    21    21     9\r\n\r\n<\/pre><p>Determine where the highest and second highest pixel values are equal:<\/p><pre style=\"background: #F9F7F3; padding: 10px; border: 1px solid rgb(200,200,200)\">non_unique_maxima_mask = (highest == second_highest)<\/pre><pre style=\"font-style:oblique\">\r\nnon_unique_maxima_mask =\r\n\r\n     0     0     0     0     0\r\n     0     0     0     1     1\r\n     0     0     0     1     1\r\n     0     0     0     0     1\r\n     0     0     0     0     0\r\n\r\n<\/pre><script language=\"JavaScript\"> \r\n<!--\r\n    function grabCode_52() {\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='52 ' + '##### ' + 'SOURCE BEGIN' + ' #####';\r\n        t2='##### ' + 'SOURCE END' + ' #####' + ' 52';\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_52()\"><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\n52 ##### SOURCE BEGIN #####\r\n%% Uniqueness of local maxima\r\n% A MATLAB user asked me how to determine which image pixels had local\r\n% maxima that weren't unique.  My answer involves a creative use of\r\n% <https:\/\/www.mathworks.com\/help\/images\/index.htmlordfilt2.html \r\n% |ordfilt2|>.\r\n%\r\n% Let's review first the concept of a local maximum filter.  This image\r\n% operator replaces every pixel value with the maximum pixel value found in\r\n% a neighborhood surrounding the pixel.    For example:\r\n\r\na = magic(5)\r\n\r\n%%\r\n% What should the output be for a local maximum filter using 3-by-3\r\n% neighborhoods?  The output in the 3rd row, 2nd column would be the\r\n% maximum value found in this submatrix:\r\n\r\nneighborhood = a(2:4, 1:3)\r\n\r\n%%\r\n% The (3,2) output is 23:\r\n\r\nmax(neighborhood(:))\r\n\r\n%%\r\n% Local maximum filtering is the same as morphological dilation, so I\r\n% usually use \r\n% <https:\/\/www.mathworks.com\/help\/images\/index.htmlimdilate.html \r\n% |imdilate|> to compute the result for the entire image:\r\n\r\nb = imdilate(a, ones(3,3))\r\n\r\n%%\r\n% That's the background; now back to the original question.  How do you\r\n% determine which pixels have multiple maxima in their neighborhoods?\r\n%\r\n% I suggest using |ordfilt2| twice.  This function is for ''two-dimensional\r\n% order-statistic filtering.''  This operation replaces every pixel value\r\n% with the n-th sorted pixel value in its neighborhood.  It uses an\r\n% ascending sort order, so if n is the number of pixels in the\r\n% neighborhood, the operation is exactly equivalent to the local maximum\r\n% filter.\r\n%\r\n% The specific procedure is:\r\n%\r\n% 1. Call |ordfilt2| to get the highest pixel values in each neighborhood.\r\n%\r\n% 2. Call |ordfilt2| again to get the second-highest pixel values in each\r\n% neighborhood.\r\n%\r\n% 3. Determine where the outputs of steps 1 and 2 are equal.  Wherever they \r\n% are equal, the local maximum is not unique.\r\n%\r\n% Let's try an example.  First, modify our original matrix a bit:\r\n\r\nap = a;\r\nap(3,4) = 22\r\n\r\n%%\r\n% Get the highest pixel values in each 3-by-3 neighborhood:\r\n\r\nhighest = ordfilt2(ap, 9, ones(3,3))\r\n\r\n%%\r\n% Get the second-highest pixel values:\r\n\r\nsecond_highest = ordfilt2(ap, 8, ones(3,3))\r\n\r\n%%\r\n% Determine where the highest and second highest pixel values are equal:\r\n\r\nnon_unique_maxima_mask = (highest == second_highest)\r\n\r\n\r\n##### SOURCE END ##### 52\r\n-->","protected":false},"excerpt":{"rendered":"<p>\r\n\r\n   A MATLAB user asked me how to determine which image pixels had local maxima that weren't unique.  My answer involves a creative\r\n      use of ordfilt2.\r\n   \r\n   Let's review first the concept... <a class=\"read-more\" href=\"https:\/\/blogs.mathworks.com\/steve\/2006\/04\/10\/quick-tip-determining-uniqueness-of-neighborhood-maximum\/\">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":[124,54,122,120],"_links":{"self":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/52"}],"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=52"}],"version-history":[{"count":1,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/52\/revisions"}],"predecessor-version":[{"id":2182,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/posts\/52\/revisions\/2182"}],"wp:attachment":[{"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/media?parent=52"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/categories?post=52"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blogs.mathworks.com\/steve\/wp-json\/wp\/v2\/tags?post=52"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}