@srsergio/taptapp-ar 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (207) hide show
  1. package/README.md +62 -0
  2. package/dist/compiler/aframe.d.ts +1 -0
  3. package/dist/compiler/aframe.js +275 -0
  4. package/dist/compiler/compiler-base.d.ts +12 -0
  5. package/dist/compiler/compiler-base.js +165 -0
  6. package/dist/compiler/compiler.d.ts +9 -0
  7. package/dist/compiler/compiler.js +24 -0
  8. package/dist/compiler/compiler.worker.d.ts +1 -0
  9. package/dist/compiler/compiler.worker.js +28 -0
  10. package/dist/compiler/controller.d.ts +101 -0
  11. package/dist/compiler/controller.js +400 -0
  12. package/dist/compiler/controller.worker.d.ts +1 -0
  13. package/dist/compiler/controller.worker.js +61 -0
  14. package/dist/compiler/detector/crop-detector.d.ts +65 -0
  15. package/dist/compiler/detector/crop-detector.js +59 -0
  16. package/dist/compiler/detector/detector.d.ts +98 -0
  17. package/dist/compiler/detector/detector.js +1049 -0
  18. package/dist/compiler/detector/freak.d.ts +1 -0
  19. package/dist/compiler/detector/freak.js +89 -0
  20. package/dist/compiler/detector/kernels/cpu/binomialFilter.d.ts +6 -0
  21. package/dist/compiler/detector/kernels/cpu/binomialFilter.js +51 -0
  22. package/dist/compiler/detector/kernels/cpu/buildExtremas.d.ts +6 -0
  23. package/dist/compiler/detector/kernels/cpu/buildExtremas.js +89 -0
  24. package/dist/compiler/detector/kernels/cpu/computeExtremaAngles.d.ts +7 -0
  25. package/dist/compiler/detector/kernels/cpu/computeExtremaAngles.js +79 -0
  26. package/dist/compiler/detector/kernels/cpu/computeExtremaFreak.d.ts +6 -0
  27. package/dist/compiler/detector/kernels/cpu/computeExtremaFreak.js +68 -0
  28. package/dist/compiler/detector/kernels/cpu/computeFreakDescriptors.d.ts +6 -0
  29. package/dist/compiler/detector/kernels/cpu/computeFreakDescriptors.js +57 -0
  30. package/dist/compiler/detector/kernels/cpu/computeLocalization.d.ts +6 -0
  31. package/dist/compiler/detector/kernels/cpu/computeLocalization.js +54 -0
  32. package/dist/compiler/detector/kernels/cpu/computeOrientationHistograms.d.ts +6 -0
  33. package/dist/compiler/detector/kernels/cpu/computeOrientationHistograms.js +118 -0
  34. package/dist/compiler/detector/kernels/cpu/downsampleBilinear.d.ts +6 -0
  35. package/dist/compiler/detector/kernels/cpu/downsampleBilinear.js +29 -0
  36. package/dist/compiler/detector/kernels/cpu/extremaReduction.d.ts +6 -0
  37. package/dist/compiler/detector/kernels/cpu/extremaReduction.js +50 -0
  38. package/dist/compiler/detector/kernels/cpu/fakeShader.d.ts +20 -0
  39. package/dist/compiler/detector/kernels/cpu/fakeShader.js +80 -0
  40. package/dist/compiler/detector/kernels/cpu/index.d.ts +1 -0
  41. package/dist/compiler/detector/kernels/cpu/index.js +25 -0
  42. package/dist/compiler/detector/kernels/cpu/prune.d.ts +1 -0
  43. package/dist/compiler/detector/kernels/cpu/prune.js +103 -0
  44. package/dist/compiler/detector/kernels/cpu/smoothHistograms.d.ts +6 -0
  45. package/dist/compiler/detector/kernels/cpu/smoothHistograms.js +47 -0
  46. package/dist/compiler/detector/kernels/cpu/upsampleBilinear.d.ts +6 -0
  47. package/dist/compiler/detector/kernels/cpu/upsampleBilinear.js +43 -0
  48. package/dist/compiler/detector/kernels/index.d.ts +1 -0
  49. package/dist/compiler/detector/kernels/index.js +2 -0
  50. package/dist/compiler/detector/kernels/webgl/binomialFilter.d.ts +6 -0
  51. package/dist/compiler/detector/kernels/webgl/binomialFilter.js +67 -0
  52. package/dist/compiler/detector/kernels/webgl/buildExtremas.d.ts +6 -0
  53. package/dist/compiler/detector/kernels/webgl/buildExtremas.js +101 -0
  54. package/dist/compiler/detector/kernels/webgl/computeExtremaAngles.d.ts +6 -0
  55. package/dist/compiler/detector/kernels/webgl/computeExtremaAngles.js +78 -0
  56. package/dist/compiler/detector/kernels/webgl/computeExtremaFreak.d.ts +6 -0
  57. package/dist/compiler/detector/kernels/webgl/computeExtremaFreak.js +86 -0
  58. package/dist/compiler/detector/kernels/webgl/computeFreakDescriptors.d.ts +6 -0
  59. package/dist/compiler/detector/kernels/webgl/computeFreakDescriptors.js +52 -0
  60. package/dist/compiler/detector/kernels/webgl/computeLocalization.d.ts +6 -0
  61. package/dist/compiler/detector/kernels/webgl/computeLocalization.js +58 -0
  62. package/dist/compiler/detector/kernels/webgl/computeOrientationHistograms.d.ts +6 -0
  63. package/dist/compiler/detector/kernels/webgl/computeOrientationHistograms.js +116 -0
  64. package/dist/compiler/detector/kernels/webgl/downsampleBilinear.d.ts +6 -0
  65. package/dist/compiler/detector/kernels/webgl/downsampleBilinear.js +46 -0
  66. package/dist/compiler/detector/kernels/webgl/extremaReduction.d.ts +6 -0
  67. package/dist/compiler/detector/kernels/webgl/extremaReduction.js +48 -0
  68. package/dist/compiler/detector/kernels/webgl/index.d.ts +1 -0
  69. package/dist/compiler/detector/kernels/webgl/index.js +25 -0
  70. package/dist/compiler/detector/kernels/webgl/smoothHistograms.d.ts +6 -0
  71. package/dist/compiler/detector/kernels/webgl/smoothHistograms.js +49 -0
  72. package/dist/compiler/detector/kernels/webgl/upsampleBilinear.d.ts +6 -0
  73. package/dist/compiler/detector/kernels/webgl/upsampleBilinear.js +56 -0
  74. package/dist/compiler/estimation/esimate-experiment.d.ts +5 -0
  75. package/dist/compiler/estimation/esimate-experiment.js +267 -0
  76. package/dist/compiler/estimation/estimate.d.ts +5 -0
  77. package/dist/compiler/estimation/estimate.js +51 -0
  78. package/dist/compiler/estimation/estimator.d.ts +13 -0
  79. package/dist/compiler/estimation/estimator.js +30 -0
  80. package/dist/compiler/estimation/refine-estimate-experiment.d.ts +6 -0
  81. package/dist/compiler/estimation/refine-estimate-experiment.js +429 -0
  82. package/dist/compiler/estimation/refine-estimate.d.ts +6 -0
  83. package/dist/compiler/estimation/refine-estimate.js +299 -0
  84. package/dist/compiler/estimation/utils.d.ts +10 -0
  85. package/dist/compiler/estimation/utils.js +80 -0
  86. package/dist/compiler/image-list.d.ts +13 -0
  87. package/dist/compiler/image-list.js +52 -0
  88. package/dist/compiler/index.d.ts +3 -0
  89. package/dist/compiler/index.js +10 -0
  90. package/dist/compiler/input-loader.d.ts +23 -0
  91. package/dist/compiler/input-loader.js +88 -0
  92. package/dist/compiler/matching/hamming-distance.d.ts +1 -0
  93. package/dist/compiler/matching/hamming-distance.js +20 -0
  94. package/dist/compiler/matching/hierarchical-clustering.d.ts +7 -0
  95. package/dist/compiler/matching/hierarchical-clustering.js +109 -0
  96. package/dist/compiler/matching/hough.d.ts +1 -0
  97. package/dist/compiler/matching/hough.js +169 -0
  98. package/dist/compiler/matching/matcher.d.ts +28 -0
  99. package/dist/compiler/matching/matcher.js +48 -0
  100. package/dist/compiler/matching/matching.d.ts +41 -0
  101. package/dist/compiler/matching/matching.js +197 -0
  102. package/dist/compiler/matching/ransacHomography.d.ts +1 -0
  103. package/dist/compiler/matching/ransacHomography.js +136 -0
  104. package/dist/compiler/offline-compiler.d.ts +10 -0
  105. package/dist/compiler/offline-compiler.js +450 -0
  106. package/dist/compiler/tensorflow-setup.d.ts +7 -0
  107. package/dist/compiler/tensorflow-setup.js +73 -0
  108. package/dist/compiler/three.d.ts +66 -0
  109. package/dist/compiler/three.js +310 -0
  110. package/dist/compiler/tracker/extract-utils.d.ts +1 -0
  111. package/dist/compiler/tracker/extract-utils.js +29 -0
  112. package/dist/compiler/tracker/extract.d.ts +4 -0
  113. package/dist/compiler/tracker/extract.js +349 -0
  114. package/dist/compiler/tracker/tracker.d.ts +38 -0
  115. package/dist/compiler/tracker/tracker.js +327 -0
  116. package/dist/compiler/utils/cumsum.d.ts +5 -0
  117. package/dist/compiler/utils/cumsum.js +39 -0
  118. package/dist/compiler/utils/geometry.d.ts +8 -0
  119. package/dist/compiler/utils/geometry.js +101 -0
  120. package/dist/compiler/utils/homography.d.ts +1 -0
  121. package/dist/compiler/utils/homography.js +138 -0
  122. package/dist/compiler/utils/images.d.ts +24 -0
  123. package/dist/compiler/utils/images.js +99 -0
  124. package/dist/compiler/utils/randomizer.d.ts +5 -0
  125. package/dist/compiler/utils/randomizer.js +25 -0
  126. package/dist/index.d.ts +6 -0
  127. package/dist/index.js +7 -0
  128. package/dist/react/AREditor.d.ts +5 -0
  129. package/dist/react/AREditor.js +159 -0
  130. package/dist/react/ProgressDialog.d.ts +13 -0
  131. package/dist/react/ProgressDialog.js +57 -0
  132. package/dist/react/types.d.ts +22 -0
  133. package/dist/react/types.js +14 -0
  134. package/package.json +53 -0
  135. package/src/astro/ARScene.astro +59 -0
  136. package/src/astro/ARVideoTrigger.astro +73 -0
  137. package/src/astro/overlays/ErrorOverlay.astro +40 -0
  138. package/src/astro/overlays/LoadingOverlay.astro +28 -0
  139. package/src/astro/overlays/ScanningOverlay.astro +119 -0
  140. package/src/astro/scripts/ARScripts.astro +118 -0
  141. package/src/astro/styles/ARStyles.astro +147 -0
  142. package/src/compiler/aframe.js +343 -0
  143. package/src/compiler/compiler-base.js +195 -0
  144. package/src/compiler/compiler.js +25 -0
  145. package/src/compiler/compiler.worker.js +30 -0
  146. package/src/compiler/controller.js +473 -0
  147. package/src/compiler/controller.worker.js +77 -0
  148. package/src/compiler/detector/crop-detector.js +68 -0
  149. package/src/compiler/detector/detector.js +1130 -0
  150. package/src/compiler/detector/freak.js +91 -0
  151. package/src/compiler/detector/kernels/cpu/binomialFilter.js +59 -0
  152. package/src/compiler/detector/kernels/cpu/buildExtremas.js +108 -0
  153. package/src/compiler/detector/kernels/cpu/computeExtremaAngles.js +91 -0
  154. package/src/compiler/detector/kernels/cpu/computeExtremaFreak.js +92 -0
  155. package/src/compiler/detector/kernels/cpu/computeFreakDescriptors.js +68 -0
  156. package/src/compiler/detector/kernels/cpu/computeLocalization.js +71 -0
  157. package/src/compiler/detector/kernels/cpu/computeOrientationHistograms.js +141 -0
  158. package/src/compiler/detector/kernels/cpu/downsampleBilinear.js +33 -0
  159. package/src/compiler/detector/kernels/cpu/extremaReduction.js +53 -0
  160. package/src/compiler/detector/kernels/cpu/fakeShader.js +88 -0
  161. package/src/compiler/detector/kernels/cpu/index.js +26 -0
  162. package/src/compiler/detector/kernels/cpu/prune.js +114 -0
  163. package/src/compiler/detector/kernels/cpu/smoothHistograms.js +57 -0
  164. package/src/compiler/detector/kernels/cpu/upsampleBilinear.js +51 -0
  165. package/src/compiler/detector/kernels/index.js +2 -0
  166. package/src/compiler/detector/kernels/webgl/binomialFilter.js +72 -0
  167. package/src/compiler/detector/kernels/webgl/buildExtremas.js +109 -0
  168. package/src/compiler/detector/kernels/webgl/computeExtremaAngles.js +82 -0
  169. package/src/compiler/detector/kernels/webgl/computeExtremaFreak.js +105 -0
  170. package/src/compiler/detector/kernels/webgl/computeFreakDescriptors.js +56 -0
  171. package/src/compiler/detector/kernels/webgl/computeLocalization.js +70 -0
  172. package/src/compiler/detector/kernels/webgl/computeOrientationHistograms.js +129 -0
  173. package/src/compiler/detector/kernels/webgl/downsampleBilinear.js +50 -0
  174. package/src/compiler/detector/kernels/webgl/extremaReduction.js +50 -0
  175. package/src/compiler/detector/kernels/webgl/index.js +26 -0
  176. package/src/compiler/detector/kernels/webgl/smoothHistograms.js +53 -0
  177. package/src/compiler/detector/kernels/webgl/upsampleBilinear.js +62 -0
  178. package/src/compiler/estimation/esimate-experiment.js +316 -0
  179. package/src/compiler/estimation/estimate.js +67 -0
  180. package/src/compiler/estimation/estimator.js +34 -0
  181. package/src/compiler/estimation/refine-estimate-experiment.js +512 -0
  182. package/src/compiler/estimation/refine-estimate.js +365 -0
  183. package/src/compiler/estimation/utils.js +97 -0
  184. package/src/compiler/image-list.js +62 -0
  185. package/src/compiler/index.js +13 -0
  186. package/src/compiler/input-loader.js +107 -0
  187. package/src/compiler/matching/hamming-distance.js +23 -0
  188. package/src/compiler/matching/hierarchical-clustering.js +131 -0
  189. package/src/compiler/matching/hough.js +206 -0
  190. package/src/compiler/matching/matcher.js +59 -0
  191. package/src/compiler/matching/matching.js +237 -0
  192. package/src/compiler/matching/ransacHomography.js +192 -0
  193. package/src/compiler/offline-compiler.js +553 -0
  194. package/src/compiler/tensorflow-setup.js +88 -0
  195. package/src/compiler/three.js +368 -0
  196. package/src/compiler/tracker/extract-utils.js +34 -0
  197. package/src/compiler/tracker/extract.js +419 -0
  198. package/src/compiler/tracker/tracker.js +397 -0
  199. package/src/compiler/utils/cumsum.js +40 -0
  200. package/src/compiler/utils/geometry.js +114 -0
  201. package/src/compiler/utils/homography.js +150 -0
  202. package/src/compiler/utils/images.js +111 -0
  203. package/src/compiler/utils/randomizer.js +29 -0
  204. package/src/index.ts +8 -0
  205. package/src/react/AREditor.tsx +394 -0
  206. package/src/react/ProgressDialog.tsx +185 -0
  207. package/src/react/types.ts +35 -0
