Skip to content

Commit 323fed4

Browse files
committed
test(getNormalisedDifference): add test with mask
1 parent 4a0fd84 commit 323fed4

File tree

2 files changed

+50
-14
lines changed

2 files changed

+50
-14
lines changed

src/stack/align/__tests__/getNormalisedDifference.test.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,33 @@ test('options error', () => {
9191
),
9292
).toThrow(/You cannot specify both minNbPixels and minFractionPixels/);
9393
});
94+
95+
test.each([
96+
{
97+
translation: { column: -1, row: 0 },
98+
expected: 3,
99+
},
100+
{
101+
translation: { column: 1, row: 0 },
102+
expected: 2,
103+
},
104+
])('use source mask ($translation)', (data) => {
105+
const source = testUtils.createGreyImage([
106+
[1, 2, 3, 4],
107+
[0, 0, 0, 0],
108+
]);
109+
const destination = testUtils.createGreyImage([
110+
[0, 0, 0, 0],
111+
[1, 1, 1, 1],
112+
]);
113+
const mask = testUtils.createMask([
114+
[1, 1, 1, 1],
115+
[0, 0, 0, 0],
116+
]);
117+
expect(
118+
getNormalisedDifference(source, destination, data.translation, {
119+
minNbPixels: 1,
120+
sourceMask: mask,
121+
}),
122+
).toBe(data.expected);
123+
});

src/stack/align/getNormalisedDifference.ts

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,22 @@ export interface AlignDifferentSizeOptions {
1515
* Mask of the source image, which specifies the pixels to consider for the calculation.
1616
* @default Mask with the dimensions of source and all pixels set to 1.
1717
*/
18-
sourceMask?: Image;
18+
sourceMask?: Mask;
1919
}
2020

2121
/**
22-
* Compute the difference between two images that are not entirely overlapping and normalise it using the nb of pixels processed.
22+
* Compute the difference between two images that are not entirely overlapping and normalise it using the nb of pixels overlapping.
2323
* Only the pixels present in both images are taken into account.
2424
* @param source - Source image.
2525
* @param destination - Destination image.
26-
* @param translation - Translation to apply on the source image before computing the difference.
26+
* @param sourceTranslation - Translation to apply on the source image before computing the difference.
2727
* @param options - Options.
2828
* @returns The normalised difference.
2929
*/
3030
export function getNormalisedDifference(
3131
source: Image,
3232
destination: Image,
33-
translation: Point,
33+
sourceTranslation: Point,
3434
options: AlignDifferentSizeOptions = {},
3535
): number {
3636
const {
@@ -58,22 +58,28 @@ export function getNormalisedDifference(
5858
let destinationXOffset = 0;
5959
let destinationYOffset = 0;
6060

61-
if (translation.column < 0) {
62-
sourceXOffet = -translation.column;
61+
if (sourceTranslation.column < 0) {
62+
sourceXOffet = -sourceTranslation.column;
6363
} else {
64-
destinationXOffset = translation.column;
64+
destinationXOffset = sourceTranslation.column;
6565
}
6666

67-
if (translation.row < 0) {
68-
sourceYOffset = -translation.row;
67+
if (sourceTranslation.row < 0) {
68+
sourceYOffset = -sourceTranslation.row;
6969
} else {
70-
destinationYOffset = translation.row;
70+
destinationYOffset = sourceTranslation.row;
7171
}
7272

73-
const maxX = Math.min(destination.width, source.width + translation.column);
74-
const minX = Math.max(0, translation.column);
75-
const maxY = Math.min(destination.height, source.height + translation.row);
76-
const minY = Math.max(0, translation.row);
73+
const maxX = Math.min(
74+
destination.width,
75+
source.width + sourceTranslation.column,
76+
);
77+
const minX = Math.max(0, sourceTranslation.column);
78+
const maxY = Math.min(
79+
destination.height,
80+
source.height + sourceTranslation.row,
81+
);
82+
const minY = Math.max(0, sourceTranslation.row);
7783

7884
const width = maxX - minX;
7985
const height = maxY - minY;

0 commit comments

Comments
 (0)