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.adaptive; 011 012import ij.process.ByteProcessor; 013import ij.process.FloatProcessor; 014import imagingbook.pub.threshold.Thresholder; 015 016public abstract class AdaptiveThresholder extends Thresholder { 017 018 public abstract ByteProcessor getThreshold(ByteProcessor bp); 019 020 public void threshold(ByteProcessor bp, ByteProcessor Q) { 021 final int w = bp.getWidth(); 022 final int h = bp.getHeight(); 023 for (int v = 0; v < h; v++) { 024 for (int u = 0; u < w; u++) { 025 int p = bp.get(u, v); 026 int q = Q.get(u, v); 027 bp.set(u, v, (p <= q) ? 0 : 255); 028 } 029 } 030 } 031 032 public boolean threshold(ByteProcessor ip) { 033 ByteProcessor Q = this.getThreshold(ip); 034 if (Q != null) { 035 this.threshold(ip, Q); 036 return true; 037 } 038 else { 039 return false; 040 } 041 } 042 043 // change to use an ImageAccessor! 044 protected int getPaddedPixel(ByteProcessor bp, int u, int v) { 045 final int w = bp.getWidth(); 046 final int h = bp.getHeight(); 047 if (u < 0) 048 u = 0; 049 else if (u >= w) 050 u = w - 1; 051 if (v < 0) 052 v = 0; 053 else if (v >= h) 054 v = h - 1; 055 return bp.get(u, v); 056 } 057 058 // used for logging/testing only 059 protected double[] getLine(ByteProcessor bp, int v) { 060 double[] line = new double[bp.getWidth()]; 061 for (int u = 0; u < line.length; u++) { 062 line[u] = bp.get(u, v); 063 } 064 return line; 065 } 066 067 // used for logging/testing only 068 protected double[] getLine(FloatProcessor fp, int v) { 069 double[] line = new double[fp.getWidth()]; 070 for (int u = 0; u < line.length; u++) { 071 line[u] = fp.getf(u, v); 072 } 073 return line; 074 } 075}