@@ -36,6 +36,11 @@ export interface AlignDifferentSizeOptions extends ComputeNbOperationsOptions {
36
36
* @default 'minMax'
37
37
*/
38
38
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 ;
39
44
}
40
45
41
46
/**
@@ -48,7 +53,7 @@ export interface AlignDifferentSizeOptions extends ComputeNbOperationsOptions {
48
53
export function alignDifferentSize (
49
54
source : Image ,
50
55
destination : Image ,
51
- options : AlignDifferentSizeOptions ,
56
+ options : AlignDifferentSizeOptions = { } ,
52
57
) : Point {
53
58
const {
54
59
xFactor = 0.5 ,
@@ -57,6 +62,7 @@ export function alignDifferentSize(
57
62
precisionFactor = 1.5 ,
58
63
thresholdAlgoritm = 'otsu' ,
59
64
level = 'minMax' ,
65
+ blurKernelSize,
60
66
} = options ;
61
67
62
68
const margins = computeXYMargins ( source , destination , { xFactor, yFactor } ) ;
@@ -72,11 +78,13 @@ export function alignDifferentSize(
72
78
const smallSource = prepareForAlign ( source , {
73
79
scalingFactor,
74
80
level,
81
+ blurKernelSize,
75
82
} ) ;
76
83
const smallMask = getAlignMask ( smallSource , thresholdAlgoritm ) ;
77
84
const smallDestination = prepareForAlign ( destination , {
78
85
scalingFactor,
79
86
level,
87
+ blurKernelSize,
80
88
} ) ;
81
89
const smallMargins = computeXYMargins ( smallSource , smallDestination ) ;
82
90
@@ -91,27 +99,32 @@ export function alignDifferentSize(
91
99
) ;
92
100
93
101
// 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 ) ;
95
108
const maxX = Math . min (
96
109
destination . width ,
97
- source . width + roughTranslation . column ,
110
+ source . width + scaledTranslation . column ,
98
111
) ;
99
- const minY = Math . min ( 0 , roughTranslation . row ) ;
112
+ const minY = Math . max ( 0 , scaledTranslation . row ) ;
100
113
const maxY = Math . min (
101
114
destination . height ,
102
- source . height + roughTranslation . row ,
115
+ source . height + scaledTranslation . row ,
103
116
) ;
104
117
105
118
const overlapWidth = maxX - minX ;
106
119
const overlapHeight = maxY - minY ;
107
120
108
121
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 ) ,
111
124
} ;
112
125
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 ) ,
115
128
} ;
116
129
117
130
// Precise alignment
@@ -126,19 +139,25 @@ export function alignDifferentSize(
126
139
height : overlapHeight ,
127
140
} ) ;
128
141
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 ) ;
130
149
131
150
const preciseTranslation = getMinDiffTranslation (
132
- sourceCrop ,
133
- destinationCrop ,
151
+ preciseSource ,
152
+ preciseDestination ,
134
153
{
135
154
leftRightMargin : preciseMargins ,
136
155
topBottomMargin : preciseMargins ,
137
156
} ,
138
157
) ;
139
158
140
159
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 ,
143
162
} ;
144
163
}
0 commit comments