Example #1
0
  @Override
  public void set(int[] indexes, double value) {
    int d = indexes.length;
    if (d == 0) {
      set(value);
    }

    T slice = slices[indexes[0]];
    switch (d) {
      case 0:
        throw new VectorzException("Can't do 0D set on SliceArray!");
      case 1:
        slice.set(value);
        return;
      case 2:
        slice.set(indexes[1], value);
        return;
      case 3:
        slice.set(indexes[1], indexes[2], value);
        return;
      default:
        slice.set(Arrays.copyOfRange(indexes, 1, d), value);
        return;
    }
  }
  /**
   * Processes the next frame in the sequence.
   *
   * @param frame Next frame in the video sequence
   * @return If a fatal error occurred or not.
   */
  public boolean process(I frame) {
    // update the feature tracker
    tracker.process(frame);
    totalProcessed++;

    // set up data structures and spawn tracks
    if (totalProcessed == 1) {
      tracker.spawnTracks();
      tracker.setKeyFrame();

      worldToKey.set(worldToInit);
      worldToCurr.set(worldToInit);
      return true;
    }

    // fit the motion model to the feature tracks
    List<KeyFrameTrack> pairs = tracker.getPairs();
    if (!modelMatcher.process((List) pairs)) {
      return false;
    }

    // refine the motion estimate
    if (modelRefiner == null
        || !modelRefiner.fitModel(modelMatcher.getMatchSet(), modelMatcher.getModel(), keyToCurr)) {
      keyToCurr.set(modelMatcher.getModel());
    }

    // Update the motion
    worldToKey.concat(keyToCurr, worldToCurr);

    return true;
  }
 /** Makes the current frame the first frame and discards its past history */
 public void reset() {
   worldToKey.set(worldToInit);
   keyToCurr.set(worldToInit);
   worldToCurr.set(worldToInit);
   tracker.reset();
   tracker.spawnTracks();
   tracker.setKeyFrame();
   totalProcessed = 0;
 }
  /**
   * calc a vector orthogonal to the given one
   *
   * @param dir the given vector
   * @return a normalised vector orthogonal to dir
   */
  @SuppressWarnings("unchecked")
  public static <T extends Tuple2d> T getOrthogonalDirection(final T dir) {

    T orth = (T) dir.clone();

    if (dir.x == 0) orth.set(1, 0);
    else if (dir.y == 0) orth.set(0, 1);
    else {
      orth.set(-dir.y / dir.x, 1);
    }

    return normalize(orth);
  }
  /** Make the current frame the first frame in the sequence */
  public void changeKeyFrame() {
    tracker.spawnTracks();
    tracker.setKeyFrame();

    totalSpawned = tracker.getActiveTracks().size();
    worldToKey.set(worldToCurr);
  }
  @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;
  }
 /**
  * Inserts a node before an existing child.
  *
  * @param ref existing child of this node, or {@code null} for append
  * @param child node to insert
  */
 public T insertBefore(T ref, T child) {
   Preconditions.checkArgument(ref == null || ref.parent == this);
   return ref == null
       ? append(child) // \u2620
       : ref == first
           ? prepend(child) // \u2620
           : (ref.prev.next = (ref.prev = child.set(self(), ref.prev, ref)));
 }
 /**
  * Inserts a node after an existing child.
  *
  * @param ref existing child of this node, or {@code null} for prepend
  * @param child node to insert
  */
 public T insertAfter(T ref, T child) {
   Preconditions.checkArgument(ref == null || ref.parent == this);
   return ref == null
       ? prepend(child) // \u2620
       : (ref == last
           ? append(child) // \u2620
           : (ref.next.prev = (ref.next = child.set(self(), ref, ref.next))));
 }
Example #9
0
 @SafeVarargs
 public static <T extends Bits<T>> T with(T start, T... others) {
   T t = with(start);
   for (T e : others) {
     t = t.set(e);
   }
   return t;
 }
