@Override
  public RandomAccessibleInterval<V> compute(
      RandomAccessibleInterval<T> input, RandomAccessibleInterval<V> output) {
    setUpNeighbours(input.numDimensions());

    // OutOfBounds for marker
    V zeroV = output.randomAccess().get().createVariable();
    zeroV.set(getVMinValue(zeroV));
    OutOfBounds<V> marker =
        new OutOfBoundsConstantValueFactory<V, RandomAccessibleInterval<V>>(zeroV).create(output);

    Cursor<V> cur = new Cursor<V>(output);

    // OutOfBounds for mask
    T zeroT = input.randomAccess().get().createVariable();
    zeroT.set(getTMinValue(zeroT));
    OutOfBounds<T> mask =
        new OutOfBoundsConstantValueFactory<T, RandomAccessibleInterval<T>>(zeroT).create(input);

    scanInRasterOrder(cur, marker, mask);
    scanInAntiRasterOrder(cur, marker, mask);

    propagate(marker, mask, m_neighbours);

    return output;
  }
  protected void generateHistogramData(DataContainer<T> container) {
    double ch1BinWidth = getXBinWidth(container);
    double ch2BinWidth = getYBinWidth(container);

    // get the 2 images for the calculation of Pearson's
    final RandomAccessibleInterval<T> img1 = getImageCh1(container);
    final RandomAccessibleInterval<T> img2 = getImageCh2(container);
    final RandomAccessibleInterval<BitType> mask = container.getMask();

    // get the cursors for iterating through pixels in images
    TwinCursor<T> cursor =
        new TwinCursor<T>(
            img1.randomAccess(), img2.randomAccess(), Views.iterable(mask).localizingCursor());

    // create new image to put the scatter-plot in
    final ImgFactory<LongType> scatterFactory = new ArrayImgFactory<LongType>();
    plotImage = scatterFactory.create(new int[] {xBins, yBins}, new LongType());

    // create access cursors
    final RandomAccess<LongType> histogram2DCursor = plotImage.randomAccess();

    // iterate over images
    long[] pos = new long[plotImage.numDimensions()];
    while (cursor.hasNext()) {
      cursor.fwd();
      double ch1 = cursor.getFirst().getRealDouble();
      double ch2 = cursor.getSecond().getRealDouble();
      /* Scale values for both channels to fit in the range.
       * Moreover mirror the y value on the x axis.
       */
      pos[0] = getXValue(ch1, ch1BinWidth, ch2, ch2BinWidth);
      pos[1] = getYValue(ch1, ch1BinWidth, ch2, ch2BinWidth);
      // set position of input/output cursor
      histogram2DCursor.setPosition(pos);
      // get current value at position and increment it
      long count = histogram2DCursor.get().getIntegerLong();
      count++;

      histogram2DCursor.get().set(count);
    }

    xBinWidth = ch1BinWidth;
    yBinWidth = ch2BinWidth;
    xLabel = getLabelCh1();
    yLabel = getLabelCh2();
    xMin = getXMin(container);
    xMax = getXMax(container);
    yMin = getYMin(container);
    yMax = getYMax(container);
  }
  @Override
  public void map() {
    for (int d = 2; d < position.length; ++d) min[d] = max[d] = position[d];

    min[0] = target.min(0);
    min[1] = target.min(1);
    max[0] = target.max(0);
    max[1] = target.max(1);
    final FinalInterval sourceInterval = new FinalInterval(min, max);

    final long cr = -target.dimension(0);

    final RandomAccess<B> targetRandomAccess = target.randomAccess(target);
    final RandomAccess<A> sourceRandomAccess = source.randomAccess(sourceInterval);

    for (sourceRandomAccess.setPosition(min),
            targetRandomAccess.setPosition(min[0], 0),
            targetRandomAccess.setPosition(min[1], 1);
        targetRandomAccess.getLongPosition(1) <= max[1];
        sourceRandomAccess.move(cr, 0), targetRandomAccess.move(cr, 0), sourceRandomAccess.fwd(1),
            targetRandomAccess.fwd(1)) {
      for (;
          targetRandomAccess.getLongPosition(0) <= max[0];
          sourceRandomAccess.fwd(0), targetRandomAccess.fwd(0))
        converter.convert(sourceRandomAccess.get(), targetRandomAccess.get());
    }
  }
  /**
   * A table of x-values, y-values and the counts is generated and returned as a string. The single
   * fields in one row (X Y Count) are separated by tabs.
   *
   * @return A String representation of the histogram data.
   */
  public String getData() {
    StringBuffer sb = new StringBuffer();

    double xBinWidth = 1.0 / getXBinWidth();
    double yBinWidth = 1.0 / getYBinWidth();
    double xMin = getXMin();
    double yMin = getYMin();
    // check if we have bins of size one or other ones
    boolean xBinWidthIsOne = Math.abs(xBinWidth - 1.0) < 0.00001;
    boolean yBinWidthIsOne = Math.abs(yBinWidth - 1.0) < 0.00001;
    // configure decimal places accordingly
    int xDecimalPlaces = xBinWidthIsOne ? 0 : 3;
    int yDecimalPlaces = yBinWidthIsOne ? 0 : 3;
    // create a cursor to access the histogram data
    RandomAccess<LongType> cursor = plotImage.randomAccess();
    // loop over 2D histogram
    for (int i = 0; i < plotImage.dimension(0); ++i) {
      for (int j = 0; j < plotImage.dimension(1); ++j) {
        cursor.setPosition(i, 0);
        cursor.setPosition(j, 1);
        sb.append(
            ResultsTable.d2s(xMin + (i * xBinWidth), xDecimalPlaces)
                + "\t"
                + ResultsTable.d2s(yMin + (j * yBinWidth), yDecimalPlaces)
                + "\t"
                + ResultsTable.d2s(cursor.get().getRealDouble(), 0)
                + "\n");
      }
    }

    return sb.toString();
  }
