-
Notifications
You must be signed in to change notification settings - Fork 5
/
Montage.ts
163 lines (146 loc) · 4.53 KB
/
Montage.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { Image } from '../../Image';
import { Point } from '../../geometry';
import { FastKeypoint } from '../keypoints/getFastKeypoints';
import { Match } from '../matching/bruteForceMatch';
import { drawKeypoints, DrawKeypointsOptions } from './drawKeypoints';
import { drawMatches, DrawMatchesOptions } from './drawMatches';
import { scaleKeypoints } from './scaleKeypoints';
export const MontageDisposition = {
HORIZONTAL: 'horizontal',
VERTICAL: 'vertical',
} as const;
// eslint-disable-next-line @typescript-eslint/no-redeclare
export type MontageDisposition =
(typeof MontageDisposition)[keyof typeof MontageDisposition];
export interface MontageOptions {
/**
* Factor by which to scale the images.
* @default `1`
*/
scale?: number;
/**
* How should the images be aligned: vertically or horizontally.
* @default `'horizontal'`
*/
disposition?: MontageDisposition;
}
export class Montage {
/**
* Scaled width of the first image.
*/
public readonly sourceWidth: number;
/**
* Scaled height of the first image.
*/
public readonly sourceHeight: number;
/**
* Scaled width of the second image.
*/
public readonly destinationWidth: number;
/**
* Scaled height of the second image.
*/
public readonly destinationHeight: number;
/**
* Origin of the destination / second image relative to top-left corner of the Montage.
*/
public readonly destinationOrigin: Point;
/**
* Width of the Montage.
*/
public readonly width: number;
/**
* Height of the Montage.
*/
public readonly height: number;
/**
* Factor by which to scale the images are scaled in the montage.
*/
public readonly scale: number;
public readonly disposition: MontageDisposition;
/**
* Image of the Montage.
*/
public image: Image;
/**
* Create a Montage of two images. The two images are placed side by side for comparison.
* @param source - First image.
* @param destination - Second image.
* @param options - Montage options.
*/
public constructor(
source: Image,
destination: Image,
options: MontageOptions = {},
) {
const { scale = 1, disposition = 'horizontal' } = options;
if (!Number.isInteger(scale)) {
throw new TypeError('scale must be an integer');
}
this.scale = scale;
this.disposition = disposition;
this.sourceWidth = scale * source.width;
this.destinationWidth = scale * destination.width;
this.sourceHeight = scale * source.height;
this.destinationHeight = scale * destination.height;
if (disposition === 'horizontal') {
this.destinationOrigin = { row: 0, column: this.sourceWidth };
this.width = this.sourceWidth + this.destinationWidth;
this.height = Math.max(this.sourceHeight, this.destinationHeight);
} else if (disposition === 'vertical') {
this.destinationOrigin = { row: this.sourceHeight, column: 0 };
this.width = Math.max(this.sourceWidth, this.destinationWidth);
this.height = this.sourceHeight + this.destinationHeight;
} else {
throw new RangeError(`invalid disposition type: ${disposition}`);
}
if (source.colorModel !== 'RGB') {
source = source.convertColor('RGB');
}
if (destination.colorModel !== 'RGB') {
destination = destination.convertColor('RGB');
}
const image = new Image(this.width, this.height);
source
.resize({ xFactor: scale, yFactor: scale })
.copyTo(image, { out: image });
destination.resize({ xFactor: scale, yFactor: scale }).copyTo(image, {
out: image,
origin: this.destinationOrigin,
});
this.image = image;
}
/**
* Draw keypoints on the Montage.
* @param keypoints - Keypoints to draw.
* @param options - Draw keypoints options.
*/
public drawKeypoints(
keypoints: FastKeypoint[],
options: DrawKeypointsOptions = {},
): void {
const scaledKeypoints = scaleKeypoints(keypoints, this.scale);
this.image = drawKeypoints(this.image, scaledKeypoints, options);
}
/**
* Draw the matches between source and destination keypoints.
* @param matches - Matches to draw.
* @param sourceKeypoints - Source keypoints.
* @param destinationKeypoints - Destination keypoints.
* @param options - Draw matches options.
*/
public drawMatches(
matches: Match[],
sourceKeypoints: FastKeypoint[],
destinationKeypoints: FastKeypoint[],
options: DrawMatchesOptions = {},
): void {
this.image = drawMatches(
this,
matches,
sourceKeypoints,
destinationKeypoints,
options,
);
}
}