image.domain\registration\image_registration.mbt
90.4% · 85 / 94 points · 329 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 图像配准 registration/image_registration - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// 图像配准(v12.7.0) - 7
// 基于相位相关的平移估计 + 仿射变换 + 图像对齐 - 8
- 9
///| - 10
/// 配准结果 - 11
pub(all) struct RegistrationResult { - 12
dx : Double // x 方向平移 - 13
dy : Double // y 方向平移 - 14
rotation : Double // 旋转角度(弧度) - 15
scale : Double // 缩放因子 - 16
confidence : Double // 配准置信度 - 17
} - 18
- 19
///| - 20
/// 仿射变换矩阵 - 21
pub(all) struct AffineTransform { - 22
a : Double // 缩放+旋转 - 23
b : Double // 剪切+旋转 - 24
c : Double // x 平移 - 25
d : Double // 剪切+旋转 - 26
e : Double // 缩放+旋转 - 27
f : Double // y 平移 - 28
} - 29
- 30
///| - 31
/// 创建恒等变换 - 32
pub fn affine_identity() -> AffineTransform { - 33●
AffineTransform::{ a: 1.0, b: 0.0, c: 0.0, d: 0.0, e: 1.0, f: 0.0, } - 34
} - 35
- 36
///| - 37
/// 创建平移变换 - 38
pub fn affine_translation(dx : Double, dy : Double) -> AffineTransform { - 39●
AffineTransform::{ a: 1.0, b: 0.0, c: dx, d: 0.0, e: 1.0, f: dy, } - 40
} - 41
- 42
///| - 43
/// 创建旋转变换(围绕原点) - 44
pub fn affine_rotation(angle : Double) -> AffineTransform { - 45●
let cos_a = @math.cos(angle) - 46●
let sin_a = @math.sin(angle) - 47
AffineTransform::{ a: cos_a, b: -sin_a, c: 0.0, d: sin_a, e: cos_a, f: 0.0, } - 48
} - 49
- 50
///| - 51
/// 创建缩放变换 - 52
pub fn affine_scale(sx : Double, sy : Double) -> AffineTransform { - 53●
AffineTransform::{ a: sx, b: 0.0, c: 0.0, d: 0.0, e: sy, f: 0.0, } - 54
} - 55
- 56
///| - 57
/// 组合两个仿射变换(先应用 t1,再应用 t2) - 58
pub fn affine_compose( - 59
t1 : AffineTransform, - 60
t2 : AffineTransform, - 61
) -> AffineTransform { - 62●
AffineTransform::{ - 63
a: t2.a * t1.a + t2.b * t1.d, - 64
b: t2.a * t1.b + t2.b * t1.e, - 65
c: t2.a * t1.c + t2.b * t1.f + t2.c, - 66
d: t2.d * t1.a + t2.e * t1.d, - 67
e: t2.d * t1.b + t2.e * t1.e, - 68
f: t2.d * t1.c + t2.e * t1.f + t2.f, - 69
} - 70
} - 71
- 72
///| - 73
/// 应用仿射变换到点 - 74
pub fn affine_apply( - 75
t : AffineTransform, - 76
x : Double, - 77
y : Double, - 78
) -> (Double, Double) { - 79●
let nx = t.a * x + t.b * y + t.c - 80
let ny = t.d * x + t.e * y + t.f - 81
(nx, ny) - 82
} - 83
- 84
///| - 85
/// 计算图像的灰度图 - 86
fn to_gray(img : @types.Image) -> Array[Double] { - 87●
let w = img.width - 88
let h = img.height - 89
let channels = img.channels - 90●
let gray : Array[Double] = Array::make(w * h, 0.0) - 91
for i in 0..<(w * h) { - 92●
let idx = i * channels - 93●
let r = img.data[idx].to_double() - 94◐
let g = if channels >= 2 { img.data[idx + 1].to_double() } else { r } - 95◐
let b = if channels >= 3 { img.data[idx + 2].to_double() } else { r } - 96
gray[i] = 0.299 * r + 0.587 * g + 0.114 * b - 97
} - 98
gray - 99
} - 100
- 101
///| - 102
/// 基于相位相关的平移估计(简化版,使用空间域互相关) - 103
pub fn estimate_translation( - 104
img1 : @types.Image, - 105
img2 : @types.Image, - 106
max_offset~ : Int32 = 20, - 107
) -> RegistrationResult raise @types.LoadError { - 108
// 输入验证 - 109●
if img1.width <= 0 || img1.height <= 0 { - 110●
raise @types.LoadError::DecodeFailed( - 111
"estimate_translation: 图像尺寸必须大于0", - 112
) - 113
} - 114
if img1.channels < 1 || img1.channels > 4 { - 115●
raise @types.LoadError::DecodeFailed( - 116
"estimate_translation: 通道数必须在1-4之间", - 117
) - 118
} - 119
let expected_size = img1.width * img1.height * img1.channels - 120●
if img1.data.length() != expected_size { - 121●
raise @types.LoadError::DecodeFailed( - 122
"estimate_translation: 数据大小不匹配", - 123
) - 124
} - 125
if img2.width <= 0 || img2.height <= 0 { - 126●
raise @types.LoadError::DecodeFailed( - 127
"estimate_translation: 图像尺寸必须大于0", - 128
) - 129
} - 130
if img2.channels < 1 || img2.channels > 4 { - 131●
raise @types.LoadError::DecodeFailed( - 132
"estimate_translation: 通道数必须在1-4之间", - 133
) - 134
} - 135
let expected_size2 = img2.width * img2.height * img2.channels - 136●
if img2.data.length() != expected_size2 { - 137●
raise @types.LoadError::DecodeFailed( - 138
"estimate_translation: 数据大小不匹配", - 139
) - 140
} - 141
let max_off = max_offset - 142
if img1.width != img2.width || img1.height != img2.height { - 143○
return RegistrationResult::{ - 144
dx: 0.0, - 145
dy: 0.0, - 146
rotation: 0.0, - 147
scale: 1.0, - 148
confidence: 0.0, - 149
} - 150
} - 151
let w = img1.width - 152
let h = img1.height - 153●
let gray1 = to_gray(img1) - 154●
let gray2 = to_gray(img2) - 155
// 计算均值 - 156
let mut mean1 = 0.0 - 157
let mut mean2 = 0.0 - 158
for i in 0..<(w * h) { - 159●
mean1 += gray1[i] - 160
mean2 += gray2[i] - 161
} - 162●
mean1 = mean1 / (w * h).to_double() - 163●
mean2 = mean2 / (w * h).to_double() - 164
// 搜索最佳平移 - 165
let mut best_dx = 0 - 166
let mut best_dy = 0 - 167
let mut best_corr = -10000000000.0 - 168
for dy in -max_off..<(max_off + 1) { - 169●
for dx in -max_off..<(max_off + 1) { - 170●
let mut corr = 0.0 - 171
let mut var1 = 0.0 - 172
let mut var2 = 0.0 - 173
let mut count = 0 - 174
for y in 0..<h { - 175●
for x in 0..<w { - 176●
let x2 = x + dx - 177
let y2 = y + dy - 178
if x2 >= 0 && x2 < w && y2 >= 0 && y2 < h { - 179●
let v1 = gray1[y * w + x] - mean1 - 180
let v2 = gray2[y2 * w + x2] - mean2 - 181
corr += v1 * v2 - 182
var1 += v1 * v1 - 183
var2 += v2 * v2 - 184
count += 1 - 185
} - 186
} - 187
} - 188
if count > 0 && var1 > 0.0 && var2 > 0.0 { - 189●
let normalized_corr = corr / (var1.sqrt() * var2.sqrt()) - 190
if normalized_corr > best_corr { - 191●
best_corr = normalized_corr - 192
best_dx = dx - 193
best_dy = dy - 194
} - 195
} - 196
} - 197
} - 198
RegistrationResult::{ - 199●
dx: best_dx.to_double(), - 200●
dy: best_dy.to_double(), - 201
rotation: 0.0, - 202
scale: 1.0, - 203
confidence: if best_corr > 0.0 { - 204●
best_corr - 205
} else { - 206○
0.0 - 207
}, - 208
} - 209
} - 210
- 211
///| - 212
/// 使用仿射变换对齐图像(反向映射 + 双线性插值) - 213
pub fn warp_affine_transform( - 214
img : @types.Image, - 215
transform : AffineTransform, - 216
out_width~ : Int32 = 0, - 217
out_height~ : Int32 = 0, - 218
) -> @types.Image raise @types.LoadError { - 219
// 输入验证 - 220●
if img.width <= 0 || img.height <= 0 { - 221●
raise @types.LoadError::DecodeFailed( - 222
"warp_affine_transform: 图像尺寸必须大于0", - 223
) - 224
} - 225
if img.channels < 1 || img.channels > 4 { - 226●
raise @types.LoadError::DecodeFailed( - 227
"warp_affine_transform: 通道数必须在1-4之间", - 228
) - 229
} - 230
let expected_size = img.width * img.height * img.channels - 231●
if img.data.length() != expected_size { - 232●
raise @types.LoadError::DecodeFailed( - 233
"warp_affine_transform: 数据大小不匹配", - 234
) - 235
} - 236◐
let ow = if out_width == 0 { img.width } else { out_width } - 237◐
let oh = if out_height == 0 { img.height } else { out_height } - 238
let w = img.width - 239
let h = img.height - 240
let channels = img.channels - 241●
let out_data : Array[Byte] = Array::make(ow * oh * channels, b'\x00') - 242
// 计算逆变换 - 243
let det = transform.a * transform.e - transform.b * transform.d - 244●
if det.abs() < 0.000001 { - 245○
return img - 246
} - 247
let inv_a = transform.e / det - 248
let inv_b = -transform.b / det - 249
let inv_c = (transform.b * transform.f - transform.e * transform.c) / det - 250
let inv_d = -transform.d / det - 251
let inv_e = transform.a / det - 252
let inv_f = (transform.d * transform.c - transform.a * transform.f) / det - 253
for y in 0..<oh { - 254●
for x in 0..<ow { - 255
// 反向映射 - 256●
let src_x = inv_a * x.to_double() + inv_b * y.to_double() + inv_c - 257●
let src_y = inv_d * x.to_double() + inv_e * y.to_double() + inv_f - 258
// 双线性插值 - 259●
let x0 = src_x.floor().to_int() - 260●
let y0 = src_y.floor().to_int() - 261
let x1 = x0 + 1 - 262
let y1 = y0 + 1 - 263●
let fx = src_x - x0.to_double() - 264●
let fy = src_y - y0.to_double() - 265
let out_idx = (y * ow + x) * channels - 266
for c in 0..<channels { - 267●
if x0 >= 0 && x1 < w && y0 >= 0 && y1 < h { - 268●
let v00 = img.data[(y0 * w + x0) * channels + c].to_double() - 269●
let v01 = img.data[(y0 * w + x1) * channels + c].to_double() - 270●
let v10 = img.data[(y1 * w + x0) * channels + c].to_double() - 271●
let v11 = img.data[(y1 * w + x1) * channels + c].to_double() - 272
let top = v00 * (1.0 - fx) + v01 * fx - 273
let bottom = v10 * (1.0 - fx) + v11 * fx - 274
let value = top * (1.0 - fy) + bottom * fy - 275●
out_data[out_idx + c] = value.min(255.0).max(0.0).to_int().to_byte() - 276
} else { - 277●
out_data[out_idx + c] = b'\x00' - 278
} - 279
} - 280
} - 281
} - 282
@types.Image::{ - 283
width: ow, - 284
height: oh, - 285
channels, - 286●
data: Bytes::from_array(out_data), - 287
} - 288
} - 289
- 290
///| - 291
/// 图像对齐(将 img2 对齐到 img1) - 292
pub fn align_images( - 293
img1 : @types.Image, - 294
img2 : @types.Image, - 295
) -> (@types.Image, RegistrationResult) raise @types.LoadError { - 296
// 输入验证 - 297●
if img1.width <= 0 || img1.height <= 0 { - 298●
raise @types.LoadError::DecodeFailed( - 299
"align_images: 图像尺寸必须大于0", - 300
) - 301
} - 302
if img1.channels < 1 || img1.channels > 4 { - 303●
raise @types.LoadError::DecodeFailed( - 304
"align_images: 通道数必须在1-4之间", - 305
) - 306
} - 307
let expected_size = img1.width * img1.height * img1.channels - 308●
if img1.data.length() != expected_size { - 309●
raise @types.LoadError::DecodeFailed("align_images: 数据大小不匹配") - 310
} - 311
if img2.width <= 0 || img2.height <= 0 { - 312●
raise @types.LoadError::DecodeFailed( - 313
"align_images: 图像尺寸必须大于0", - 314
) - 315
} - 316
if img2.channels < 1 || img2.channels > 4 { - 317●
raise @types.LoadError::DecodeFailed( - 318
"align_images: 通道数必须在1-4之间", - 319
) - 320
} - 321
let expected_size2 = img2.width * img2.height * img2.channels - 322●
if img2.data.length() != expected_size2 { - 323●
raise @types.LoadError::DecodeFailed("align_images: 数据大小不匹配") - 324
} - 325●
let result = estimate_translation(img1, img2) - 326●
let transform = affine_translation(-result.dx, -result.dy) - 327●
let aligned = warp_affine_transform(img2, transform) - 328
(aligned, result) - 329
}