Example #5
0
  @SuppressWarnings({"rawtypes", "unchecked"})
  public static <T extends Type<T>> ImageRenderer<T>[] createSuitableRenderer(
      final RandomAccessibleInterval<T> img) {

    final List<ImageRenderer> res = new ArrayList<ImageRenderer>();

    if (Util.getTypeFromInterval(img) instanceof LabelingType) {
      res.add(new ColorLabelingRenderer());
      res.add(new BoundingBoxLabelRenderer());
      res.add(new BoundingBoxRandomColorLabelRenderer());
    } else {
      final T type = img.randomAccess().get();

      if (type instanceof RealType) {
        res.add(new Real2GreyRenderer(((RealType) Util.getTypeFromInterval(img)).getMinValue()));
        for (int d = 0; d < img.numDimensions(); d++) {
          if ((img.dimension(d) > 1) && (img.dimension(d) < 4)) {
            res.add(new Real2ColorRenderer(d));
          }
        }
      }
    }

    return res.toArray(new ImageRenderer[res.size()]);
  }
  @Override
  public RealSum call() throws Exception {
    final Cursor<FloatType> psiCursor = psiIterable.localizingCursor();
    psiCursor.jumpFwd(portion.getStartPosition());

    final int m = iterableImgs.size();

    if (compatibleIteration) {
      final ArrayList<Cursor<FloatType>> cursorWeights = new ArrayList<Cursor<FloatType>>();
      final ArrayList<Cursor<FloatType>> cursorImgs = new ArrayList<Cursor<FloatType>>();

      for (final IterableInterval<FloatType> img : iterableImgs) {
        final Cursor<FloatType> imgCursor = img.cursor();
        imgCursor.jumpFwd(portion.getStartPosition());
        cursorImgs.add(imgCursor);
      }

      for (final IterableInterval<FloatType> weight : iterableWeights) {
        final Cursor<FloatType> weightCursor = weight.cursor();
        weightCursor.jumpFwd(portion.getStartPosition());
        cursorWeights.add(weightCursor);
      }

      for (int j = 0; j < portion.getLoopSize(); ++j)
        compatibleLoop(psiCursor, cursorWeights, cursorImgs, realSum, m);
    } else {
      final ArrayList<RandomAccess<FloatType>> randomAccessWeights =
          new ArrayList<RandomAccess<FloatType>>();
      final ArrayList<RandomAccess<FloatType>> randomAccessImgs =
          new ArrayList<RandomAccess<FloatType>>();

      for (final RandomAccessibleInterval<FloatType> img : imgs)
        randomAccessImgs.add(img.randomAccess());

      for (final RandomAccessibleInterval<FloatType> weight : weights)
        randomAccessWeights.add(weight.randomAccess());

      for (int j = 0; j < portion.getLoopSize(); ++j)
        incompatibleLoop(psiCursor, randomAccessWeights, randomAccessImgs, realSum, m);
    }

    return realSum;
  }
  @Override
  public void compute(final RandomAccessibleInterval<T> input, final IterableInterval<V> output) {
    final Cursor<V> cursor = output.localizingCursor();
    final RandomAccess<T> access = input.randomAccess();

    while (cursor.hasNext()) {
      cursor.fwd();
      for (int d = 0; d < input.numDimensions(); d++) {
        if (d != dim) {
          access.setPosition(cursor.getIntPosition(d - d > dim ? -1 : 0), d);
        }
      }

      method.compute(new DimensionIterable(input.dimension(dim), access), cursor.get());
    }
  }
