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-2023 Wilhelm Burger, Mark J. Burge. All rights reserved. 007 * Visit https://imagingbook.com for additional details. 008 ******************************************************************************/ 009 010package imagingbook.common.threshold.global; 011 012import imagingbook.common.histogram.HistogramUtils; 013 014/** 015 * <p> 016 * Slow version of the {@link IsodataThresholder} using explicit recalculation of background and foreground means in 017 * every iteration. See [1], Alg. 9.2. 018 * </p> 019 * <p> 020 * [1] W. Burger, M.J. Burge, <em>Digital Image Processing – An Algorithmic Introduction</em>, 3rd ed, Springer 021 * (2022). 022 * </p> 023 * 024 * @author WB 025 * @version 2022/08/01 026 */ 027public class IsodataThresholderSlow implements GlobalThresholder { 028 029 030 private static int MAX_ITERATIONS = 100; 031 032 public IsodataThresholderSlow() { 033 super(); 034 } 035 036 @Override 037 public float getThreshold(int[] h) { 038 int K = h.length; 039 int q = (int) HistogramUtils.mean(h, 0, K-1); // start with the total mean 040 int qq; 041 042 int i = 0; // iteration counter 043 do { 044 i++; 045 int nB = HistogramUtils.count(h, 0, q); 046 int nF = HistogramUtils.count(h, q+1, K-1); 047 if (nB == 0 || nF == 0) 048 return NoThreshold; 049 double meanB = HistogramUtils.mean(h, 0, q); 050 double meanF = HistogramUtils.mean(h, q+1, K-1); 051 qq = q; 052 q = (int)((meanB + meanF)/2); 053 } while(q != qq && i < MAX_ITERATIONS); 054 055 return q; 056 } 057 058}