@@ -0,0 +1,349 @@
1
+ import { Cumsum } from "../utils/cumsum.js";
2
+ const SEARCH_SIZE1 = 10;
3
+ const SEARCH_SIZE2 = 2;
4
+ //const TEMPLATE_SIZE = 22 // DEFAULT
5
+ const TEMPLATE_SIZE = 6;
6
+ const TEMPLATE_SD_THRESH = 5.0;
7
+ const MAX_SIM_THRESH = 0.95;
8
+ const MAX_THRESH = 0.9;
9
+ //const MIN_THRESH = 0.55;
10
+ const MIN_THRESH = 0.2;
11
+ const SD_THRESH = 8.0;
12
+ const OCCUPANCY_SIZE = (24 * 2) / 3;
13
+ /*
14
+ * Input image is in grey format. the imageData array size is width * height. value range from 0-255
15
+ * pixel value at row r and c = imageData[r * width + c]
16
+ *
17
+ * @param {Uint8Array} options.imageData
18
+ * @param {int} options.width image width
19
+ * @param {int} options.height image height
20
+ */
21
+ const extract = (image) => {
22
+ const { data: imageData, width, height, scale } = image;
23
+ // Step 1 - filter out interesting points. Interesting points have strong pixel value changed across neighbours
24
+ const isPixelSelected = [width * height];
25
+ for (let i = 0; i < isPixelSelected.length; i++)
26
+ isPixelSelected[i] = false;
27
+ // Step 1.1 consider a pixel at position (x, y). compute:
28
+ // dx = ((data[x+1, y-1] - data[x-1, y-1]) + (data[x+1, y] - data[x-1, y]) + (data[x+1, y+1] - data[x-1, y-1])) / 256 / 3
29
+ // dy = ((data[x+1, y+1] - data[x+1, y-1]) + (data[x, y+1] - data[x, y-1]) + (data[x-1, y+1] - data[x-1, y-1])) / 256 / 3
30
+ // dValue = sqrt(dx^2 + dy^2) / 2;
31
+ const dValue = new Float32Array(imageData.length);
32
+ for (let i = 0; i < width; i++) {
33
+ dValue[i] = -1;
34
+ dValue[width * (height - 1) + i] = -1;
35
+ }
36
+ for (let j = 0; j < height; j++) {
37
+ dValue[j * width] = -1;
38
+ dValue[j * width + width - 1] = -1;
39
+ }
40
+ for (let i = 1; i < width - 1; i++) {
41
+ for (let j = 1; j < height - 1; j++) {
42
+ let pos = i + width * j;
43
+ let dx = 0.0;
44
+ let dy = 0.0;
45
+ for (let k = -1; k <= 1; k++) {
46
+ dx += imageData[pos + width * k + 1] - imageData[pos + width * k - 1];
47
+ dy += imageData[pos + width + k] - imageData[pos - width + k];
48
+ }
49
+ dx /= 3 * 256;
50
+ dy /= 3 * 256;
51
+ dValue[pos] = Math.sqrt((dx * dx + dy * dy) / 2);
52
+ }
53
+ }
54
+ // Step 1.2 - select all pixel which is dValue largest than all its neighbour as "potential" candidate
55
+ // the number of selected points is still too many, so we use the value to further filter (e.g. largest the dValue, the better)
56
+ const dValueHist = new Uint32Array(1000); // histogram of dvalue scaled to [0, 1000)
57
+ for (let i = 0; i < 1000; i++)
58
+ dValueHist[i] = 0;
59
+ const neighbourOffsets = [-1, 1, -width, width];
60
+ let allCount = 0;
61
+ for (let i = 1; i < width - 1; i++) {
62
+ for (let j = 1; j < height - 1; j++) {
63
+ let pos = i + width * j;
64
+ let isMax = true;
65
+ for (let d = 0; d < neighbourOffsets.length; d++) {
66
+ if (dValue[pos] <= dValue[pos + neighbourOffsets[d]]) {
67
+ isMax = false;
68
+ break;
69
+ }
70
+ }
71
+ if (isMax) {
72
+ let k = Math.floor(dValue[pos] * 1000);
73
+ if (k > 999)
74
+ k = 999; // k>999 should not happen if computaiton is correction
75
+ if (k < 0)
76
+ k = 0; // k<0 should not happen if computaiton is correction
77
+ dValueHist[k] += 1;
78
+ allCount += 1;
79
+ isPixelSelected[pos] = true;
80
+ }
81
+ }
82
+ }
83
+ // reduce number of points according to dValue.
84
+ // actually, the whole Step 1. might be better to just sort the dvalues and pick the top (0.02 * width * height) points
85
+ const maxPoints = 0.02 * width * height;
86
+ let k = 999;
87
+ let filteredCount = 0;
88
+ while (k >= 0) {
89
+ filteredCount += dValueHist[k];
90
+ if (filteredCount > maxPoints)
91
+ break;
92
+ k--;
93
+ }
94
+ //console.log("image size: ", width * height);
95
+ //console.log("extracted featues: ", allCount);
96
+ //console.log("filtered featues: ", filteredCount);
97
+ for (let i = 0; i < isPixelSelected.length; i++) {
98
+ if (isPixelSelected[i]) {
99
+ if (dValue[i] * 1000 < k)
100
+ isPixelSelected[i] = false;
101
+ }
102
+ }
103
+ //console.log("selected count: ", isPixelSelected.reduce((a, b) => {return a + (b?1:0);}, 0));
104
+ // Step 2
105
+ // prebuild cumulative sum matrix for fast computation
106
+ const imageDataSqr = [];
107
+ for (let i = 0; i < imageData.length; i++) {
108
+ imageDataSqr[i] = imageData[i] * imageData[i];
109
+ }
110
+ const imageDataCumsum = new Cumsum(imageData, width, height);
111
+ const imageDataSqrCumsum = new Cumsum(imageDataSqr, width, height);
112
+ // holds the max similariliy value computed within SEARCH area of each pixel
113
+ // idea: if there is high simliarity with another pixel in nearby area, then it's not a good feature point
114
+ // next step is to find pixel with low similarity
115
+ const featureMap = new Float32Array(imageData.length);
116
+ for (let i = 0; i < width; i++) {
117
+ for (let j = 0; j < height; j++) {
118
+ const pos = j * width + i;
119
+ if (!isPixelSelected[pos]) {
120
+ featureMap[pos] = 1.0;
121
+ continue;
122
+ }
123
+ const vlen = _templateVar({
124
+ image,
125
+ cx: i,
126
+ cy: j,
127
+ sdThresh: TEMPLATE_SD_THRESH,
128
+ imageDataCumsum,
129
+ imageDataSqrCumsum,
130
+ });
131
+ if (vlen === null) {
132
+ featureMap[pos] = 1.0;
133
+ continue;
134
+ }
135
+ let max = -1.0;
136
+ for (let jj = -SEARCH_SIZE1; jj <= SEARCH_SIZE1; jj++) {
137
+ for (let ii = -SEARCH_SIZE1; ii <= SEARCH_SIZE1; ii++) {
138
+ if (ii * ii + jj * jj <= SEARCH_SIZE2 * SEARCH_SIZE2)
139
+ continue;
140
+ const sim = _getSimilarity({
141
+ image,
142
+ cx: i + ii,
143
+ cy: j + jj,
144
+ vlen: vlen,
145
+ tx: i,
146
+ ty: j,
147
+ imageDataCumsum,
148
+ imageDataSqrCumsum,
149
+ });
150
+ if (sim === null)
151
+ continue;
152
+ if (sim > max) {
153
+ max = sim;
154
+ if (max > MAX_SIM_THRESH)
155
+ break;
156
+ }
157
+ }
158
+ if (max > MAX_SIM_THRESH)
159
+ break;
160
+ }
161
+ featureMap[pos] = max;
162
+ }
163
+ }
164
+ // Step 2.2 select feature
165
+ const coords = _selectFeature({
166
+ image,
167
+ featureMap,
168
+ templateSize: TEMPLATE_SIZE,
169
+ searchSize: SEARCH_SIZE2,
170
+ occSize: OCCUPANCY_SIZE,
171
+ maxSimThresh: MAX_THRESH,
172
+ minSimThresh: MIN_THRESH,
173
+ sdThresh: SD_THRESH,
174
+ imageDataCumsum,
175
+ imageDataSqrCumsum,
176
+ });
177
+ return coords;
178
+ };
179
+ const _selectFeature = (options) => {
180
+ let { image, featureMap, templateSize, searchSize, occSize, maxSimThresh, minSimThresh, sdThresh, imageDataCumsum, imageDataSqrCumsum, } = options;
181
+ const { data: imageData, width, height, scale } = image;
182
+ //console.log("params: ", templateSize, templateSize, occSize, maxSimThresh, minSimThresh, sdThresh);
183
+ //occSize *= 2;
184
+ occSize = Math.floor(Math.min(image.width, image.height) / 10);
185
+ const divSize = (templateSize * 2 + 1) * 3;
186
+ const xDiv = Math.floor(width / divSize);
187
+ const yDiv = Math.floor(height / divSize);
188
+ let maxFeatureNum = Math.floor(width / occSize) * Math.floor(height / occSize) + xDiv * yDiv;
189
+ //console.log("max feature num: ", maxFeatureNum);
190
+ const coords = [];
191
+ const image2 = new Float32Array(imageData.length);
192
+ for (let i = 0; i < image2.length; i++) {
193
+ image2[i] = featureMap[i];
194
+ }
195
+ let num = 0;
196
+ while (num < maxFeatureNum) {
197
+ let minSim = maxSimThresh;
198
+ let cx = -1;
199
+ let cy = -1;
200
+ for (let j = 0; j < height; j++) {
201
+ for (let i = 0; i < width; i++) {
202
+ if (image2[j * width + i] < minSim) {
203
+ minSim = image2[j * width + i];
204
+ cx = i;
205
+ cy = j;
206
+ }
207
+ }
208
+ }
209
+ if (cx === -1)
210
+ break;
211
+ const vlen = _templateVar({
212
+ image,
213
+ cx: cx,
214
+ cy: cy,
215
+ sdThresh: 0,
216
+ imageDataCumsum,
217
+ imageDataSqrCumsum,
218
+ });
219
+ if (vlen === null) {
220
+ image2[cy * width + cx] = 1.0;
221
+ continue;
222
+ }
223
+ if (vlen / (templateSize * 2 + 1) < sdThresh) {
224
+ image2[cy * width + cx] = 1.0;
225
+ continue;
226
+ }
227
+ let min = 1.0;
228
+ let max = -1.0;
229
+ for (let j = -searchSize; j <= searchSize; j++) {
230
+ for (let i = -searchSize; i <= searchSize; i++) {
231
+ if (i * i + j * j > searchSize * searchSize)
232
+ continue;
233
+ if (i === 0 && j === 0)
234
+ continue;
235
+ const sim = _getSimilarity({
236
+ image,
237
+ vlen,
238
+ cx: cx + i,
239
+ cy: cy + j,
240
+ tx: cx,
241
+ ty: cy,
242
+ imageDataCumsum,
243
+ imageDataSqrCumsum,
244
+ });
245
+ if (sim === null)
246
+ continue;
247
+ if (sim < min) {
248
+ min = sim;
249
+ if (min < minSimThresh && min < minSim)
250
+ break;
251
+ }
252
+ if (sim > max) {
253
+ max = sim;
254
+ if (max > 0.99)
255
+ break;
256
+ }
257
+ }
258
+ if ((min < minSimThresh && min < minSim) || max > 0.99)
259
+ break;
260
+ }
261
+ if ((min < minSimThresh && min < minSim) || max > 0.99) {
262
+ image2[cy * width + cx] = 1.0;
263
+ continue;
264
+ }
265
+ coords.push({ x: cx, y: cy });
266
+ //coords.push({
267
+ //mx: 1.0 * cx / scale,
268
+ //my: 1.0 * (height - cy) / scale,
269
+ //})
270
+ num += 1;
271
+ //console.log(num, '(', cx, ',', cy, ')', minSim, 'min = ', min, 'max = ', max, 'sd = ', vlen/(templateSize*2+1));
272
+ // no other feature points within occSize square
273
+ for (let j = -occSize; j <= occSize; j++) {
274
+ for (let i = -occSize; i <= occSize; i++) {
275
+ if (cy + j < 0 || cy + j >= height || cx + i < 0 || cx + i >= width)
276
+ continue;
277
+ image2[(cy + j) * width + (cx + i)] = 1.0;
278
+ }
279
+ }
280
+ }
281
+ return coords;
282
+ };
283
+ // compute variances of the pixels, centered at (cx, cy)
284
+ const _templateVar = ({ image, cx, cy, sdThresh, imageDataCumsum, imageDataSqrCumsum }) => {
285
+ if (cx - TEMPLATE_SIZE < 0 || cx + TEMPLATE_SIZE >= image.width)
286
+ return null;
287
+ if (cy - TEMPLATE_SIZE < 0 || cy + TEMPLATE_SIZE >= image.height)
288
+ return null;
289
+ const templateWidth = 2 * TEMPLATE_SIZE + 1;
290
+ const nPixels = templateWidth * templateWidth;
291
+ let average = imageDataCumsum.query(cx - TEMPLATE_SIZE, cy - TEMPLATE_SIZE, cx + TEMPLATE_SIZE, cy + TEMPLATE_SIZE);
292
+ average /= nPixels;
293
+ //v = sum((pixel_i - avg)^2) for all pixel i within the template
294
+ // = sum(pixel_i^2) - sum(2 * avg * pixel_i) + sum(avg^avg)
295
+ let vlen = imageDataSqrCumsum.query(cx - TEMPLATE_SIZE, cy - TEMPLATE_SIZE, cx + TEMPLATE_SIZE, cy + TEMPLATE_SIZE);
296
+ vlen -=
297
+ 2 *
298
+ average *
299
+ imageDataCumsum.query(cx - TEMPLATE_SIZE, cy - TEMPLATE_SIZE, cx + TEMPLATE_SIZE, cy + TEMPLATE_SIZE);
300
+ vlen += nPixels * average * average;
301
+ if (vlen / nPixels < sdThresh * sdThresh)
302
+ return null;
303
+ vlen = Math.sqrt(vlen);
304
+ return vlen;
305
+ };
306
+ const _getSimilarity = (options) => {
307
+ const { image, cx, cy, vlen, tx, ty, imageDataCumsum, imageDataSqrCumsum } = options;
308
+ const { data: imageData, width, height } = image;
309
+ const templateSize = TEMPLATE_SIZE;
310
+ if (cx - templateSize < 0 || cx + templateSize >= width)
311
+ return null;
312
+ if (cy - templateSize < 0 || cy + templateSize >= height)
313
+ return null;
314
+ const templateWidth = 2 * templateSize + 1;
315
+ let sx = imageDataCumsum.query(cx - templateSize, cy - templateSize, cx + templateSize, cy + templateSize);
316
+ let sxx = imageDataSqrCumsum.query(cx - templateSize, cy - templateSize, cx + templateSize, cy + templateSize);
317
+ let sxy = 0;
318
+ // !! This loop is the performance bottleneck. Use moving pointers to optimize
319
+ //
320
+ // for (let i = cx - templateSize, i2 = tx - templateSize; i <= cx + templateSize; i++, i2++) {
321
+ // for (let j = cy - templateSize, j2 = ty - templateSize; j <= cy + templateSize; j++, j2++) {
322
+ // sxy += imageData[j*width + i] * imageData[j2*width + i2];
323
+ // }
324
+ // }
325
+ //
326
+ let p1 = (cy - templateSize) * width + (cx - templateSize);
327
+ let p2 = (ty - templateSize) * width + (tx - templateSize);
328
+ let nextRowOffset = width - templateWidth;
329
+ for (let j = 0; j < templateWidth; j++) {
330
+ for (let i = 0; i < templateWidth; i++) {
331
+ sxy += imageData[p1] * imageData[p2];
332
+ p1 += 1;
333
+ p2 += 1;
334
+ }
335
+ p1 += nextRowOffset;
336
+ p2 += nextRowOffset;
337
+ }
338
+ let templateAverage = imageDataCumsum.query(tx - templateSize, ty - templateSize, tx + templateSize, ty + templateSize);
339
+ templateAverage /= templateWidth * templateWidth;
340
+ sxy -= templateAverage * sx;
341
+ let vlen2 = sxx - (sx * sx) / (templateWidth * templateWidth);
342
+ if (vlen2 == 0)
343
+ return null;
344
+ vlen2 = Math.sqrt(vlen2);
345
+ // covariance between template and current pixel
346
+ const sim = (1.0 * sxy) / (vlen * vlen2);
347
+ return sim;
348
+ };
349
+ export { extract };
@@ -0,0 +1,38 @@
1
+ export class Tracker {
2
+ constructor(markerDimensions: any, trackingDataList: any, projectionTransform: any, inputWidth: any, inputHeight: any, debugMode?: boolean);
3
+ markerDimensions: any;
4
+ trackingDataList: any;
5
+ projectionTransform: any;
6
+ debugMode: boolean;
7
+ trackingKeyframeList: any[];
8
+ featurePointsListT: tf.Tensor<tf.Rank>[];
9
+ imagePixelsListT: tf.Tensor<tf.Rank>[];
10
+ imagePropertiesListT: tf.Tensor<tf.Rank>[];
11
+ kernelCaches: {};
12
+ dummyRun(inputT: any): void;
13
+ track(inputImageT: any, lastModelViewTransform: any, targetIndex: any): {
14
+ worldCoords: {
15
+ x: number;
16
+ y: number;
17
+ z: number;
18
+ }[];
19
+ screenCoords: {
20
+ x: number;
21
+ y: number;
22
+ }[];
23
+ debugExtra: {};
24
+ };
25
+ _computeMatching(featurePointsT: any, imagePixelsT: any, imagePropertiesT: any, projectedImageT: any): {
26
+ matchingPointsT: tf.Tensor<tf.Rank>;
27
+ simT: tf.Tensor<tf.Rank>;
28
+ };
29
+ _computeProjection(modelViewProjectionTransformT: any, inputImageT: any, targetIndex: any): tf.Tensor<tf.Rank>;
30
+ _buildAdjustedModelViewTransform(modelViewProjectionTransform: any): tf.Tensor<tf.Rank>;
31
+ _prebuild(trackingFrame: any, maxCount: any): {
32
+ featurePoints: tf.Tensor<tf.Rank>;
33
+ imagePixels: tf.Tensor<tf.Rank>;
34
+ imageProperties: tf.Tensor<tf.Rank>;
35
+ };
36
+ _compileAndRun(program: any, inputs: any): tf.Tensor<tf.Rank>;
37
+ }
38
+ import * as tf from "@tensorflow/tfjs";