001/*******************************************************************************
002 * This software is provided as a supplement to the authors' textbooks on digital
003 *  image processing published by Springer-Verlag in various languages and editions.
004 * Permission to use and distribute this software is granted under the BSD 2-Clause 
005 * "Simplified" License (see http://opensource.org/licenses/BSD-2-Clause). 
006 * Copyright (c) 2006-2016 Wilhelm Burger, Mark J. Burge. All rights reserved. 
007 * Visit http://imagingbook.com for additional details.
008 *******************************************************************************/
009
010package imagingbook.pub.threshold.global;
011
012import imagingbook.pub.threshold.global.GlobalThresholder;
013
014/**
015 * This thresholder implements the algorithm proposed by Ridler and Calvard (1978),
016 * T.W. Ridler, S. Calvard, Picture thresholding using an iterative selection method,
017 * IEEE Trans. System, Man and Cybernetics, SMC-8 (August 1978) 630-632.
018 * described in Glasbey/Horgan: "Image Analysis for the Biological Sciences" (Ch. 4).
019 * 
020 * Slow version using explicit recomputation of background and foreground means 
021 * in every iteration.
022 */
023public class IsodataThresholderSlow extends GlobalThresholder {
024        
025        private int MAX_ITERATIONS = 100;
026        
027        public IsodataThresholderSlow() {
028                super();
029        }
030
031        public int getThreshold(int[] h) {
032                int K = h.length;
033                int q = (int) mean(h, 0, K-1);  // start with the total mean
034                int q_;
035                
036                int i = 0;      // iteration counter
037                do {
038                        i++;
039                        int nB = count(h, 0, q);
040                        int nF = count(h, q+1, K-1);
041                        if (nB == 0 || nF == 0)
042                                return -1;
043                        double meanB = mean(h, 0, q);
044                        double meanF = mean(h, q+1, K-1);
045                        q_ = q;                         
046                        q = (int)((meanB + meanF)/2);
047                } while (q != q_ && i < MAX_ITERATIONS);
048                
049                return q;
050        }
051
052}