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
014public class QuantileThresholder extends GlobalThresholder {
015        
016        private double b = 0.5; // quantile of expected background pixels
017
018        public QuantileThresholder() {
019                super();
020        }
021        
022        public QuantileThresholder(double b) {
023                super();
024                this.b = b;
025        }
026
027        public int getThreshold(int[] h) {
028                int K = h.length;
029                int N = sum(h);
030                
031                double n = N * b;       
032                int i = 0;
033                int c = h[0];
034                while (i < K && c < n) {
035                        i++;
036                        c+= h[i];
037                }
038                
039                int q = (c < N) ? i : -1; 
040                return q;
041        }
042}