Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

51
LINES

< > BotCompany Repo | #1035325 // GoodThingsToTrack [dev.]

JavaX fragment (include) [tags: use-pretranspiled]

Uses 652K of libraries. Click here for Pure Java version (10695L/62K).

// see https://nanonets.com/blog/optical-flow/

srecord noeq GoodThingsToTrack(BWImage image) {
  settable int windowSize = 3;
  settable double scoreFactor = 1/256.0;
  gettable BWImage outputImage;
  
  gettable double maxScore;
  
  run {
    int w = image.getWidth(), h = image.getHeight();
    int wn = windowSize*windowSize;
    int ofs = (windowSize+1)/2;
    outputImage = new BWImage(w, h);
    for (int y = 1; y < h-windowSize-1; y++) {
      for (int x = 1; x < w-windowSize-1; x++) {
        double[] ix = new[wn];
        double[] iy = new[wn];
        int iw = 0;
        for yy to windowSize:
          for xx to windowSize: {
            //double value = image.getInt(x+xx, y+yy);
            double left = image.getInt(x+xx-1, y+yy);
            double right = image.getInt(x+xx+1, y+yy);
            double above = image.getInt(x+xx, y+yy-1);
            double below = image.getInt(x+xx, y+yy+1);
            //ix[iw] = right-value;
            //iy[iw] = below-value;
            ix[iw] = right-left;
            iy[iw] = below-above;
            iw++;
          }
        
        double sideSum = 0;
        for i to wn: sideSum += ix[i]*iy[i];
        double xSum = 0;
        for i to wn: xSum += sqr(ix[i]);
        double ySum = 0;
        for i to wn: ySum += sqr(iy[i]);
        double[][] matrix = new double[][]
          { { xSum, sideSum }, { sideSum, ySum } };
        Complex[] lambdas = eigenvaluesOfSymmetricMatrix(matrix);
        if (l(lambdas) == 2) {
          double score = min(abs(lambdas[0]), abs(lambdas[1]));
          if (score > maxScore) maxScore = score;
          outputImage.setInt(x+ofs, y+ofs, iround(score*scoreFactor));
        }
      }
    }
  }
}

download  show line numbers  debug dex  old transpilations   

Travelled to 4 computer(s): bhatertpkbcr, ekrmjmnbrukm, mowyntqkapby, mqqgnosmbjvj

No comments. add comment

Snippet ID: #1035325
Snippet name: GoodThingsToTrack [dev.]
Eternal ID of this version: #1035325/8
Text MD5: a5bacde6d91a76c47172e0b8f1b97e32
Transpilation MD5: 77276ef832516f30c973e61acfc5852b
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-05-01 04:16:51
Source code size: 1755 bytes / 51 lines
Pitched / IR pitched: No / No
Views / Downloads: 164 / 263
Version history: 7 change(s)
Referenced in: [show references]