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

55
LINES

< > BotCompany Repo | #1006185 // Find centroids

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (4476L/30K/107K).

1  
!752
2  
3  
static int viewSize = 20;
4  
5  
static L<Pt> findCentroids(BWImage img, int size, bool dark) {
6  
  float minOffset = 0f;
7  
  int maxLoop = 3;
8  
  int w = img.w()-size, h = img.h()-size;
9  
  new L<Pt> points;
10  
  if (w < 0 || h < 0) ret points;
11  
  Pt[][] centroids = new Pt[h][w];
12  
  for (int y = 0; y < h; y++)
13  
    for (int x = 0; x < w; x++) {
14  
      Rect r = new Rect(x, y, size, size);
15  
      Pair<Float, Float> c = bwCentroid(img.clip(r), dark);
16  
      float cx = c.a-size/2, cy = c.b-size/2;
17  
      centroids[y][x] = new Pt(
18  
        x+signWithThreshold(cx, minOffset),
19  
        y+signWithThreshold(cy, minOffset));
20  
    }
21  
    
22  
  for (int y = 0; y < h; y++)
23  
    for (int x = 0; x < w; x++) {
24  
      int n = 0;
25  
      int xx = x, yy = y;
26  
      while (n++ < maxLoop) {
27  
        Pt p = centroids[yy][xx];
28  
        xx = p.x; yy = p.y;
29  
        if (xx < 0 || xx >= w || yy < 0 || yy >= h) break;
30  
        if (x == xx && y == yy) {
31  
          points.add(new Pt(x+viewSize/2, y+viewSize/2));
32  
          break;
33  
        }
34  
      }
35  
    }
36  
  
37  
  ret points;
38  
}
39  
40  
p {
41  
  screenQuickSelect(voidfunc(BufferedImage screen, Rect r) {
42  
    final BWImage img = new BWImage(clipBufferedImage(screen, r));
43  
    thread "Calc" {
44  
      print("Finding centroids... (size=\*viewSize*/, \*img.w()*/*\*img.h()*/ px)");
45  
      L<Pt> dark = findCentroids(img, viewSize, true);
46  
      printStruct("dark", dark);
47  
      L<Pt> light = findCentroids(img, viewSize, false);
48  
      printStruct("light", light);
49  
      RGBImage rgb = img.toRGB();
50  
      markPointsInRGBImage(rgb, dark);
51  
      markPointsInRGBImage(rgb, light, Color.green);
52  
      showImage(rgb);
53  
    }
54  
  });
55  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, sawdedvomwva, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1006185
Snippet name: Find centroids
Eternal ID of this version: #1006185/1
Text MD5: df2ca167465c9787cb8ce718ba8eeae2
Transpilation MD5: 503f8ec5a89a43c2571994e6297316a9
Author: stefan
Category: javax / imaging
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-12-22 22:33:42
Source code size: 1658 bytes / 55 lines
Pitched / IR pitched: No / No
Views / Downloads: 467 / 538
Referenced in: [show references]