Example #10
0
 public void create(T obj) {
   String key = kayVeepUtilities.getKey();
   obj.setId(key);
   this.data.store(obj);
   this.updateMapping(obj);
   Long createdTime = System.currentTimeMillis();
   this.created.store(obj.getId(), createdTime);
   obj.set("created_time", createdTime);
 }
Example #11
0
 public T loadById(String id) {
   T obj = this.data.load(id, this.dataType);
   if (obj != null) {
     // this.load_raw_properties(obj);
     this.updateMapping(obj);
     obj.set("created_time", this.created.load(obj.getId()));
     obj.set("updated_time", this.updated.load(obj.getId()));
   }
   return obj;
 }
  /**
   * calc a normalised copy of vector
   *
   * @param p the vector to normalise
   * @return normalised version
   */
  @SuppressWarnings("unchecked")
  public static <T extends Tuple2d> T normalize(final T p) {
    double length = Math.sqrt(p.getX() * p.getX() + p.getY() * p.getY());

    T x = (T) p.clone();
    x.set(p.getX(), p.getY());
    x.scale(1 / length);

    return x;
  }
Example #13
0
 public void store(T obj) {
   if (obj.getId() == null) {
     create(obj);
     return;
   }
   this.data.store(obj);
   this.updateMapping(obj);
   Long updatedTime = System.currentTimeMillis();
   this.updated.store(obj.getId(), updatedTime);
   obj.set("updated_time", updatedTime);
 }
Example #14
0
  protected void integrateLine(
      final int d, final RandomAccess<T> cursor, final T sum, final long size) {
    // init sum on first pixel that is not zero
    sum.set(cursor.get());

    for (long i = 2; i < size; ++i) {
      cursor.fwd(d);

      sum.add(cursor.get());
      cursor.get().set(sum);
    }
  }
 /**
  * Compute the steepest descent images of the template at the identity warp. Each steepest descent
  * image comprises the partial derivatives of template intensities with respect to one parameter
  * of the warp function.
  *
  * <p>The result is stored in the <em>n+1</em> dimensional {@link #target} image. Dimension
  * <em>n</em> is used to index the partial derivative. For example, the partial derivative by the
  * second parameter of the warp function is stored in slice <em>n=1</em>.
  *
  * @param gradients n+1 dimensional image of partial derivatives of the template. Dimension n is
  *     used to index the partial derivative. For example, the partial derivative by Y is stored in
  *     slice n=1.
  * @param warpFunction The warp function to be applied to the template. The partial derivatives of
  *     template intensities with respect to the parameters of this warp function are computed.
  * @param target Image of <em>n+1</em> dimensions to store the steepest descent Dimension
  *     <em>n</em> is used to index the parameters of the warp function. For example, the partial
  *     derivative of the template image intensity by parameter 2 of the warp function at pixel
  *     <em>(x,y)</em> is stored at position <em>(x,y,1)</em>.
  */
 public static <T extends NumericType<T>> void computeSteepestDescents(
     final RandomAccessibleInterval<T> gradients,
     final WarpFunction warpFunction,
     final RandomAccessibleInterval<T> target) {
   final int n = gradients.numDimensions() - 1;
   final int numParameters = warpFunction.numParameters();
   final T tmp = Util.getTypeFromInterval(gradients).createVariable();
   for (int p = 0; p < numParameters; ++p) {
     for (int d = 0; d < n; ++d) {
       final Cursor<T> gd =
           Views.flatIterable(Views.hyperSlice(gradients, n, d)).localizingCursor();
       for (final T t : Views.flatIterable(Views.hyperSlice(target, n, p))) {
         tmp.set(gd.next());
         tmp.mul(warpFunction.partial(gd, d, p));
         t.add(tmp);
       }
     }
   }
 }
