Libraryless. Click here for Pure Java version (5559L/36K).
1 | sclass CheesyRecognizer { |
2 | BWIntegralImage inputImage; |
3 | |
4 | new L<Prototype> prototypes; |
5 | new Map<Int, ScaledInputImage> scaledInputImages; // key = width |
6 | |
7 | class Prototype { |
8 | BWIntegralImage image; |
9 | new Map<Int, ScaledPrototype> scaledPrototypes; // key = width |
10 | |
11 | *(BWImage image) { this.image = BWIntegralImage(image); } |
12 | |
13 | ScaledPrototype scaledPrototype(int width) { |
14 | ret mapGetOrCreate(scaledPrototypes, width, |
15 | () -> new ScaledPrototype(this, scaleDownUsingIntegralImageBW(image, width))); |
16 | } |
17 | } |
18 | |
19 | class ScaledInputImage { |
20 | BWImage image; |
21 | |
22 | *(BWImage *image) {} |
23 | |
24 | Rect scaleToInputImage(Rect r) { |
25 | ret scaleRect(doubleRatio(inputImage.getWidth(), image.getWidth()), r); |
26 | } |
27 | |
28 | int getHeight() { ret image.getHeight(); } |
29 | } |
30 | |
31 | class ScaledPrototype { |
32 | Prototype prototype; |
33 | BWImage image; |
34 | |
35 | *(Prototype *prototype, BWImage *image) {} |
36 | |
37 | int getHeight() { ret image.getHeight(); } |
38 | } |
39 | |
40 | *(BWImage inputImage) { this.inputImage = bwIntegralImage(inputImage); } |
41 | *(BufferedImage inputImage) { this(BWImage(inputImage)); } |
42 | |
43 | void fullScan(ScaledInputImage img, ScaledPrototype proto, float minSimilarity, IVF2<Rect, Double> onFound) { |
44 | bwImageSearch_onFound_allCandidates(img.image, proto.image, minSimilarity, onFound); |
45 | } |
46 | |
47 | LPair<Rect, Double> fullScanToNBest(ScaledInputImage img, ScaledPrototype proto, float minSimilarity, int n) { |
48 | new LPair<Rect, Double> list; |
49 | fullScan(img, proto, minSimilarity, (r, score) -> list.add(pair(r, score))); |
50 | sortBySecondOfPairsDesc_inPlace(list); |
51 | ret takeFirst(n, list); |
52 | } |
53 | |
54 | // compare scaled prototype to scaled image at some location |
55 | Pair<Rect, Double> singleCheck(ScaledInputImage img, ScaledPrototype prototype, int x, int y, float minSimilarity) { |
56 | BWImage proto = prototype.image, image = img.image; |
57 | int wp = proto.getWidth(), hp = proto.getHeight(); |
58 | |
59 | float sim = bwImageSectionsSimilarity2(image, proto, x, y, minSimilarity); |
60 | ret sim < minSimilarity ? null : pair(new Rect(x, y, wp, hp), (double) sim); |
61 | } |
62 | |
63 | ScaledInputImage scaledInputImage(int width) { |
64 | ret mapGetOrCreate(scaledInputImages, width, |
65 | () -> new ScaledInputImage(scaleDownUsingIntegralImageBW(inputImage, width))); |
66 | } |
67 | |
68 | Prototype addPrototype(BWImage img) { |
69 | ret addAndReturn(prototypes, new Prototype(img)); |
70 | } |
71 | |
72 | LPair<Rect, Double> fullScanToNBest(Prototype proto, int imageWidth, int protoWidth, float minSimilarity, int n) { |
73 | ScaledInputImage scaled = scaledInputImage(imageWidth); |
74 | ret mapPairsA(r -> scaled.scaleToInputImage(r), |
75 | fullScanToNBest(scaled, proto.scaledPrototype(protoWidth), 0f, n)); |
76 | } |
77 | |
78 | int getWidth() { ret inputImage.getWidth(); } |
79 | int getHeight() { ret inputImage.getHeight(); } |
80 | } |
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: | #1027278 |
Snippet name: | CheesyRecognizer - cheaty image recognition by scanning for prototype images [dev.] |
Eternal ID of this version: | #1027278/33 |
Text MD5: | 9d7eeddb86f3dfa5d43783f0a5bb7660 |
Transpilation MD5: | 465da5a721e314a71e8195211012e8d1 |
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-29 17:21:55 |
Source code size: | 2878 bytes / 80 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 304 / 752 |
Version history: | 32 change(s) |
Referenced in: | [show references] |