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

14
LINES

< > BotCompany Repo | #1005817 // bwCentroid - get centroid of dark or light pixels in BWImage

JavaX fragment (include)

1  
// returns pair of x and y
2  
static Pair<Float, Float> bwCentroid(BWImage img, bool dark) {
3  
  int w = img.getWidth(), h = img.getHeight();
4  
  double sumX = 0, sumY = 0;
5  
  double midX = (w-1)/2.0, midY = (h-1)/2.0; // optimized for odd image dimensions
6  
  for y to h: for x to w: {
7  
    float weight = img.getPixel(x, y);
8  
    if (dark) weight = 1-weight;
9  
    sumX += weight*(x-midX);
10  
    sumY += weight*(y-midY);
11  
  }
12  
  double baseX = midX*h, baseY = midY*w;
13  
  ret new Pair((float) (midX+sumX/baseX), (float) (midY+sumY/baseY));
14  
}

Author comment

Began life as a copy of #1005814

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: #1005817
Snippet name: bwCentroid - get centroid of dark or light pixels in BWImage
Eternal ID of this version: #1005817/1
Text MD5: 2946ffee8aec0b1fb56a230021188c47
Author: stefan
Category: javax / imaging
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-12-10 15:46:45
Source code size: 536 bytes / 14 lines
Pitched / IR pitched: No / No
Views / Downloads: 513 / 571
Referenced in: [show references]