Libraryless. Click here for Pure Java version (3467L/23K/79K).
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 streaksToGaps(int w, L<Streak> l) { |
96 | ret makeStreaks(streaksToFloats(w, l), 0.5f); |
97 | }*/ |
98 | |
99 | /*static BWImage visualizeStreaks(BWImage img, L<Streak> streaks) { |
100 | int gap = 5; |
101 | float gapColor = 0.5f; |
102 | int w = img.getWidth(), h = img.getHeight(); |
103 | L<Streak> gaps = streaksToGaps(w, streaks); |
104 | int n = l(gaps); |
105 | int w2 = w+n*gap; |
106 | BWImage im = new BWImage(w2, gapColor); |
107 | |
108 | copyBWImage(img, |
109 | ret im; |
110 | }*/ |
111 | |
112 | static BWImage visualizeStreaks(BWImage img, L<Streak> streaks) { |
113 | float gapColor = 0.5f; |
114 | int w = img.getWidth(), h = img.getHeight(); |
115 | BWImage im = new BWImage(w, h*2, gapColor); |
116 | float[] f = streaksToFloats(w, streaks); |
117 | for x to w: |
118 | copyBWImage(img, x, 0, im, x, (int) (f[x]*h), 1, h); |
119 | ret im; |
120 | } |
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: | #1005703 |
Snippet name: | Test splitting text image into characters |
Eternal ID of this version: | #1005703/1 |
Text MD5: | 2330277a0102b1931f5dd7e2e6931981 |
Transpilation MD5: | 703f40da3b26d9bdcf9613e5daa29a1f |
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 01:02:42 |
Source code size: | 3061 bytes / 120 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 539 / 689 |
Referenced in: | [show references] |