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

40
LINES

< > BotCompany Repo | #1004640 // unclusterImages

JavaX fragment (include)

1  
static L<BWImage> unclusterImages(BWImage img) {
2  
  int w = img.getWidth(), h = img.getHeight();
3  
  L<Int> bc = unclusterImages_findBlackCols(img);
4  
  L<Int> br = unclusterImages_findBlackRows(img);
5  
  if (!bc.contains(0) || !br.contains(0)
6  
    || !bc.contains(w-1) || !br.contains(h-1))
7  
    null;
8  
  new L<BWImage> images;
9  
  for (int y = 1; y < l(br)-1; y++)
10  
    for (int x = 1; x < l(bc)-1; x++) {
11  
      int x1 = bc.get(x)+1, cw = bc.get(x+1)-x1;
12  
      int y1 = br.get(y)+1, ch = br.get(y+1)-y1;
13  
      images.add(img.clip(x1, y1, cw, ch));
14  
    }
15  
  ret images;
16  
}
17  
18  
static L<Int> unclusterImages_findBlackCols(BWImage img) {
19  
  new L<Int> l;
20  
  int w = img.getWidth(), h = img.getHeight();
21  
  outer: for (int x = 0; x < w; x++) {
22  
    for (int y = 0; y < h; y++)
23  
      if (img.getPixel(x, y) > 0.1f)
24  
        continue outer;
25  
    l.add(x);
26  
  }
27  
  ret l;
28  
}
29  
30  
static L<Int> unclusterImages_findBlackRows(BWImage img) {
31  
  new L<Int> l;
32  
  int w = img.getWidth(), h = img.getHeight();
33  
  outer: for (int y = 0; y < h; y++) {
34  
    for (int x = 0; x < w; x++)
35  
      if (img.getPixel(x, y) > 0.1f)
36  
        continue outer;
37  
    l.add(y);
38  
  }
39  
  ret l;
40  
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1004640
Snippet name: unclusterImages
Eternal ID of this version: #1004640/1
Text MD5: 3f07a8e4beda796e0a03f88b76d0f234
Author: stefan
Category: javax / images
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-08-24 17:26:02
Source code size: 1165 bytes / 40 lines
Pitched / IR pitched: No / No
Views / Downloads: 572 / 553
Referenced in: [show references]