Example #8
0
  @SuppressWarnings({"rawtypes", "unchecked"})
  public static <T extends Type<T>> ImageRenderer<T>[] createSuitableRenderer(
      final RandomAccessibleInterval<T> img, final ImageMetadata imageMetaData) {

    final List<ImageRenderer> res = new ArrayList<ImageRenderer>();
    res.addAll(Arrays.asList(createSuitableRenderer(img)));

    // color rendering
    final T type = img.randomAccess().get();

    if (type instanceof RealType) {
      if ((imageMetaData != null) && (imageMetaData.getColorTableCount() > 0)) {

        res.add(new Real2TableColorRenderer());
      }
    }

    return res.toArray(new ImageRenderer[res.size()]);
  }
    public Cursor(final RandomAccessibleInterval<U> ra) {
      m_ra = ra.randomAccess();

      m_numPixel = numPixels(ra);

      m_lastPos = new long[ra.numDimensions()];
      for (int i = 0; i < m_lastPos.length; i++) {
        m_lastPos[i] = ra.dimension(i) - 1;
      }

      m_breaks = new long[ra.numDimensions()];
      ra.dimensions(m_breaks);

      for (int i = 1; i < m_breaks.length; i++) {
        m_breaks[i] *= m_breaks[i - 1];
      }

      setToOrigin();
    }
  private static void adjustForOSEM(
      final HashMap<ViewId, RandomAccessibleInterval<FloatType>> weights,
      final WeightType weightType,
      final double osemspeedup) {
    if (osemspeedup == 1.0) return;

    if (weightType == WeightType.PRECOMPUTED_WEIGHTS
        || weightType == WeightType.WEIGHTS_ONLY
        || weightType == WeightType.LOAD_WEIGHTS) {
      for (final RandomAccessibleInterval<FloatType> w : weights.values()) {
        for (final FloatType f : Views.iterable(w))
          f.set(
              Math.min(
                  1, f.get() * (float) osemspeedup)); // individual contribution never higher than 1
      }
    } else if (weightType == WeightType.NO_WEIGHTS) {
      for (final RandomAccessibleInterval<FloatType> w : weights.values()) {
        final RandomAccess<FloatType> r = w.randomAccess();
        final long[] min = new long[w.numDimensions()];
        w.min(min);
        r.setPosition(min);
        r.get()
            .set(
                Math.min(
                    1,
                    r.get().get()
                        * (float) osemspeedup)); // individual contribution never higher than 1
      }
    } else if (weightType == WeightType.VIRTUAL_WEIGHTS) {
      for (final RandomAccessibleInterval<FloatType> w : weights.values())
        ((NormalizingRandomAccessibleInterval<FloatType>) w).setOSEMspeedup(osemspeedup);
    } else {
      throw new RuntimeException(
          "Weight Type: "
              + weightType.name()
              + " not supported in ProcessForDeconvolution.adjustForOSEM()");
    }
  }
 /** Compute the inverse Hessian matrix from the the steepest descent images. */
 public static <T extends RealType<T>> double[][] computeInverseHessian(
     final RandomAccessibleInterval<T> descent) {
   final int n = descent.numDimensions() - 1;
   final int numParameters = (int) descent.dimension(n);
   final long[] dim = new long[n + 1];
   descent.dimensions(dim);
   dim[n] = 1;
   final LocalizingIntervalIterator pos = new LocalizingIntervalIterator(dim);
   final RandomAccess<T> r = descent.randomAccess();
   final double[] deriv = new double[numParameters];
   final double[][] H = new double[numParameters][numParameters];
   while (pos.hasNext()) {
     pos.fwd();
     r.setPosition(pos);
     for (int p = 0; p < numParameters; ++p) {
       deriv[p] = r.get().getRealDouble();
       r.fwd(n);
     }
     for (int i = 0; i < numParameters; ++i)
       for (int j = 0; j < numParameters; ++j) H[i][j] += deriv[i] * deriv[j];
   }
   return new Matrix(H).inverse().getArray();
 }
