Browse Source

Simplify ImageTrackerUtils.bestFitAspectRatioNDC()

customisations
alemart 9 months ago
parent
commit
e91d2432fa

+ 3
- 6
src/trackers/image-tracker/image-tracker-utils.ts View File

28
 import { SpeedyPoint2 } from 'speedy-vision/types/core/speedy-point';
28
 import { SpeedyPoint2 } from 'speedy-vision/types/core/speedy-point';
29
 import { SpeedyVector2 } from 'speedy-vision/types/core/speedy-vector';
29
 import { SpeedyVector2 } from 'speedy-vision/types/core/speedy-vector';
30
 import { SpeedyKeypoint } from 'speedy-vision/types/core/speedy-keypoint';
30
 import { SpeedyKeypoint } from 'speedy-vision/types/core/speedy-keypoint';
31
-import { ImageTracker } from './image-tracker';
32
 import { ReferenceImageWithMedia } from './reference-image';
31
 import { ReferenceImageWithMedia } from './reference-image';
33
 import { Utils } from '../../utils/utils';
32
 import { Utils } from '../../utils/utils';
34
 import { IllegalOperationError, IllegalArgumentError, NumericalError } from '../../utils/errors';
33
 import { IllegalOperationError, IllegalArgumentError, NumericalError } from '../../utils/errors';
162
 
161
 
163
     /**
162
     /**
164
      * Find the best-fit aspect ratio for the rectification of the reference image in NDC
163
      * Find the best-fit aspect ratio for the rectification of the reference image in NDC
165
-     * @param imageTracker
164
+     * @param screenSize
166
      * @param referenceImage
165
      * @param referenceImage
167
      * @returns a best-fit aspect ratio
166
      * @returns a best-fit aspect ratio
168
      */
167
      */
169
-    static bestFitAspectRatioNDC(imageTracker: ImageTracker, referenceImage: ReferenceImageWithMedia): number
168
+    static bestFitAspectRatioNDC(screenSize: SpeedySize, referenceImage: ReferenceImageWithMedia): number
170
     {
169
     {
171
-        const screenSize = imageTracker.screenSize;
172
-        const screenAspectRatio = screenSize.width / screenSize.height;
173
-
174
         /*
170
         /*
175
         
171
         
176
         The best-fit aspectRatio (a) is constructed as follows:
172
         The best-fit aspectRatio (a) is constructed as follows:
192
 
188
 
193
         */
189
         */
194
 
190
 
191
+        const screenAspectRatio = screenSize.width / screenSize.height;
195
         return referenceImage.aspectRatio / screenAspectRatio;
192
         return referenceImage.aspectRatio / screenAspectRatio;
196
     }
193
     }
197
 
194
 

+ 1
- 1
src/trackers/image-tracker/states/pre-tracking-a.ts View File

128
 
128
 
129
         // rectify the image
129
         // rectify the image
130
         const scale = TRACK_RECTIFIED_SCALE;
130
         const scale = TRACK_RECTIFIED_SCALE;
131
-        const aspectRatio = ImageTrackerUtils.bestFitAspectRatioNDC(this._imageTracker, this._referenceImage!);
131
+        const aspectRatio = ImageTrackerUtils.bestFitAspectRatioNDC(screenSize, this._referenceImage!);
132
         const shrink = ImageTrackerUtils.bestFitScaleNDC(aspectRatio, scale);
132
         const shrink = ImageTrackerUtils.bestFitScaleNDC(aspectRatio, scale);
133
         const toScreen = ImageTrackerUtils.NDCToRaster(screenSize);
133
         const toScreen = ImageTrackerUtils.NDCToRaster(screenSize);
134
         const toNDC = ImageTrackerUtils.rasterToNDC(screenSize);
134
         const toNDC = ImageTrackerUtils.rasterToNDC(screenSize);

+ 5
- 5
src/trackers/image-tracker/states/pre-tracking-b.ts View File

