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

59
LINES

< > BotCompany Repo | #1006555 // autoCropOfBWImage_blackBG - returns Rect

JavaX fragment (include)

1  
static float autoCropOfBWImage_blackBG_threshold = 0.2f;
2  
3  
static Rect autoCropOfBWImage_blackBG(BWImage img) {
4  
  ret autoCropOfBWImage(img, new Rect(0, 0, img.getWidth(), img.getHeight()));
5  
}
6  
7  
static Rect autoCropOfBWImage_blackBG(BWImage img, Rect r) {
8  
  ret autoCropOfBWImage(img, r, autoCropOfBWImage_blackBG_threshold);
9  
}
10  
11  
// r = initial rectangle
12  
static Rect autoCropOfBWImage_blackBG(BWImage img, Rect r, float threshold) {
13  
  int x1 = r.x, y1 = r.y, x2 = r.x2(), y2 = r.y2();
14  
15  
  // left side
16  
17  
  end1:
18  
  while (x1 < x2) {
19  
    for (int y = y1; y < y2; y++)
20  
      if (img.getPixel(x1, y) >= threshold)
21  
        break end1;
22  
    x1++;
23  
  }
24  
25  
  // top side
26  
27  
  end2:
28  
  while (y1 < y2) {
29  
    for (int x = x1; x < x2; x++)
30  
      if (img.getPixel(x, y1) >= threshold)
31  
        break end2;
32  
    y1++;
33  
  }
34  
35  
  // right side
36  
37  
  end3:
38  
  while (x2 > x1) {
39  
    for (int y = y1; y < y2; y++)
40  
      if (img.getPixel(x2-1, y) >= threshold)
41  
        break end3;
42  
    x2--;
43  
  }
44  
45  
  // bottom side
46  
47  
  end4:
48  
  while (y2 > y1) {
49  
    for (int x = x1; x < x2; x++)
50  
      if (img.getPixel(x, y2-1) >= threshold)
51  
        break end4;
52  
    y2--;
53  
  }
54  
55  
  if (x2 <= x1 || y2 <= y1)
56  
    return r;
57  
  else
58  
    return new Rect(x1, y1, x2 - x1, y2 - y1);
59  
}

Author comment

Began life as a copy of #1005705

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1006555
Snippet name: autoCropOfBWImage_blackBG - returns Rect
Eternal ID of this version: #1006555/1
Text MD5: 8f2b5d25c0db6394d6e5090ff2c6f274
Author: stefan
Category: javax / ocr
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-01-16 12:31:56
Source code size: 1274 bytes / 59 lines
Pitched / IR pitched: No / No
Views / Downloads: 500 / 520
Referenced in: [show references]