1 | static float autoCropOfBWImage_threshold = 0.9f; |
2 | |
3 | static Rect autoCropOfBWImage(BWImage img) {
|
4 | ret autoCropOfBWImage(img, new Rect(0, 0, img.getWidth(), img.getHeight())); |
5 | } |
6 | |
7 | static Rect autoCropOfBWImage(BWImage img, Rect r) {
|
8 | ret autoCropOfBWImage(img, r, autoCropOfBWImage_threshold); |
9 | } |
10 | |
11 | // r = initial rectangle |
12 | static Rect autoCropOfBWImage(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 | } |
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: | #1005705 |
| Snippet name: | autoCropOfBWImage - returns Rect |
| Eternal ID of this version: | #1005705/1 |
| Text MD5: | e541664cbf192e2bdd5a43805da4bd9b |
| Author: | stefan |
| Category: | javax / ocr |
| Type: | JavaX fragment (include) |
| Public (visible to everyone): | Yes |
| Archived (hidden from active list): | No |
| Created/modified: | 2016-12-11 07:57:09 |
| Source code size: | 1234 bytes / 59 lines |
| Pitched / IR pitched: | No / No |
| Views / Downloads: | 783 / 818 |
| Referenced in: | [show references] |