Libraryless. Click here for Pure Java version (4188L/28K/93K).
1 | !752 |
2 | |
3 | static BWImage img; |
4 | static ImageSurface is2; |
5 | static float threshold = 0.9f; |
6 | |
7 | p { |
8 | img = loadBWImage("#1005702"); |
9 | update(); |
10 | } |
11 | |
12 | svoid update() { |
13 | float[] f = columnBrightnesses(img); |
14 | //quickShowZoomedImage(img.toRGB()); |
15 | //showZoomedImage(brightnessesAsImage(f).toRGB()); |
16 | L<Streak> streaks = makeStreaks(f, threshold); |
17 | //printStruct(f); |
18 | //printStruct(streaks); |
19 | print("Threshold: " + threshold + ", streaks: " + l(streaks)); |
20 | RGBImage vis = visualizeStreaks(img, streaks).toRGB(); |
21 | if (is2 == null) { |
22 | is2 = showZoomedImage(vis); |
23 | addToWindowAndPack(is2, liveSlider(threshold/10000f*9999f, voidfunc(float x) { |
24 | threshold = x*10000f/9999f; |
25 | update(); |
26 | })); |
27 | } else |
28 | is2.setImage(vis); |
29 | } |
30 | |
31 | // f: voidfunc(float) |
32 | static JSlider liveSlider(float def, final O f) { |
33 | final int max = 9999; |
34 | final JSlider slider = new JSlider(0, max, iround(def*max)); |
35 | slider.addChangeListener(new ChangeListener { |
36 | int lastValue = slider.getValue(); |
37 | public void stateChanged(ChangeEvent e) { |
38 | int value = slider.getValue(); |
39 | if (value != lastValue) { |
40 | lastValue = value; |
41 | pcallF(f, value/(float) max); |
42 | } |
43 | } |
44 | }); |
45 | ret slider; |
46 | } |
47 | |
48 | sclass Streak { |
49 | int from, to; // to = exclusive |
50 | |
51 | *() {} |
52 | *(int *from, int *to) {} |
53 | } |
54 | |
55 | static L<Streak> makeStreaks(float[] f, float threshold) { |
56 | new L<Streak> l; |
57 | int j = -1; |
58 | for (int i = 0; i < l(f); i++) |
59 | if (f[i] < threshold) { // start / continue streak |
60 | if (j < 0) j = i; |
61 | } else { |
62 | if (j >= 0) { // end streak |
63 | l.add(new Streak(j, i)); |
64 | j = -1; |
65 | } |
66 | } |
67 | if (j >= 0) |
68 | l.add(new Streak(j, l(f))); |
69 | ret l; |
70 | } |
71 | |
72 | static float[] columnBrightnesses(BWImage img) { |
73 | int w = img.getWidth(), h = img.getHeight(); |
74 | float[] f = new float[w]; |
75 | for x to w: |
76 | f[x] = (float) img.clip(x, 0, 1, h).averageBrightness(); |
77 | ret f; |
78 | } |
79 | |
80 | static BWImage brightnessesAsImage(float[] f) { |
81 | int w = f.length, h = 20; |
82 | BWImage img = new BWImage(w, h); |
83 | for y to h: for x to w: img.setPixel(x, y, f[x]); |
84 | ret img; |
85 | } |
86 | |
87 | static float[] streaksToFloats(int w, L<Streak> l) { |
88 | float[] f = new float[w]; |
89 | for (Streak s : l) |
90 | for (int i = s.from; i < s.to; i++) |
91 | f[i] = 1f; |
92 | ret f; |
93 | } |
94 | |
95 | static BWImage visualizeStreaks(BWImage img, L<Streak> streaks) { |
96 | float gapColor = 0.5f; |
97 | int w = img.getWidth(), h = img.getHeight(); |
98 | BWImage im = new BWImage(w, h*2, gapColor); |
99 | float[] f = streaksToFloats(w, streaks); |
100 | |
101 | for x to w: |
102 | copyBWImage(img, x, 0, im, x, (int) (f[x]*h), 1, h); |
103 | |
104 | // auto-crop characters |
105 | for (Streak s : streaks) { |
106 | Rect r = autoCropOfBWImage(im, new Rect(s.from, h, s.to-s.from, h)); |
107 | darkenBWImagePart(im, r); |
108 | } |
109 | ret im; |
110 | } |
Began life as a copy of #1005703
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: | #1005706 |
Snippet name: | Test splitting text image into characters with auto-cropping characters |
Eternal ID of this version: | #1005706/1 |
Text MD5: | 67439e2a1ae91eacb017ac8cd6f55bf5 |
Transpilation MD5: | 5123e980abab9d9b8d3211c77c8a2e92 |
Author: | stefan |
Category: | javax / ocr |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2016-12-04 13:21:44 |
Source code size: | 2805 bytes / 110 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 425 / 528 |
Referenced in: | [show references] |