39
 import { ImageTracker, ImageTrackerOutput, ImageTrackerStateName } from '../image-tracker';
39
 import { ImageTracker, ImageTrackerOutput, ImageTrackerStateName } from '../image-tracker';
40
 import { ImageTrackerUtils, ImageTrackerKeypointPair } from '../image-tracker-utils';
40
 import { ImageTrackerUtils, ImageTrackerKeypointPair } from '../image-tracker-utils';
41
 import { ImageTrackerState, ImageTrackerStateOutput } from './state';
41
 import { ImageTrackerState, ImageTrackerStateOutput } from './state';
42
-import { ReferenceImage } from '../reference-image';
42
+import { ReferenceImageWithMedia } from '../reference-image';
43
 import { Nullable, Utils } from '../../../utils/utils';
43
 import { Nullable, Utils } from '../../../utils/utils';
44
 import { TrackingError } from '../../../utils/errors';
44
 import { TrackingError } from '../../../utils/errors';
45
 import {
45
 import {
64
 export class ImageTrackerPreTrackingBState extends ImageTrackerState
64
 export class ImageTrackerPreTrackingBState extends ImageTrackerState
65
 {
65
 {
66
     /** reference image */
66
     /** reference image */
67
-    private _referenceImage: Nullable<ReferenceImage>;
67
+    private _referenceImage: Nullable<ReferenceImageWithMedia>;
68
 
68
 
69
     /** a snapshot of the video from the scanning state and corresponding to the initial homography */
69
     /** a snapshot of the video from the scanning state and corresponding to the initial homography */
70
     private _snapshot: Nullable<SpeedyPipelineNodeImagePortalSink>;
70
     private _snapshot: Nullable<SpeedyPipelineNodeImagePortalSink>;
101
     onEnterState(settings: Record<string,any>)
101
     onEnterState(settings: Record<string,any>)
102
     {
102
     {
103
         const homography = settings.homography as SpeedyMatrix;
103
         const homography = settings.homography as SpeedyMatrix;
104
-        const referenceImage = settings.referenceImage as ReferenceImage;
104
+        const referenceImage = settings.referenceImage as ReferenceImageWithMedia;
105
         const snapshot = settings.snapshot as SpeedyPipelineNodeImagePortalSink;
105
         const snapshot = settings.snapshot as SpeedyPipelineNodeImagePortalSink;
106
         const referenceKeypointPortalSink = settings.referenceKeypointPortalSink as SpeedyPipelineNodeKeypointPortalSink;
106
         const referenceKeypointPortalSink = settings.referenceKeypointPortalSink as SpeedyPipelineNodeKeypointPortalSink;
107
 
107
 
143
 
143
 
144
         // rectify the image
144
         // rectify the image
145
         const scale = TRACK_RECTIFIED_SCALE;
145
         const scale = TRACK_RECTIFIED_SCALE;
146
-        const aspectRatio = ImageTrackerUtils.bestFitAspectRatioNDC(this._imageTracker, this._referenceImage!);
146
+        const aspectRatio = ImageTrackerUtils.bestFitAspectRatioNDC(screenSize, this._referenceImage!);
147
         const shrink = ImageTrackerUtils.bestFitScaleNDC(aspectRatio, scale);
147
         const shrink = ImageTrackerUtils.bestFitScaleNDC(aspectRatio, scale);
148
         const undistort = this._homography.inverse();
148
         const undistort = this._homography.inverse();
149
         const toScreen = ImageTrackerUtils.NDCToRaster(screenSize);
149
         const toScreen = ImageTrackerUtils.NDCToRaster(screenSize);
233
         }).then(([ warp, score ]) => {
233
         }).then(([ warp, score ]) => {
234
 
234
 
235
             const scale = TRACK_RECTIFIED_SCALE;
235
             const scale = TRACK_RECTIFIED_SCALE;
236
-            const aspectRatio = ImageTrackerUtils.bestFitAspectRatioNDC(this._imageTracker, this._referenceImage!);
236
+            const aspectRatio = ImageTrackerUtils.bestFitAspectRatioNDC(this.screenSize, this._referenceImage!);
237
             const shrink = ImageTrackerUtils.bestFitScaleNDC(aspectRatio, scale);
237
             const shrink = ImageTrackerUtils.bestFitScaleNDC(aspectRatio, scale);
238
             const grow = ImageTrackerUtils.inverseBestFitScaleNDC(aspectRatio, scale);
238
             const grow = ImageTrackerUtils.inverseBestFitScaleNDC(aspectRatio, scale);
239
             const scaledWarp = grow.times(warp).times(shrink);
239
             const scaledWarp = grow.times(warp).times(shrink);

+ 3
- 3
src/trackers/image-tracker/states/tracking.ts View File

228
 
228
 
229
         // rectify the image
229
         // rectify the image
230
         const scale = TRACK_RECTIFIED_SCALE;
230
         const scale = TRACK_RECTIFIED_SCALE;
231
-        const aspectRatio = ImageTrackerUtils.bestFitAspectRatioNDC(this._imageTracker, this._referenceImage!);
231
+        const aspectRatio = ImageTrackerUtils.bestFitAspectRatioNDC(screenSize, this._referenceImage!);
232
         const shrink = ImageTrackerUtils.bestFitScaleNDC(aspectRatio, scale);
232
         const shrink = ImageTrackerUtils.bestFitScaleNDC(aspectRatio, scale);
233
         const undistort = this._warpHomography.inverse();
233
         const undistort = this._warpHomography.inverse();
234
         const toScreen = ImageTrackerUtils.NDCToRaster(screenSize);
234
         const toScreen = ImageTrackerUtils.NDCToRaster(screenSize);
461
         }).then(([ warp, score ]) => {
461
         }).then(([ warp, score ]) => {
462
 
462
 
463
             const scale = TRACK_RECTIFIED_SCALE;
463
             const scale = TRACK_RECTIFIED_SCALE;
464
-            const aspectRatio = ImageTrackerUtils.bestFitAspectRatioNDC(this._imageTracker, this._referenceImage!);
464
+            const aspectRatio = ImageTrackerUtils.bestFitAspectRatioNDC(this.screenSize, this._referenceImage!);
465
             const shrink = ImageTrackerUtils.bestFitScaleNDC(aspectRatio, scale);
465
             const shrink = ImageTrackerUtils.bestFitScaleNDC(aspectRatio, scale);
466
             const grow = ImageTrackerUtils.inverseBestFitScaleNDC(aspectRatio, scale);
466
             const grow = ImageTrackerUtils.inverseBestFitScaleNDC(aspectRatio, scale);
467
             const scaledWarp = grow.times(warp).times(shrink);
467
             const scaledWarp = grow.times(warp).times(shrink);
496
         }).then(([ warp, score ]) => {
496
         }).then(([ warp, score ]) => {
497
 
497
 
498
             const scale = TRACK_RECTIFIED_SCALE;
498
             const scale = TRACK_RECTIFIED_SCALE;
499
-            const aspectRatio = ImageTrackerUtils.bestFitAspectRatioNDC(this._imageTracker, this._referenceImage!);
499
+            const aspectRatio = ImageTrackerUtils.bestFitAspectRatioNDC(this.screenSize, this._referenceImage!);
500
             const shrink = ImageTrackerUtils.bestFitScaleNDC(aspectRatio, scale);
500
             const shrink = ImageTrackerUtils.bestFitScaleNDC(aspectRatio, scale);
501
             const grow = ImageTrackerUtils.inverseBestFitScaleNDC(aspectRatio, scale);
501
             const grow = ImageTrackerUtils.inverseBestFitScaleNDC(aspectRatio, scale);
502
             const scaledWarp = grow.times(warp).times(shrink);
502
             const scaledWarp = grow.times(warp).times(shrink);

Loading…
Cancel
Save