1 | sclass MultiLevelRecognizer1 { |
2 | // how big do we think the prototype is in the whole image |
3 | // (percentage by height) |
4 | transient double assumedPrototypeHeightPercentage = 70; |
5 | |
6 | transient JTabbedPane tabs; // initialize if you want visualization |
7 | |
8 | transient BWIntegralImage baseImage; |
9 | transient BWImage prototypeImage; |
10 | transient L<Int> widths = ll(32, 64, 128); |
11 | transient new L<IBWIntegralImage> integralImages; |
12 | transient new L<OneLevel> levels; // recognizers for each granularity |
13 | |
14 | class OneLevel extends SteppableAndBest<Rect> { |
15 | IBWIntegralImage ii; // scaled integral image |
16 | BWImage image; // scaled image |
17 | BWImage prototype; // scaled prototype |
18 | float minSimilarity = 0.5f; |
19 | ImageSurface is; |
20 | |
21 | // candidates are top-left corner of rect to try in our coordinates |
22 | L<Pt> candidatesQueue = syncLinkedList(); |
23 | new Set<Pt> candidatesTried; |
24 | Iterator<Pt> candidatesStream; |
25 | |
26 | *(IBWIntegralImage *ii) { |
27 | image = iBWIntegralImageToBWImage(ii); |
28 | // get assumed height of prototype in scaled-down image |
29 | int ph = iround(ii.getHeight()*assumedPrototypeHeightPercentage/100.0); |
30 | // resize prototype |
31 | prototype = bwResizeToHeightSmooth(prototypeImage, ph); |
32 | addTab(tabs, "proto " + ii.getWidth(), |
33 | jFullCenterScroll(jPixelatedZoomedImageSurface(4.0, prototype))); |
34 | candidatesStream = mapI rectTopLeftCorner(allSubRectsOfSizeIterator(prototype.getWidth(), prototype.getHeight(), |
35 | imageRect(image))); |
36 | } |
37 | |
38 | public bool step() { |
39 | Pt p = nextCandidate(); |
40 | if (p != null) ret true with tryCandidate(p); |
41 | false; |
42 | } |
43 | |
44 | Pt nextCandidate() { |
45 | try object Pt p = popFirst(candidatesQueue); |
46 | ret nextFromIterator(candidatesStream); |
47 | } |
48 | |
49 | void tryCandidate(Pt p) { |
50 | if (!candidatesTried.add(p)) ret; |
51 | |
52 | int x = p.x, y = p.y, wp = prototype.getWidth(), hp = prototype.getHeight(); |
53 | |
54 | float maxError = (1f-minSimilarity)*wp*hp; |
55 | float diff = bwImageSectionsSimilarity(image, prototype, x, y, maxError); |
56 | if (diff <= maxError) |
57 | best.put(new Rect(x, y, wp, hp), 1-diff/(wp*hp)); |
58 | } |
59 | |
60 | void showBest() { |
61 | setImageSurfaceSelection(is, best!); |
62 | } |
63 | |
64 | // best rect in original image coordinates |
65 | Rect bestRescaled() { |
66 | ret rescaleRect(best!, ii.getWidth(), ii.getHeight(), |
67 | baseImage.getWidth(), baseImage.getHeight()); |
68 | } |
69 | |
70 | Scored<Rect> scoredBestRescaled() { |
71 | ret scored(bestRescaled(), best.score); |
72 | } |
73 | } |
74 | |
75 | *() {} |
76 | *(File prototypeImage, File imgFile) { |
77 | this.prototypeImage = loadBWImage(prototypeImage); |
78 | this.baseImage = loadBWIntegralImage(imgFile); |
79 | } |
80 | |
81 | Scored<Rect> go() { |
82 | assertNotNull(+baseImage); |
83 | assertNotNull(+prototypeImage); |
84 | |
85 | for (int w : widths) { |
86 | IBWIntegralImage ii = scaledIBWIntegralImage(baseImage, w); |
87 | integralImages.add(ii); |
88 | OneLevel lvl = new(ii); |
89 | levels.add(lvl); |
90 | if (tabs != null) { |
91 | ImageSurface is = jPixelatedZoomedImageSurface( |
92 | doubleRatio(baseImage.getWidth(), w), iBWIntegralImageToBWImage(ii)); |
93 | addTab(tabs, "w=" + w, jFullCenterScroll(is)); |
94 | lvl.is = is; |
95 | } |
96 | } |
97 | if (tabs != null) |
98 | addTab(tabs, "Prototype", jFullCenterScroll(jPixelatedZoomedImageSurface(4, prototypeImage))); |
99 | |
100 | time "Process" { |
101 | print("Steps: " + stepAll_roundRobin(levels)); |
102 | } |
103 | |
104 | if (tabs != null) |
105 | for (OneLevel l : levels) l.showBest(); |
106 | |
107 | ret bestResult(); |
108 | } |
109 | |
110 | Scored<Rect> bestResult() { |
111 | ret last(levels).scoredBestRescaled(); |
112 | } |
113 | } |
Began life as a copy of #1027225
download show line numbers debug dex old transpilations
Travelled to 7 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tvejysmllsmz, vouqrxazstgt, xrpafgyirdlv
No comments. add comment
Snippet ID: | #1027229 |
Snippet name: | MultiLevelRecognizer1 (backup 1, single size) |
Eternal ID of this version: | #1027229/1 |
Text MD5: | 119c1753957400d30d10ec621d81ac42 |
Author: | stefan |
Category: | javax / image recognition |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2020-02-26 14:12:29 |
Source code size: | 3694 bytes / 113 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 205 / 236 |
Referenced in: | [show references] |