Libraryless. Click here for Pure Java version (2676L/17K/57K).
1 | !752 |
2 | |
3 | static Map<BWImage, S> chars; |
4 | sbool debug; |
5 | |
6 | !include #1000522 // helper functions |
7 | !include #1004653 // Lowest |
8 | |
9 | static S recognizeWord(BWImage iw, O infos) { |
10 | chars = (Map) quickImport(get(infos, "chars")); |
11 | debug = isTrue(getOpt(infos, "debug")); |
12 | |
13 | int w = iw.getWidth(); |
14 | new StringBuilder buf; |
15 | xloop: for (int x = 0; x < w; x++) { |
16 | if (debug) |
17 | print("Best match at " + x + ": " + structure(bestMatch(iw, x))); |
18 | for (BWImage ic : keys(chars)) { |
19 | if (preciseMatch(ic, iw, x)) { |
20 | buf.append(chars.get(ic)); |
21 | x += ic.getWidth()-1; |
22 | continue xloop; |
23 | } |
24 | } |
25 | } |
26 | ret str(buf); |
27 | } |
28 | |
29 | static Pair<S, Double> bestMatch(BWImage iw, int x) { |
30 | new Lowest<S> best; |
31 | for (BWImage ic : keys(chars)) { |
32 | double d = diff(ic, iw.clip(x, 0, ic.getWidth(), ic.getHeight())); |
33 | best.update(chars.get(ic), d); |
34 | } |
35 | ret best.getPair(); |
36 | } |
37 | |
38 | static bool preciseMatch(BWImage ic, BWImage iw, int x1) { |
39 | int w = iw.getWidth(), h = iw.getHeight(); |
40 | if (ic.getHeight() != h) false; |
41 | int x2 = x1+ic.getWidth(); |
42 | if (x2 > w) false; |
43 | for (int x = x1; x < x2; x++) |
44 | for (int y = 0; y < h; y++) |
45 | if (ic.getPixel(x-x1, y) != iw.getPixel(x, y)) false; |
46 | true; |
47 | } |
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: | #1004663 |
Snippet name: | Test Algorithm For Text Recognition |
Eternal ID of this version: | #1004663/1 |
Text MD5: | be227ed902736512aa93d3999d0cd2ab |
Transpilation MD5: | 74cdc2f4c79136e614a51f21af87cd54 |
Author: | stefan |
Category: | javax / a.i. |
Type: | JavaX source code |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2017-01-05 11:09:15 |
Source code size: | 1249 bytes / 47 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 503 / 787 |
Referenced in: | [show references] |