From: <ha...@us...> - 2011-09-02 21:30:47
|
Revision: 8486 http://octave.svn.sourceforge.net/octave/?rev=8486&view=rev Author: hauberg Date: 2011-09-02 21:30:40 +0000 (Fri, 02 Sep 2011) Log Message: ----------- Ensure we only consider thresholds within the min/max range of the image Modified Paths: -------------- trunk/octave-forge/main/image/inst/graythresh.m Modified: trunk/octave-forge/main/image/inst/graythresh.m =================================================================== --- trunk/octave-forge/main/image/inst/graythresh.m 2011-09-01 21:49:49 UTC (rev 8485) +++ trunk/octave-forge/main/image/inst/graythresh.m 2011-09-02 21:30:40 UTC (rev 8486) @@ -9,76 +9,76 @@ ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. - -## -*- texinfo -*- -## @deftypefn {Function File} {@var{level}=} graythresh (@var{I}) -## Compute global image threshold using Otsu's method. -## -## The output @var{level} is a global threshold (level) that can be used to convert -## an intensity image to a binary image with @code{im2bw}. -## @var{level} is a normalized intensity value that lies in the range [0, 1]. -## -## The function uses Otsu's method, which chooses the threshold to -## minimize the intraclass variance of the black and white pixels. -## -## Color images are converted grayscale before @var{level} is computed. -## @seealso{im2bw} -## @end deftypefn - -## Note: -## This function is taken from -## http://www.irit.fr/recherches/SAMOVA/MEMBERS/JOLY/Homepage_files/IRR05/Barre-Piquot/graythresh.m -## I added texinfo documentation, error checking and sanitised the code. -## -- Søren Hauberg - -function level = graythresh (I) - ## Input checking - if (nargin != 1) - print_usage(); - endif - if (!isgray(I) && !isrgb(I)) - error("graythresh: input must be an image"); - endif - - ## If the image is RGB convert it to grayscale - if (isrgb(I)) - I = rgb2gray(I); - endif - - ## Calculation of the normalized histogram - n = 256; - h = hist(I(:), n); - h = h/(length(I(:))+1); - - ## Calculation of the cumulated histogram and the mean values - w = cumsum(h); - mu = zeros(n, 1); mu(1) = h(1); - for i=2:n - mu(i) = mu(i-1) + i*h(i); - end - - ## Initialisation of the values used for the threshold calculation - w0 = w(1); - w1 = 1-w0; - mu0 = mu(1)/w0; - mu1 = (mu(end)-mu(1))/w1; - max = w0*w1*(mu1-mu0)*(mu1-mu0); - level = 1; - - ## For each step of the histogram, calculation of the threshold and storing of the maximum - for i = 2:n - w0 = w(i); - w1 = 1-w0; - mu0 = mu(i)/w0; - mu1 = (mu(end)-mu(i))/w1; - s = w0*w1*(mu1-mu0)*(mu1-mu0); - if (s > max) - max = s; - level = i; - endif - endfor - - ## Normalisation of the threshold - level /= n; -endfunction - + +## -*- texinfo -*- +## @deftypefn {Function File} {@var{level}=} graythresh (@var{I}) +## Compute global image threshold using Otsu's method. +## +## The output @var{level} is a global threshold (level) that can be used to convert +## an intensity image to a binary image with @code{im2bw}. +## @var{level} is a normalized intensity value that lies in the range [0, 1]. +## +## The function uses Otsu's method, which chooses the threshold to +## minimize the intraclass variance of the black and white pixels. +## +## Color images are converted grayscale before @var{level} is computed. +## @seealso{im2bw} +## @end deftypefn + +## Note: +## This function is taken from +## http://www.irit.fr/recherches/SAMOVA/MEMBERS/JOLY/Homepage_files/IRR05/Barre-Piquot/graythresh.m +## I added texinfo documentation, error checking and sanitised the code. +## -- Søren Hauberg + +function level = graythresh (I) + ## Input checking + if (nargin != 1) + print_usage(); + endif + if (!isgray(I) && !isrgb(I)) + error("graythresh: input must be an image"); + endif + + ## If the image is RGB convert it to grayscale + if (isrgb(I)) + I = rgb2gray(I); + endif + + ## Calculation of the normalized histogram + n = 256; + h = hist(I(:), 1:n); + h = h/(length(I(:))+1); + + ## Calculation of the cumulated histogram and the mean values + w = cumsum(h); + mu = zeros(n, 1); mu(1) = h(1); + for i=2:n + mu(i) = mu(i-1) + i*h(i); + end + + ## Initialisation of the values used for the threshold calculation + level = find (h > 0, 1); + w0 = w(level); + w1 = 1-w0; + mu0 = mu(level)/w0; + mu1 = (mu(end)-mu(level))/w1; + max = w0*w1*(mu1-mu0)*(mu1-mu0); + + ## For each step of the histogram, calculation of the threshold and storing of the maximum + for i = find (h > 0) + w0 = w(i); + w1 = 1-w0; + mu0 = mu(i)/w0; + mu1 = (mu(end)-mu(i))/w1; + s = w0*w1*(mu1-mu0)*(mu1-mu0); + if (s > max) + max = s; + level = i; + endif + endfor + + ## Normalisation of the threshold + level /= n; +endfunction + This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <car...@us...> - 2012-09-02 00:51:52
|
Revision: 10939 http://octave.svn.sourceforge.net/octave/?rev=10939&view=rev Author: carandraug Date: 2012-09-02 00:51:46 +0000 (Sun, 02 Sep 2012) Log Message: ----------- graythresh: update link to original code Modified Paths: -------------- trunk/octave-forge/main/image/inst/graythresh.m Modified: trunk/octave-forge/main/image/inst/graythresh.m =================================================================== --- trunk/octave-forge/main/image/inst/graythresh.m 2012-09-01 16:27:40 UTC (rev 10938) +++ trunk/octave-forge/main/image/inst/graythresh.m 2012-09-02 00:51:46 UTC (rev 10939) @@ -30,7 +30,7 @@ ## Note: ## This function is taken from -## http://www.irit.fr/recherches/SAMOVA/MEMBERS/JOLY/Homepage_files/IRR05/Barre-Piquot/graythresh.m +## http://www.irit.fr/~Philippe.Joly/Teaching/M2IRR/IRR05/ ## I added texinfo documentation, error checking and sanitised the code. ## -- Søren Hauberg This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <car...@us...> - 2012-09-16 22:37:08
|
Revision: 11037 http://octave.svn.sourceforge.net/octave/?rev=11037&view=rev Author: carandraug Date: 2012-09-16 22:37:02 +0000 (Sun, 16 Sep 2012) Log Message: ----------- graythresh: fix bug because intmax does not return double and wrongly named var Modified Paths: -------------- trunk/octave-forge/main/image/inst/graythresh.m Modified: trunk/octave-forge/main/image/inst/graythresh.m =================================================================== --- trunk/octave-forge/main/image/inst/graythresh.m 2012-09-16 17:47:16 UTC (rev 11036) +++ trunk/octave-forge/main/image/inst/graythresh.m 2012-09-16 22:37:02 UTC (rev 11037) @@ -99,14 +99,14 @@ endif ## If the image is RGB convert it to grayscale - if (isrgb(img)) + if (isrgb (img)) img = rgb2gray (img); endif switch tolower (algo) case {"otsu"} ## Calculation of the normalized histogram - n = intmax (class (img)) + 1; + n = double (intmax (class (img))) + 1; h = hist (img(:), 1:n); h = h / (length (img(:)) + 1); @@ -115,7 +115,7 @@ mu = zeros (n, 1); mu(1) = h(1); for i = 2:n - mu(i) = mu(i-1) + i * h(i); + mu(i) = mu(i-1) + i * h(i); endfor ## Initialisation of the values used for the threshold calculation @@ -134,12 +134,12 @@ mu0 = mu(i) / w0; mu1 = (mu(end) - mu(i)) / w1; s = w0 * w1 * (mu1 - mu0) * (mu1 - mu0); - if (s > max) + if (s > goodness) goodness = s; level = i; endif endfor - + ## Normalisation of the threshold level /= n; otherwise This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |
From: <car...@us...> - 2012-09-16 22:39:29
|
Revision: 11038 http://octave.svn.sourceforge.net/octave/?rev=11038&view=rev Author: carandraug Date: 2012-09-16 22:39:23 +0000 (Sun, 16 Sep 2012) Log Message: ----------- graythresh: consistent indentation Modified Paths: -------------- trunk/octave-forge/main/image/inst/graythresh.m Modified: trunk/octave-forge/main/image/inst/graythresh.m =================================================================== --- trunk/octave-forge/main/image/inst/graythresh.m 2012-09-16 22:37:02 UTC (rev 11037) +++ trunk/octave-forge/main/image/inst/graythresh.m 2012-09-16 22:39:23 UTC (rev 11038) @@ -91,58 +91,58 @@ ## added texinfo documentation, error checking and sanitised the code. function [level, goodness] = graythresh (img, algo = "otsu") - ## Input checking - if (nargin < 1 || nargin > 2) - print_usage(); - elseif (!isgray (img) && !isrgb (img)) - error ("graythresh: input must be an image"); - endif + ## Input checking + if (nargin < 1 || nargin > 2) + print_usage(); + elseif (!isgray (img) && !isrgb (img)) + error ("graythresh: input must be an image"); + endif - ## If the image is RGB convert it to grayscale - if (isrgb (img)) - img = rgb2gray (img); - endif + ## If the image is RGB convert it to grayscale + if (isrgb (img)) + img = rgb2gray (img); + endif - switch tolower (algo) - case {"otsu"} - ## Calculation of the normalized histogram - n = double (intmax (class (img))) + 1; - h = hist (img(:), 1:n); - h = h / (length (img(:)) + 1); + switch tolower (algo) + case {"otsu"} + ## Calculation of the normalized histogram + n = double (intmax (class (img))) + 1; + h = hist (img(:), 1:n); + h = h / (length (img(:)) + 1); - ## Calculation of the cumulated histogram and the mean values - w = cumsum (h); - mu = zeros (n, 1); - mu(1) = h(1); - for i = 2:n - mu(i) = mu(i-1) + i * h(i); - endfor + ## Calculation of the cumulated histogram and the mean values + w = cumsum (h); + mu = zeros (n, 1); + mu(1) = h(1); + for i = 2:n + mu(i) = mu(i-1) + i * h(i); + endfor - ## Initialisation of the values used for the threshold calculation - level = find (h > 0, 1); - w0 = w(level); - w1 = 1 - w0; - mu0 = mu(level) / w0; - mu1 = (mu(end) - mu(level)) / w1; - goodness = w0 * w1 * (mu1 - mu0) * (mu1 - mu0); - - ## For each step of the histogram, calculation of the threshold - ## and storing of the maximum - for i = find (h > 0) - w0 = w(i); - w1 = 1 - w0; - mu0 = mu(i) / w0; - mu1 = (mu(end) - mu(i)) / w1; - s = w0 * w1 * (mu1 - mu0) * (mu1 - mu0); - if (s > goodness) - goodness = s; - level = i; - endif - endfor + ## Initialisation of the values used for the threshold calculation + level = find (h > 0, 1); + w0 = w(level); + w1 = 1 - w0; + mu0 = mu(level) / w0; + mu1 = (mu(end) - mu(level)) / w1; + goodness = w0 * w1 * (mu1 - mu0) * (mu1 - mu0); + + ## For each step of the histogram, calculation of the threshold + ## and storing of the maximum + for i = find (h > 0) + w0 = w(i); + w1 = 1 - w0; + mu0 = mu(i) / w0; + mu1 = (mu(end) - mu(i)) / w1; + s = w0 * w1 * (mu1 - mu0) * (mu1 - mu0); + if (s > goodness) + goodness = s; + level = i; + endif + endfor - ## Normalisation of the threshold - level /= n; - otherwise - error ("graythresh: unknown method '%s'", algo); - endswitch + ## Normalisation of the threshold + level /= n; + otherwise + error ("graythresh: unknown method '%s'", algo); + endswitch endfunction This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |