Skip to content

Commit fa6ca72

Browse files
committed
test(alignDifferentSize): works with id crops
1 parent 9aac03e commit fa6ca72

File tree

5 files changed

+50
-16
lines changed

5 files changed

+50
-16
lines changed

src/operations/paintMaskOnImage.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export interface PaintMaskOnImageOptions {
2929
}
3030

3131
/**
32-
* Paint a mask onto an image and the given position and with the given color.
32+
* Paint a mask onto an image at the given position and with the given color.
3333
* @param image - Image on which to paint the mask.
3434
* @param mask - Mask to paint on the image.
3535
* @param options - Paint mask options.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { overlapImages } from '../../../featureMatching';
2+
import { alignDifferentSize } from '../alignDifferentSize';
3+
4+
test('id crops', () => {
5+
const destination = testUtils.load('align/cropped.png').grey();
6+
const source = testUtils.load('align/croppedRef.png').grey();
7+
8+
const result = alignDifferentSize(source, destination, {
9+
maxNbOperations: 1e8,
10+
});
11+
12+
const overlap = overlapImages(source, destination, { origin: result });
13+
14+
expect(overlap).toMatchImageSnapshot();
15+
});

src/stack/align/alignDifferentSize.ts

Lines changed: 33 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ export interface AlignDifferentSizeOptions extends ComputeNbOperationsOptions {
3636
* @default 'minMax'
3737
*/
3838
level?: LevelingAlgorithm;
39+
/**
40+
* Kernel size for the blur applied to the images before the rough alignment phase.
41+
* @default 3
42+
*/
43+
blurKernelSize?: number;
3944
}
4045

4146
/**
@@ -48,7 +53,7 @@ export interface AlignDifferentSizeOptions extends ComputeNbOperationsOptions {
4853
export function alignDifferentSize(
4954
source: Image,
5055
destination: Image,
51-
options: AlignDifferentSizeOptions,
56+
options: AlignDifferentSizeOptions = {},
5257
): Point {
5358
const {
5459
xFactor = 0.5,
@@ -57,6 +62,7 @@ export function alignDifferentSize(
5762
precisionFactor = 1.5,
5863
thresholdAlgoritm = 'otsu',
5964
level = 'minMax',
65+
blurKernelSize,
6066
} = options;
6167

6268
const margins = computeXYMargins(source, destination, { xFactor, yFactor });
@@ -72,11 +78,13 @@ export function alignDifferentSize(
7278
const smallSource = prepareForAlign(source, {
7379
scalingFactor,
7480
level,
81+
blurKernelSize,
7582
});
7683
const smallMask = getAlignMask(smallSource, thresholdAlgoritm);
7784
const smallDestination = prepareForAlign(destination, {
7885
scalingFactor,
7986
level,
87+
blurKernelSize,
8088
});
8189
const smallMargins = computeXYMargins(smallSource, smallDestination);
8290

@@ -91,27 +99,32 @@ export function alignDifferentSize(
9199
);
92100

93101
// Find overlapping surface and source and destination origins
94-
const minX = Math.min(0, roughTranslation.column);
102+
const scaledTranslation = {
103+
column: Math.round(roughTranslation.column * scalingFactor),
104+
row: Math.round(roughTranslation.row * scalingFactor),
105+
};
106+
107+
const minX = Math.max(0, scaledTranslation.column);
95108
const maxX = Math.min(
96109
destination.width,
97-
source.width + roughTranslation.column,
110+
source.width + scaledTranslation.column,
98111
);
99-
const minY = Math.min(0, roughTranslation.row);
112+
const minY = Math.max(0, scaledTranslation.row);
100113
const maxY = Math.min(
101114
destination.height,
102-
source.height + roughTranslation.row,
115+
source.height + scaledTranslation.row,
103116
);
104117

105118
const overlapWidth = maxX - minX;
106119
const overlapHeight = maxY - minY;
107120

108121
const sourceOrigin = {
109-
column: Math.max(0, -roughTranslation.column),
110-
row: Math.max(0, -roughTranslation.row),
122+
column: Math.max(0, -scaledTranslation.column),
123+
row: Math.max(0, -scaledTranslation.row),
111124
};
112125
const destinationOrigin = {
113-
column: Math.max(0, roughTranslation.column),
114-
row: Math.max(0, roughTranslation.row),
126+
column: Math.max(0, scaledTranslation.column),
127+
row: Math.max(0, scaledTranslation.row),
115128
};
116129

117130
// Precise alignment
@@ -126,19 +139,25 @@ export function alignDifferentSize(
126139
height: overlapHeight,
127140
});
128141

129-
const preciseMargins = precisionFactor * scalingFactor;
142+
const preciseSource = prepareForAlign(sourceCrop, { level, blurKernelSize });
143+
const preciseDestination = prepareForAlign(destinationCrop, {
144+
level,
145+
blurKernelSize,
146+
});
147+
148+
const preciseMargins = Math.round(precisionFactor * scalingFactor);
130149

131150
const preciseTranslation = getMinDiffTranslation(
132-
sourceCrop,
133-
destinationCrop,
151+
preciseSource,
152+
preciseDestination,
134153
{
135154
leftRightMargin: preciseMargins,
136155
topBottomMargin: preciseMargins,
137156
},
138157
);
139158

140159
return {
141-
column: roughTranslation.column + preciseTranslation.column,
142-
row: roughTranslation.row + preciseTranslation.row,
160+
column: scaledTranslation.column + preciseTranslation.column,
161+
row: scaledTranslation.row + preciseTranslation.row,
143162
};
144163
}

src/stack/align/computeNbOperations.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ export function computeXYMargins(
5353
if (source.height < destination.height) {
5454
yMargin = Math.round(yFactor * source.height);
5555
} else {
56-
yMargin = source.height - destination.height * (1 - yFactor);
56+
yMargin = Math.round(source.height - destination.height * (1 - yFactor));
5757
}
5858
return { xMargin, yMargin };
5959
}

0 commit comments

Comments
 (0)