PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/imglib2/broken/scripting/src/main/java/net/imglib2/script/analysis/DoGPeaks.java

http://github.com/imagej/imglib
Java | 158 lines | 108 code | 21 blank | 29 comment | 14 complexity | b488b3c212bee73e5dcbb0a5ea140eee MD5 | raw file
Possible License(s): BSD-2-Clause, GPL-2.0
  1. package net.imglib2.script.analysis;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import javax.vecmath.Point2f;
  5. import javax.vecmath.Point3f;
  6. import javax.vecmath.Point4f;
  7. import net.imglib2.algorithm.scalespace.DifferenceOfGaussian;
  8. import net.imglib2.algorithm.scalespace.DifferenceOfGaussianPeak;
  9. import net.imglib2.img.array.ArrayImgFactory;
  10. import net.imglib2.function.RealTypeConverter;
  11. import net.imglib2.img.Img;
  12. import net.imglib2.outofbounds.OutOfBoundsMirrorFactory;
  13. import net.imglib2.type.numeric.RealType;
  14. import net.imglib2.type.numeric.real.FloatType;
  15. import net.imglib2.script.algorithm.fn.AlgorithmUtil;
  16. /** Perform a difference of Gaussian on the given {@link Image}, and this class itself
  17. * becomes the {@link List} of found peaks, each as a float[] array that specifies its position.
  18. *
  19. * See also {@link DifferenceOfGaussian, DifferenceOfGaussianPeak}.
  20. *
  21. */
  22. public class DoGPeaks<N extends RealType<N>> extends ArrayList<float[]>
  23. {
  24. private static final long serialVersionUID = 7614417748092214062L;
  25. private final List<DifferenceOfGaussianPeak<FloatType>> peaks;
  26. @SuppressWarnings("unchecked")
  27. public DoGPeaks(final Object fn, final Number sigmaLarge, final Number sigmaSmall,
  28. final Number minPeakValue, final Number normalizationFactor) throws Exception {
  29. this(AlgorithmUtil.wrapS(fn), sigmaLarge, sigmaSmall, minPeakValue, normalizationFactor);
  30. }
  31. /** Consider the {@param img} as isotropic: apply the same sigma to all dimensions. */
  32. public DoGPeaks(final Img<N> img, final Number sigmaLarge, final Number sigmaSmall,
  33. final Number minPeakValue, final Number normalizationFactor) throws Exception {
  34. this(img, AlgorithmUtil.asArray(img.numDimensions(), sigmaLarge.doubleValue()),
  35. AlgorithmUtil.asArray(img.numDimensions(), sigmaSmall.doubleValue()),
  36. minPeakValue, normalizationFactor);
  37. }
  38. /** Consider the {@param fn} as an isotropic image: apply the same sigma to all dimensions. */
  39. public DoGPeaks(final Object fn, final List<Number> sigmaLarge, final List<Number> sigmaSmall,
  40. final Number minPeakValue, final Number normalizationFactor) throws Exception {
  41. this(fn, AlgorithmUtil.asDoubleArray(sigmaLarge),
  42. AlgorithmUtil.asDoubleArray(sigmaSmall),
  43. minPeakValue, normalizationFactor);
  44. }
  45. /** Consider the {@param fn} as an isotropic image: apply the same sigma to all dimensions. */
  46. public DoGPeaks(final Object fn, final float[] sigmaLarge, final float[] sigmaSmall,
  47. final Number minPeakValue, final Number normalizationFactor) throws Exception {
  48. this(fn, AlgorithmUtil.asDoubleArray(sigmaLarge),
  49. AlgorithmUtil.asDoubleArray(sigmaSmall),
  50. minPeakValue, normalizationFactor);
  51. }
  52. @SuppressWarnings("unchecked")
  53. public DoGPeaks(final Object fn, final double[] sigmaLarge, final double[] sigmaSmall,
  54. final Number minPeakValue, final Number normalizationFactor) throws Exception {
  55. this(AlgorithmUtil.wrapS(fn), sigmaLarge, sigmaSmall, minPeakValue, normalizationFactor);
  56. }
  57. /**
  58. * @param img The {@link Image} to search peaks into.
  59. * @param sigmaLarge A double[] array with a sigma value for each dimension of the image.
  60. * @param sigmaSmall A double[] array with a sigma value for each dimension of the image.
  61. * @param minPeakValue The lowest intensity value for a difference of Gaussian peak to be considered so.
  62. * @param normalizationFactor See {@link DifferenceOfGaussian}. */
  63. public DoGPeaks(final Img<N> img, final double[] sigmaLarge, final double[] sigmaSmall,
  64. final Number minPeakValue, final Number normalizationFactor) throws Exception {
  65. final DifferenceOfGaussian<N, FloatType> dog
  66. = new DifferenceOfGaussian<N, FloatType>(img, new ImageFactory<FloatType>(new FloatType(), new ArrayContainerFactory()),
  67. new RealTypeConverter<N, FloatType>(), new OutOfBoundsStrategyMirrorFactory<FloatType>(),
  68. sigmaLarge, sigmaSmall,
  69. new FloatType(minPeakValue.floatValue()), new FloatType(normalizationFactor.floatValue()));
  70. if (!dog.process()) {
  71. throw new Exception("Could not process DifferenceOfGaussian: " + dog.getErrorMessage());
  72. }
  73. this.peaks = dog.getPeaks();
  74. for (final DifferenceOfGaussianPeak<FloatType> p : this.peaks) {
  75. this.add(p.getSubPixelPosition());
  76. }
  77. }
  78. /** Get all peaks as {@link DifferenceOfGaussianPeak} instances. */
  79. public List<DifferenceOfGaussianPeak<FloatType>> getPeaks() {
  80. return new ArrayList<DifferenceOfGaussianPeak<FloatType>>(this.peaks);
  81. }
  82. /** Returns a list of peaks as Java3D points if possible:
  83. *
  84. * {@link Point2f} for 2D images
  85. * {@link Point3f} for 3D images
  86. * {@link Point4f} for 4D images
  87. *
  88. * @throws IllegalArgumentException if scalingFactor.length is different than the image dimensions.
  89. * @throws Exception if the image dimensions is not 2, 3, or 4.
  90. */
  91. public List<Object> asPoints(final float[] scalingFactor) throws Exception {
  92. final List<Object> points = new ArrayList<Object>();
  93. if (this.isEmpty()) return points;
  94. int len = this.get(0).length;
  95. if (len < 2 || len > 4) {
  96. throw new Exception("Dimensions of the peak coordinates are not 2, 3 or 4. Use getDoGPeaks() instead.");
  97. }
  98. if (scalingFactor.length != len) {
  99. throw new IllegalArgumentException("scalingFactor dimensions does not match with the dimensions of the peaks.");
  100. }
  101. switch (len) {
  102. case 2:
  103. for (final float[] p : this)
  104. points.add(new Point2f(scaled(p, scalingFactor)));
  105. break;
  106. case 3:
  107. for (final float[] p : this)
  108. points.add(new Point3f(scaled(p, scalingFactor)));
  109. break;
  110. case 4:
  111. for (final float[] p : this)
  112. points.add(new Point4f(scaled(p, scalingFactor)));
  113. break;
  114. }
  115. return points;
  116. }
  117. /** Returns a list of peaks as Java3D points: Point2f for 2D images, Point3f for 3D images, and Point4f for 4D images.
  118. * For images of 1D or more than 4D, returns a list of float[]. */
  119. public List<Object> asPoints() throws Exception {
  120. return asPoints(1);
  121. }
  122. /** Returns a list of peaks as Java3D points: Point2f for 2D images, Point3f for 3D images, and Point4f for 4D images.
  123. * For images of 1D or more than 4D, returns a list of float[]. */
  124. public List<Object> asPoints(final Number scalingFactor) throws Exception {
  125. if (this.isEmpty()) return new ArrayList<Object>();
  126. float[] s = new float[this.get(0).length];
  127. for (int i=0; i<s.length; i++) s[i] = scalingFactor.floatValue();
  128. return asPoints(s);
  129. }
  130. private static final float[] scaled(final float[] peak, final float[] scalingFactor) {
  131. final float[] p = new float[peak.length];
  132. for (int i=0; i<peak.length; i++) {
  133. p[i] = peak[i] * scalingFactor[i];
  134. }
  135. return p;
  136. }
  137. }