Example #16
0
  public boolean analyzePeak(final DifferenceOfGaussianPeak<T> peak) {
    final int numDimensions = laPlacian.numDimensions();

    // the subpixel values
    final double[] subpixelLocation = new double[numDimensions];

    // the current position for the quadratic fit
    final long[] currentPosition = new long[numDimensions];
    peak.localize(currentPosition);

    // the cursor for the computation (one that cannot move out of Img)
    final RandomAccess<T> cursor;

    if (canMoveOutside) cursor = Views.extendPeriodic(laPlacian).randomAccess();
    else cursor = laPlacian.randomAccess();

    // the current hessian matrix and derivative vector
    Img<DoubleType> hessianMatrix =
        doubleArrayFactory.create(
            new int[] {cursor.numDimensions(), cursor.numDimensions()}, new DoubleType());
    Img<DoubleType> derivativeVector =
        doubleArrayFactory.create(new int[] {cursor.numDimensions()}, new DoubleType());

    // the inverse hessian matrix
    Matrix A, B, X;

    // the current value of the center
    T value = peak.value.createVariable();

    boolean foundStableMaxima = true, pointsValid = false;
    int numMoves = 0;

    // fit n-dimensional quadratic function to the extremum and
    // if the extremum is shifted more than 0.5 in one or more
    // directions we test wheather it is better there
    // until we
    //   - converge (find a stable extremum)
    //   - move out of the Img
    //   - achieved the maximal number of moves

    do {
      ++numMoves;

      // move the cursor to the current positon
      cursor.setPosition(currentPosition);

      // store the center value
      value.set(cursor.get());

      // compute the n-dimensional hessian matrix [numDimensions][numDimensions]
      // containing all second derivatives, e.g. for 3d:
      //
      // xx xy xz
      // yx yy yz
      // zx zy zz
      hessianMatrix = getHessianMatrix(cursor, hessianMatrix);

      // compute the inverse of the hessian matrix
      A = invertMatrix(hessianMatrix);

      if (A == null) return handleFailure(peak, "Cannot invert hessian matrix");

      // compute the n-dimensional derivative vector
      derivativeVector = getDerivativeVector(cursor, derivativeVector);
      B = getMatrix(derivativeVector);

      if (B == null) return handleFailure(peak, "Cannot compute derivative vector");

      // compute the extremum of the n-dimensinal quadratic fit
      X = (A.uminus()).times(B);

      for (int d = 0; d < numDimensions; ++d) subpixelLocation[d] = X.get(d, 0);

      // test all dimensions for their change
      // if the absolute value of the subpixel location
      // is bigger than 0.5 we move into that direction
      foundStableMaxima = true;

      for (int d = 0; d < numDimensions; ++d) {
        // Normally, above an offset of 0.5 the base position
        // has to be changed, e.g. a subpixel location of 4.7
        // would mean that the new base location is 5 with an offset of -0.3
        //
        // If we allow an increasing maxima tolerance we will
        // not change the base position that easily. Sometimes
        // it simply jumps from left to right and back, because
        // it is 4.51 (i.e. goto 5), then 4.49 (i.e. goto 4)
        // Then we say, ok, lets keep the base position even if
        // the subpixel location is 0.6...

        final double threshold = allowMaximaTolerance ? 0.5 + numMoves * maximaTolerance : 0.5;

        if (Math.abs(subpixelLocation[d]) > threshold) {
          if (allowedToMoveInDim[d]) {
            // move to another base location
            currentPosition[d] += Math.signum(subpixelLocation[d]);
            foundStableMaxima = false;
          } else {
            // set it to the position that is maximally away when keeping the current base position
            // e.g. if (0.7) do 4 -> 4.5 (although it should be 4.7, i.e. a new base position of 5)
            // or  if (-0.9) do 4 -> 3.5 (although it should be 3.1, i.e. a new base position of 3)
            subpixelLocation[d] = Math.signum(subpixelLocation[d]) * 0.5;
          }
        }
      }

      // check validity of the new location if there is a need to move
      pointsValid = true;

      if (!canMoveOutside)
        if (!foundStableMaxima)
          for (int d = 0; d < numDimensions; ++d)
            if (currentPosition[d] <= 0 || currentPosition[d] >= laPlacian.dimension(d) - 1)
              pointsValid = false;

    } while (numMoves <= maxNumMoves && !foundStableMaxima && pointsValid);

    if (!foundStableMaxima) return handleFailure(peak, "No stable extremum found.");

    if (!pointsValid) return handleFailure(peak, "Moved outside of the Img.");

    // compute the function value (intensity) of the fit
    double quadrFuncValue = 0;

    for (int d = 0; d < numDimensions; ++d) quadrFuncValue += X.get(d, 0) * B.get(d, 0);

    quadrFuncValue /= 2.0;

    // set the results if everything went well

    // subpixel location
    for (int d = 0; d < numDimensions; ++d)
      peak.setSubPixelLocationOffset((float) subpixelLocation[d], d);

    // pixel location
    peak.setPixelLocation(currentPosition);

    // quadratic fit value
    final T quadraticFit = peak.getImgValue().createVariable();
    quadraticFit.setReal(quadrFuncValue);
    peak.setFitValue(quadraticFit);

    // normal value
    peak.setImgValue(value);

    return true;
  }
