View Javadoc
1   package com.github.celldynamics.quimp.plugin.randomwalk;
2   
3   import java.util.Arrays;
4   
5   import org.apache.commons.lang3.ArrayUtils;
6   
7   import com.github.celldynamics.quimp.plugin.randomwalk.RandomWalkSegmentation.SeedTypes;
8   
9   /**
10   * Hold algorithm parameters.
11   * 
12   * @author p.baniukiewicz
13   *
14   */
15  public class RandomWalkOptions {
16    /**
17     * Alpha penalises pixels whose intensities are far away from the mean seed intensity.
18     */
19    public double alpha;
20  
21    /**
22     * Beta penalises pixels located at an edge, i.e. where there is a large gradient in intensity.
23     * Diffusion will be reduced.
24     */
25    public double beta;
26  
27    /**
28     * Gamma is the strength of competition between foreground and background.
29     * 
30     * <p>gamma[1]==0 disables second sweep.
31     */
32    public double[] gamma;
33  
34    /**
35     * Maximum number of Euler iterations.
36     */
37    public int iter;
38  
39    /**
40     * Timestep, if dt=1 we are at the limit of CFL stability.
41     */
42    public double dt;
43  
44    /**
45     * Upper relative error limit used as stopping criterion.
46     * 
47     * <p>Contains errors rot two steps.
48     */
49    public double[] relim;
50  
51    /**
52     * true if local mean algorithm is used. false if global mean for seeds is computed.
53     * 
54     * <p>If localMean is used, the seeds provided to
55     * {@link RandomWalkSegmentation#run(Seeds)} must have {@link SeedTypes#ROUGHMASK} entry.
56     */
57    public boolean useLocalMean;
58  
59    /**
60     * Size of mask for local mean algorithm (odd).
61     */
62    public int localMeanMaskSize;
63  
64    /**
65     * Reference to filter used for filtering results between sweeps.
66     * 
67     * <p>null value switches off second sweep as well as gamma[1]==0. To switch off filtering between
68     * sweeps use
69     * {@link BinaryFilters.EmptyMorpho}
70     */
71    public transient BinaryFilters intermediateFilter;
72    /**
73     * Reference to filter used on final processing.
74     * 
75     * <p>null value switches off final filtering.
76     */
77    public transient BinaryFilters finalFilter;
78    /**
79     * If true, RW mask will be cut by AC mask.
80     */
81    public boolean maskLimit;
82  
83    /**
84     * Set default values.
85     */
86    public RandomWalkOptions() {
87      this.gamma = new double[2];
88      alpha = 4e2;
89      beta = 2 * 25;
90      gamma[0] = 100;
91      gamma[1] = 0;
92      iter = 10000;
93      dt = 0.1;
94      relim = new double[] { 8e-3, 10 * 8e-3 };
95      intermediateFilter = null;
96      finalFilter = null;
97      useLocalMean = false;
98      localMeanMaskSize = 25;
99      maskLimit = false;
100   }
101 
102   /**
103    * Set user values. For compulsory parameters only. If any of given parameters is null default
104    * value is used instead.
105    * 
106    * @param alpha alpha
107    * @param beta beta
108    * @param gamma1 gamma1
109    * @param gamma2 gamma2
110    * @param iter iter
111    * @param dt dt
112    * @param relim relim (will be copied)
113    * @param useLocalMean useLocalMean
114    * @param localMeanMaskSize localMeanMaskSize
115    */
116   public RandomWalkOptions(Double alpha, Double beta, Double gamma1, Double gamma2, Integer iter,
117           Double dt, Double[] relim, Boolean useLocalMean, Integer localMeanMaskSize) {
118     this();
119     if (alpha != null) {
120       this.alpha = alpha;
121     }
122     if (beta != null) {
123       this.beta = beta;
124     }
125     if (gamma1 != null) {
126       this.gamma[0] = gamma1;
127     }
128     if (gamma2 != null) {
129       this.gamma[1] = gamma2;
130     }
131     if (iter != null) {
132       this.iter = iter;
133     }
134     if (dt != null) {
135       this.dt = dt;
136     }
137     if (relim != null) {
138       this.relim = Arrays.copyOf(ArrayUtils.toPrimitive(relim), this.relim.length);
139     }
140     if (useLocalMean != null) {
141       this.useLocalMean = useLocalMean;
142     }
143     if (localMeanMaskSize != null) {
144       this.localMeanMaskSize = localMeanMaskSize;
145     }
146   }
147 
148   /*
149    * (non-Javadoc)
150    * 
151    * @see java.lang.Object#toString()
152    */
153   @Override
154   public String toString() {
155     return "RandomWalkParams [alpha=" + alpha + ", beta=" + beta + ", gamma="
156             + Arrays.toString(gamma) + ", iter=" + iter + ", dt=" + dt + ", relim="
157             + Arrays.toString(relim) + ", useLocalMean=" + useLocalMean + ", localMeanMaskSize="
158             + localMeanMaskSize + ", intermediateFilter=" + intermediateFilter + ", finalFilter="
159             + finalFilter + "]";
160   }
161 
162   /*
163    * (non-Javadoc)
164    * 
165    * @see java.lang.Object#hashCode()
166    */
167   /*
168    * Do not use BinaryFilters references.
169    * 
170    * @see java.lang.Object#hashCode()
171    */
172   @Override
173   public int hashCode() {
174     final int prime = 31;
175     int result = 1;
176     long temp;
177     temp = Double.doubleToLongBits(alpha);
178     result = prime * result + (int) (temp ^ (temp >>> 32));
179     temp = Double.doubleToLongBits(beta);
180     result = prime * result + (int) (temp ^ (temp >>> 32));
181     temp = Double.doubleToLongBits(dt);
182     result = prime * result + (int) (temp ^ (temp >>> 32));
183     result = prime * result + Arrays.hashCode(gamma);
184     result = prime * result + iter;
185     result = prime * result + localMeanMaskSize;
186     result = prime * result + Arrays.hashCode(relim);
187     result = prime * result + (useLocalMean ? 1231 : 1237);
188     return result;
189   }
190 
191   /*
192    * (non-Javadoc)
193    * 
194    * @see java.lang.Object#equals(java.lang.Object)
195    */
196   /*
197    * Do not use BinaryFilters references.
198    * 
199    * @see java.lang.Object#equals(java.lang.Object)
200    */
201   @Override
202   public boolean equals(Object obj) {
203     if (this == obj) {
204       return true;
205     }
206     if (obj == null) {
207       return false;
208     }
209     if (getClass() != obj.getClass()) {
210       return false;
211     }
212     RandomWalkOptions./../../com/github/celldynamics/quimp/plugin/randomwalk/RandomWalkOptions.html#RandomWalkOptions">RandomWalkOptions other = (RandomWalkOptions) obj;
213     if (Double.doubleToLongBits(alpha) != Double.doubleToLongBits(other.alpha)) {
214       return false;
215     }
216     if (Double.doubleToLongBits(beta) != Double.doubleToLongBits(other.beta)) {
217       return false;
218     }
219     if (Double.doubleToLongBits(dt) != Double.doubleToLongBits(other.dt)) {
220       return false;
221     }
222     if (!Arrays.equals(gamma, other.gamma)) {
223       return false;
224     }
225     if (iter != other.iter) {
226       return false;
227     }
228     if (localMeanMaskSize != other.localMeanMaskSize) {
229       return false;
230     }
231     if (!Arrays.equals(relim, other.relim)) {
232       return false;
233     }
234     if (useLocalMean != other.useLocalMean) {
235       return false;
236     }
237     return true;
238   }
239 
240 }