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)

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

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: 512 / 570
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)
#1022952 - bwCentroid2 - get centroid of dark or light pixels in BWImage (better version?)