View Javadoc
1   package com.github.celldynamics.quimp.plugin.randomwalk;
2   
3   import java.util.List;
4   import java.util.ListIterator;
5   
6   import org.apache.commons.math3.linear.RealMatrix;
7   
8   import com.github.celldynamics.quimp.plugin.randomwalk.RandomWalkSegmentation.SeedTypes;
9   import com.github.celldynamics.quimp.utils.QuimPArrayUtils;
10  
11  import ij.ImageStack;
12  import ij.process.ImageProcessor;
13  
14  /**
15   * Store probability maps computed for foreground and background objects.
16   * 
17   * <p>Allow to keep many maps for FG and BG, e.g. when there are more than one object that should be
18   * handled separately. For given {@link SeedTypes} key maps are stored in ArrayList.
19   * 
20   * @author p.baniukiewicz
21   * @see Seeds
22   */
23  @SuppressWarnings("serial")
24  public class ProbabilityMaps extends ListMap<RealMatrix> {
25  
26    /**
27     * Default constructor.
28     */
29    public ProbabilityMaps() {
30      super();
31    }
32  
33    /**
34     * Convert list of maps under specified key to 3d array of doubles.
35     * 
36     * <p>Data in output array are references. Require the same size of all maps within specified key.
37     * 
38     * @param key which map to convert
39     * @return 3d array [map][width][height] or null if there is no maps under specified key
40     * @throws IllegalArgumentException if size of maps is not equal
41     */
42    public double[][][] convertTo3dMatrix(Object key) {
43      List<RealMatrix> maps = get(key);
44      // Can be null if points not found
45      if (maps == null || maps.isEmpty()) {
46        return null;
47      }
48      // assume all maps have the same resolution
49      int width = maps.get(0).getColumnDimension();
50      int height = maps.get(0).getRowDimension();
51      int depth = maps.size();
52      for (int i = 1; i < depth; i++) {
53        RealMatrix tmp = maps.get(i);
54        if (tmp.getRowDimension() != height || tmp.getColumnDimension() != width) {
55          throw new IllegalArgumentException("All maps must have the same resoultion");
56        }
57      }
58      double[][][] ret = new double[depth][][];
59      ListIterator<RealMatrix> it = maps.listIterator();
60      while (it.hasNext()) {
61        ret[it.nextIndex()] = it.next().getData();
62      }
63  
64      return ret;
65    }
66  
67    /**
68     * Convert probability maps to ImageStack.
69     * 
70     * @param key which map to convert
71     * @return Float image stack or null if specified key does not exists
72     * @throws IllegalArgumentException if size of maps is not equal
73     */
74    public ImageStack convertToImageStack(Object key) {
75      List<RealMatrix> maps = get(key);
76      // Can be null if points not found
77      if (maps == null || maps.isEmpty()) {
78        return null;
79      }
80      // assume all maps have the same resolution
81      int width = maps.get(0).getColumnDimension();
82      int height = maps.get(0).getRowDimension();
83      int depth = maps.size();
84      for (int i = 1; i < depth; i++) {
85        RealMatrix tmp = maps.get(i);
86        if (tmp.getRowDimension() != height || tmp.getColumnDimension() != width) {
87          throw new IllegalArgumentException("All maps must have the same resoultion");
88        }
89      }
90      ImageStack is = new ImageStack(width, height);
91      for (RealMatrix m : maps) {
92        ImageProcessor fp = QuimPArrayUtils.realMatrix2ImageProcessor(m); // returns float
93        is.addSlice(fp);
94      }
95      return is;
96    }
97  }