Uses 108K of libraries. Click here for Pure Java version (13374L/82K).
1 | do not include class IntegralImage. |
2 | do not include class IIntegralImage. |
3 | |
4 | srecord noeq MinimalRecognizer(BufferedImage inputImage) { |
5 | replace Channel with int. |
6 | |
7 | IIntegralImage mainImage; |
8 | |
9 | static final int grayscale = 3; // channel number for grayscale |
10 | |
11 | abstract class IIntegralImage { |
12 | // width and height of image |
13 | int w, h; |
14 | |
15 | abstract double integralValue(int x, int y, Channel channel); |
16 | |
17 | BufferedImage render() { |
18 | ret imageFromFunction(w, h, (x, y) -> rgbPixel(x, y, x+1, y+1) | fullAlphaMask()); |
19 | } |
20 | |
21 | double getPixel(Rect r, int channel) { |
22 | ret getPixel(r.x, r.y, r.x2(), r.y2(), channel); |
23 | } |
24 | |
25 | double getPixel(int channel) { ret getPixel(0, 0, w, h, channel); } |
26 | |
27 | // return value ranges from 0 to 1 (usually) |
28 | double getPixel(int x1, int y1, int x2, int y2, int channel) { |
29 | ret doubleRatio(rectSum(x1, y1, x2, y2, channel), (x2-x1)*(y2-y1)*255.0); |
30 | } |
31 | |
32 | double rectSum(Rect r, int channel) { |
33 | ret rectSum(r.x, r.y, r.x2(), r.y2(), channel); |
34 | } |
35 | |
36 | double rectSum(int x1, int y1, int x2, int y2, int channel) { |
37 | double bottomLeft = integralValue(x1-1, y2-1, channel); |
38 | double bottomRight = integralValue(x2-1, y2-1, channel); |
39 | double topLeft = integralValue(x1-1, y1-1, channel); |
40 | double topRight = integralValue(x2-1, y1-1, channel); |
41 | ret bottomRight-topRight-bottomLeft+topLeft; |
42 | } |
43 | |
44 | int rgbPixel(int x1, int y1, int x2, int y2) { |
45 | int r = iround(clampZeroToOne(getPixel(x1, y1, x2, y2, 0))*255); |
46 | int g = iround(clampZeroToOne(getPixel(x1, y1, x2, y2, 1))*255); |
47 | int b = iround(clampZeroToOne(getPixel(x1, y1, x2, y2, 2))*255); |
48 | ret rgbInt(r, g, b); |
49 | } |
50 | |
51 | double liveliness(int channel) { |
52 | // optimization (but no change in semantics): |
53 | // if (w <= 1 && h <= 1) ret 0; // liveliness of single pixel is 0 |
54 | ret standardDeviation(map(q -> q.getPixel(channel), quadrants())); |
55 | } |
56 | |
57 | L<IIntegralImage> descentShapes() { |
58 | ret centerPlusQuadrants(); |
59 | } |
60 | |
61 | L<IIntegralImage> centerPlusQuadrants() { |
62 | int midX = w/2, midY = h/2; |
63 | Rect r = rectAround(midX, midY, max(midX/2, 1), max(midY/2, 1)); |
64 | ret itemPlusList(clip(r), quadrants()); |
65 | } |
66 | |
67 | L<IIntegralImage> quadrants() { |
68 | if (w <= 1 && h <= 1) null; // let's really not have quadrants of a single pixel |
69 | int midX = w/2, midY = h/2; |
70 | ret mapLL clip( |
71 | rect(0, 0, max(midX, 1), max(midY, 1)), |
72 | rect(midX, 0, w-midX, max(midY, 1)), |
73 | rect(0, midY, max(midX, 1), h-midY), |
74 | rect(midX, midY, w-midX, h-midY) |
75 | ); |
76 | } |
77 | |
78 | IIntegralImage liveliestQuadrant(int channel) { |
79 | ret highestBy(q -> q.liveliness(channel), quadrants()); |
80 | } |
81 | |
82 | Clip clip(Rect r) { ret new Clip(this, r); } |
83 | Clip clip(int x1, int y1, int w, int h) { ret clip(rect(x1, y1, w, h)); } |
84 | |
85 | Rect positionInImage(IIntegralImage mainImage) { |
86 | if (this == mainImage) ret rect(0, 0, w, h); |
87 | null; |
88 | } |
89 | } |
90 | |
91 | // virtual clip of an integral image |
92 | class Clip extends IIntegralImage { |
93 | IIntegralImage fullImage; |
94 | int x1, y1; |
95 | |
96 | *(IIntegralImage *fullImage, Rect r) { |
97 | x1 = r.x; y1 = r.y; w = r.w; h = r.h; |
98 | } |
99 | |
100 | *(IIntegralImage *fullImage, int *x1, int *y1, int *w, int *h) {} |
101 | |
102 | public double integralValue(int x, int y, int channel) { |
103 | ret fullImage.integralValue(x+x1, y+y1, channel); |
104 | } |
105 | |
106 | // don't clip a clip - be smarter than that! |
107 | Clip clip(Rect r) { |
108 | ret new Clip(fullImage, translateRect(r, x1, y1)); |
109 | } |
110 | |
111 | Rect rectInImage() { |
112 | ret rect(x1, y1, w, h); |
113 | } |
114 | |
115 | Rect positionInImage(IIntegralImage mainImage) { |
116 | try object Rect r = super.positionInImage(mainImage); |
117 | if (fullImage == mainImage) ret rect(x1, y1, w, h); |
118 | null; |
119 | } |
120 | |
121 | toString { ret rectInImage() + " in " + fullImage; } |
122 | } |
123 | |
124 | class IntegralImage extends IIntegralImage { |
125 | int[] data; |
126 | |
127 | *(BufferedImage img) { |
128 | w = img.getWidth(); h = img.getHeight(); |
129 | if (longMul(w, h) > 8000000) fail("Image too big: " + w + "*" + h); |
130 | int[] pixels = pixelsOfBufferedImage(img); |
131 | data = new int[w*h*3]; |
132 | int i = 0, j = 0; |
133 | int[] sum = new[3]; |
134 | for y to h: { |
135 | for c to 3: sum[c] = 0; |
136 | for x to w: { |
137 | int rgb = pixels[j++] & 0xFFFFFF; |
138 | for c to 3: { |
139 | data[i] = (sum[c] += rgb >> 16); |
140 | if (y > 0) |
141 | data[i] += data[i-w*3]; |
142 | i++; |
143 | rgb = (rgb << 8) & 0xFFFFFF; |
144 | } |
145 | } |
146 | } |
147 | } |
148 | |
149 | public double integralValue(int x, int y, Channel channel) { |
150 | if (channel == grayscale) |
151 | ret doubleAvg(countIterator(3, c -> integralValue(x, y, c))); |
152 | |
153 | ret x < 0 || y < 0 ? 0 |
154 | : data[(min(y, h-1)*w+min(x, w-1))*3+channel]; |
155 | } |
156 | } |
157 | |
158 | run { |
159 | mainImage = new IntegralImage(inputImage); |
160 | inputImage = null; // save space |
161 | |
162 | print(liveliness := mainImage.liveliness(grayscale)); |
163 | IIntegralImage clip = mainImage; |
164 | clip = applyUntilEqual_goOneBackOnNull(c -> c.liveliestQuadrant(grayscale), clip); |
165 | print(liveliestPoint := clip); |
166 | showImageWithSelection(mainImage.render(), growRect(10, clip.positionInImage(mainImage))); |
167 | } |
168 | } |
Began life as a copy of #1032199
download show line numbers debug dex old transpilations
Travelled to 3 computer(s): bhatertpkbcr, mqqgnosmbjvj, pyentgdyhuwx
No comments. add comment
Snippet ID: | #1032203 |
Snippet name: | Minimal Recognizer [backup] |
Eternal ID of this version: | #1032203/1 |
Text MD5: | c343d8e8b52eb8d5426a77a556abdbf4 |
Transpilation MD5: | 83cb647ad179ffbb182f80cdcc0f51f9 |
Author: | stefan |
Category: | |
Type: | JavaX fragment (include) |
Public (visible to everyone): | Yes |
Archived (hidden from active list): | No |
Created/modified: | 2021-08-20 05:09:28 |
Source code size: | 5425 bytes / 168 lines |
Pitched / IR pitched: | No / No |
Views / Downloads: | 140 / 188 |
Referenced in: | [show references] |