Example #17
0
 public static <T extends Bits<T>> T with(T t1, T t2, T t3, T t4, T t5) {
   T t = with(t1, t2, t3, t4);
   return t.set(t5);
 }
Example #18
0
 public static <T extends Bits<T>> T with(T t1, T t2, T t3) {
   T t = with(t1, t2);
   return t.set(t3);
 }
Example #19
0
 public static <T extends TypeWrapper, E> T wrap(T wrapper, E data) {
   wrapper.set(data);
   return wrapper;
 }
 /**
  * Inserts a node as the last child of this node.
  *
  * @param child node to insert
  */
 public T append(T child) {
   return last =
       ((last == null) // \u2620
           ? (first = child.set(self(), null, null)) // \u2620
           : (last.next = child.set(self(), last, null)));
 }
 /**
  * Inserts a node as the first child of this node.
  *
  * @param child node to insert
  */
 public T prepend(T child) {
   return first =
       ((first == null) // \u2620
           ? (last = child.set(self(), null, null)) // \u2620
           : (first.prev = child.set(self(), null, first)));
 }
Example #22
0
 @Override
 public void set(double value) {
   for (T s : slices) {
     s.set(value);
   }
 }
Example #23
0
  /**
   * This method creates a gaussian kernel
   *
   * @param sigma Standard Derivation of the gaussian function in the desired {@link Type}
   * @param normalize Normalize integral of gaussian function to 1 or not...
   * @return T[] The gaussian kernel
   */
  public static <T extends ExponentialMathType<T>> T[] createGaussianKernel1D(
      final T sigma, final boolean normalize) {
    final T[] gaussianKernel;
    int kernelSize;

    final T zero = sigma.createVariable();
    final T two = sigma.createVariable();
    final T one = sigma.createVariable();
    final T minusOne = sigma.createVariable();
    final T two_sq_sigma = zero.createVariable();
    final T sum = sigma.createVariable();
    final T value = sigma.createVariable();
    final T xPos = sigma.createVariable();
    final T cs = sigma.createVariable();

    zero.setZero();
    one.setOne();

    two.setOne();
    two.add(one);

    minusOne.setZero();
    minusOne.sub(one);

    if (sigma.compareTo(zero) <= 0) {
      kernelSize = 3;
      // NB: Need explicit cast to T[] to satisfy javac;
      // See: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6302954
      gaussianKernel = (T[]) genericArray(3); // zero.createArray1D( 3 );
      gaussianKernel[1].set(one);
    } else {
      // size = Math.max(3, (int) (2 * (int) (3 * sigma + 0.5) + 1));
      cs.set(sigma);
      cs.mul(3.0);
      cs.round();
      cs.mul(2.0);
      cs.add(one);

      kernelSize = Util.round(cs.getRealFloat());

      // kernelsize has to be at least 3
      kernelSize = Math.max(3, kernelSize);

      // kernelsize has to be odd
      if (kernelSize % 2 == 0) ++kernelSize;

      // two_sq_sigma = 2 * sigma * sigma;
      two_sq_sigma.set(two);
      two_sq_sigma.mul(sigma);
      two_sq_sigma.mul(sigma);

      // NB: Need explicit cast to T[] to satisfy javac;
      // See: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6302954
      gaussianKernel = (T[]) genericArray(kernelSize); // zero.createArray1D( kernelSize );

      for (int i = 0; i < gaussianKernel.length; ++i) gaussianKernel[i] = zero.createVariable();

      // set the xPos to kernelSize/2
      xPos.setZero();
      for (int x = 1; x <= kernelSize / 2; ++x) xPos.add(one);

      for (int x = kernelSize / 2; x >= 0; --x) {
        // final double val = Math.exp( -(x * x) / two_sq_sigma );
        value.set(xPos);
        value.mul(xPos);
        value.mul(minusOne);
        value.div(two_sq_sigma);
        value.exp();

        gaussianKernel[kernelSize / 2 - x].set(value);
        gaussianKernel[kernelSize / 2 + x].set(value);

        xPos.sub(one);
      }
    }

    if (normalize) {
      sum.setZero();

      for (final T val : gaussianKernel) sum.add(val);

      for (int i = 0; i < gaussianKernel.length; ++i) gaussianKernel[i].div(sum);
    }

    for (int i = 0; i < gaussianKernel.length; ++i) System.out.println(gaussianKernel[i]);

    return gaussianKernel;
  }
  protected void convolve(
      final RandomAccess<T> inputIterator,
      final Cursor<T> outputIterator,
      final int dim,
      final float[] kernel,
      final long startPos,
      final long loopSize) {
    // move to the starting position of the current thread
    outputIterator.jumpFwd(startPos);

    final int filterSize = kernel.length;
    final int filterSizeMinus1 = filterSize - 1;
    final int filterSizeHalf = filterSize / 2;
    final int filterSizeHalfMinus1 = filterSizeHalf - 1;
    final int numDimensions = inputIterator.numDimensions();

    final int iteratorPosition = filterSizeHalf;

    final int[] to = new int[numDimensions];

    final T sum = inputIterator.get().createVariable();
    final T tmp = inputIterator.get().createVariable();

    // do as many pixels as wanted by this thread
    for (long j = 0; j < loopSize; ++j) {
      outputIterator.fwd();

      // set the sum to zero
      sum.setZero();

      //
      // we move filtersize/2 of the convolved pixel in the input container
      //

      // get the current positon in the output container
      outputIterator.localize(to);

      // position in the input container is filtersize/2 to the left
      to[dim] -= iteratorPosition;

      // set the input cursor to this very position
      inputIterator.setPosition(to);

      // System.out.println( "out: " + outputIterator );
      // System.out.println( "iteratorPosition: " + iteratorPosition );
      // System.out.println( "in: " + inputIterator );
      // System.exit ( 0 );

      // iterate over the kernel length across the input container
      for (int f = -filterSizeHalf; f <= filterSizeHalfMinus1; ++f) {
        // get value from the input container
        tmp.set(inputIterator.get());

        // multiply the kernel
        tmp.mul(kernel[f + filterSizeHalf]);

        // add up the sum
        sum.add(tmp);

        // move the cursor forward for the next iteration
        inputIterator.fwd(dim);
      }

      //
      // for the last pixel we do not move forward
      //

      // get value from the input container
      tmp.set(inputIterator.get());

      // multiply the kernel
      tmp.mul(kernel[filterSizeMinus1]);

      // add up the sum
      sum.add(tmp);

      outputIterator.get().set(sum);
    }
  }