Example #12
0
 /**
  * TODO
  *
  * @param r The segmentation image.
  * @param op0 Source intensity image.
  * @param op1 Start position.
  */
 public final void compute(
     RandomAccessibleInterval<T> op0, final long[] op1, final RandomAccessibleInterval<T> r) {
   final RandomAccess<T> rc = r.randomAccess();
   final RandomAccess<T> op0c = op0.randomAccess();
   op0c.setPosition(op1);
   final T floodVal = op0c.get().copy();
   final LinkedList<long[]> q = new LinkedList<long[]>();
   q.addFirst(op1.clone());
   long[] pos, nextPos;
   long[] perm = new long[r.numDimensions()];
   while (!q.isEmpty()) {
     pos = q.removeLast();
     rc.setPosition(pos);
     if (rc.get().compareTo(floodVal) == 0) {
       continue;
     }
     op0c.setPosition(pos);
     if (op0c.get().compareTo(floodVal) == 0) {
       // set new label
       rc.get().set(floodVal);
       switch (m_type) {
         case EIGHT_CONNECTED:
           Arrays.fill(perm, -1);
           int i = r.numDimensions() - 1;
           boolean add;
           while (i > -1) {
             nextPos = pos.clone();
             add = true;
             // Modify position
             for (int j = 0; j < r.numDimensions(); j++) {
               nextPos[j] += perm[j];
               // Check boundaries
               if (nextPos[j] < 0 || nextPos[j] >= r.dimension(j)) {
                 add = false;
                 break;
               }
             }
             if (add) {
               q.addFirst(nextPos);
             }
             // Calculate next permutation
             for (i = perm.length - 1; i > -1; i--) {
               if (perm[i] < 1) {
                 perm[i]++;
                 for (int j = i + 1; j < perm.length; j++) {
                   perm[j] = -1;
                 }
                 break;
               }
             }
           }
           break;
         case FOUR_CONNECTED:
         default:
           for (int j = 0; j < r.numDimensions(); j++) {
             if (pos[j] + 1 < r.dimension(j)) {
               nextPos = pos.clone();
               nextPos[j]++;
               q.addFirst(nextPos);
             }
             if (pos[j] - 1 >= 0) {
               nextPos = pos.clone();
               nextPos[j]--;
               q.addFirst(nextPos);
             }
           }
           break;
       }
     }
   }
 }