@Test
  public void checkNeighbors() {
    int r = 3;
    Dummy alg = new Dummy(r, 2, 200, ImageUInt8.class);

    alg.scores = new float[20 * 30];
    ImageFlow flows = new ImageFlow(20, 30);
    ImageFlow.D tmp = new ImageFlow.D();

    tmp.valid = true;
    tmp.x = -1;
    tmp.y = 2;

    // checks to see if a pixel is invalid that it's flow is always set
    // if a pixel is valid then the score is only set if the score is better
    flows.get(6, 5).valid = true;
    alg.scores[5 * 20 + 6] = 10;
    flows.get(5, 5).valid = true;
    alg.scores[5 * 20 + 5] = 4;
    // same score, but more motion
    flows.get(5, 6).valid = true;
    alg.scores[6 * 20 + 5] = 5;
    flows.get(5, 6).x = 2;
    flows.get(5, 6).y = 2;
    // same score, but less motion
    flows.get(6, 6).valid = true;
    alg.scores[6 * 20 + 6] = 5;
    flows.get(6, 6).x = 0;
    flows.get(6, 6).y = 1;

    alg.checkNeighbors(6, 7, tmp, flows, 5);

    for (int i = -r; i <= r; i++) {
      for (int j = -r; j <= r; j++) {
        int x = j + 6;
        int y = i + 7;

        ImageFlow.D f = flows.get(x, y);

        assertTrue(f.valid);
        if (x == 5 && y == 5) {
          assertEquals(4, alg.scores[y * 20 + x], 1e-4);
          assertEquals(0, f.x, 1e-4);
          assertEquals(0, f.y, 1e-4);
        } else if (x == 6 && y == 6) {
          assertEquals(5, alg.scores[y * 20 + x], 1e-4);
          assertEquals(0, f.x, 1e-4);
          assertEquals(1, f.y, 1e-4);
        } else {
          assertEquals(x + " " + y, 5, alg.scores[y * 20 + x], 1e-4);
          assertEquals(-1, f.x, 1e-4);
          assertEquals(2, f.y, 1e-4);
        }
      }
    }
  }
示例#2
0
  /**
   * Computes the optical flow form 'prev' to 'curr' and stores the output into output
   *
   * @param prev Previous image
   * @param curr Current image
   * @param output Dense optical flow output
   */
  public void process(T prev, T curr, ImageFlow output) {

    InputSanityCheck.checkSameShape(prev, curr);
    output.invalidateAll();

    int N = prev.width * prev.height;
    if (scores.length < N) scores = new float[N];

    int r = searchRadius + regionRadius;
    int x1 = prev.width - r;
    int y1 = prev.height - r;

    for (int y = r; y < y1; y++) {
      for (int x = r; x < x1; x++) {
        extractTemplate(x, y, prev);
        float score = findFlow(x, y, curr, tmp);

        if (tmp.valid) checkNeighbors(x, y, tmp, output, score);
      }
    }

    // TODO process image border
  }