image.photography\fusion\image_fusion.mbt
77.2% · 78 / 101 points · 259 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 图像融合 fusion/image_fusion - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// 图像融合(v12.6.0) - 7
// 多焦点融合、曝光融合、Alpha 融合、拉普拉斯金字塔融合 - 8
- 9
///| - 10
/// 图像融合配置 - 11
#derive(Eq, @debug.Debug) - 12
pub(all) struct FusionConfig { - 13
method_type : String // "alpha" / "multifocus" / "exposure" / "laplacian" - 14
alpha : Double // Alpha 融合的权重(0-1) - 15
pyramid_levels : Int32 // 拉普拉斯金字塔层数 - 16
} - 17
- 18
///| - 19
pub extend FusionConfig with Eq::{not_equal, equal} - 20
- 21
///| - 22
pub extend FusionConfig with @debug.Debug::{to_repr} - 23
- 24
///| - 25
/// 创建默认融合配置 - 26
pub fn fusion_default_config() -> FusionConfig { - 27●
FusionConfig::{ method_type: "alpha", alpha: 0.5, pyramid_levels: 3, } - 28
} - 29
- 30
///| - 31
/// Alpha 融合 - 32
/// 线性融合 out = img1 * alpha + img2 * (1 - alpha) - 33
pub fn fuse_alpha( - 34
img1 : @types.Image, - 35
img2 : @types.Image, - 36
alpha~ : Double = 0.5, - 37
) -> @types.Image { - 38●
let a = alpha - 39
if img1.width != img2.width || img1.height != img2.height { - 40○
return img1 - 41
} - 42
let w = img1.width - 43
let h = img1.height - 44●
let channels = img1.channels.min(img2.channels) - 45●
let out_channels = img1.channels.max(img2.channels) - 46●
let out_data : Array[Byte] = Array::make(w * h * out_channels, b'\x00') - 47
for y in 0..<h { - 48●
for x in 0..<w { - 49●
let idx1 = (y * w + x) * img1.channels - 50
let idx2 = (y * w + x) * img2.channels - 51
let out_idx = (y * w + x) * out_channels - 52
for c in 0..<channels { - 53●
let v1 = img1.data[idx1 + c].to_double() - 54●
let v2 = img2.data[idx2 + c].to_double() - 55
let fused = v1 * a + v2 * (1.0 - a) - 56●
out_data[out_idx + c] = fused.min(255.0).max(0.0).to_int().to_byte() - 57
} - 58
// 复制额外通道 - 59
for c in channels..<out_channels { - 60○
if c < img1.channels { - 61○
out_data[out_idx + c] = img1.data[idx1 + c] - 62○
} else if c < img2.channels { - 63○
out_data[out_idx + c] = img2.data[idx2 + c] - 64
} - 65
} - 66
} - 67
} - 68
@types.Image::{ - 69
width: w, - 70
height: h, - 71
channels: out_channels, - 72●
data: Bytes::from_array(out_data), - 73
} - 74
} - 75
- 76
///| - 77
/// 计算图像的清晰度(拉普拉斯方差) - 78
fn compute_sharpness(img : @types.Image, x : Int32, y : Int32) -> Double { - 79●
let w = img.width - 80
let h = img.height - 81
let channels = img.channels - 82
if x <= 0 || x >= w - 1 || y <= 0 || y >= h - 1 { - 83●
return 0.0 - 84
} - 85
let _ = (y * w + x) * channels - 86●
let gray = (px : Int32, py : Int32) => { - 87
let i = (py * w + px) * channels - 88●
let r = img.data[i].to_double() - 89◐
let g = if channels >= 2 { img.data[i + 1].to_double() } else { r } - 90◐
let b = if channels >= 3 { img.data[i + 2].to_double() } else { r } - 91
0.299 * r + 0.587 * g + 0.114 * b - 92
} - 93
// 拉普拉斯算子 - 94●
let laplacian = 4.0 * gray(x, y) - - 95●
gray(x - 1, y) - - 96●
gray(x + 1, y) - - 97●
gray(x, y - 1) - - 98●
gray(x, y + 1) - 99
laplacian * laplacian - 100
} - 101
- 102
///| - 103
/// 多焦点融合 - 104
/// 基于清晰度的像素级融合,选择更清晰的像素 - 105
pub fn fuse_multifocus( - 106
img1 : @types.Image, - 107
img2 : @types.Image, - 108
) -> @types.Image { - 109●
if img1.width != img2.width || img1.height != img2.height { - 110○
return img1 - 111
} - 112
let w = img1.width - 113
let h = img1.height - 114●
let channels = img1.channels.min(img2.channels) - 115●
let out_channels = img1.channels.max(img2.channels) - 116●
let out_data : Array[Byte] = Array::make(w * h * out_channels, b'\x00') - 117
// 计算清晰度图 - 118●
let sharpness1 : Array[Double] = Array::make(w * h, 0.0) - 119●
let sharpness2 : Array[Double] = Array::make(w * h, 0.0) - 120
for y in 0..<h { - 121●
for x in 0..<w { - 122●
sharpness1[y * w + x] = compute_sharpness(img1, x, y) - 123●
sharpness2[y * w + x] = compute_sharpness(img2, x, y) - 124
} - 125
} - 126
// 像素级融合 - 127
for y in 0..<h { - 128●
for x in 0..<w { - 129●
let idx1 = (y * w + x) * img1.channels - 130
let idx2 = (y * w + x) * img2.channels - 131
let out_idx = (y * w + x) * out_channels - 132
let use_img1 = sharpness1[y * w + x] >= sharpness2[y * w + x] - 133
for c in 0..<channels { - 134●
if use_img1 { - 135●
out_data[out_idx + c] = img1.data[idx1 + c] - 136
} else { - 137●
out_data[out_idx + c] = img2.data[idx2 + c] - 138
} - 139
} - 140
// 复制额外通道 - 141
for c in channels..<out_channels { - 142○
if c < img1.channels { - 143○
out_data[out_idx + c] = img1.data[idx1 + c] - 144○
} else if c < img2.channels { - 145○
out_data[out_idx + c] = img2.data[idx2 + c] - 146
} - 147
} - 148
} - 149
} - 150
@types.Image::{ - 151
width: w, - 152
height: h, - 153
channels: out_channels, - 154●
data: Bytes::from_array(out_data), - 155
} - 156
} - 157
- 158
///| - 159
/// 曝光融合 - 160
/// 基于曝光质量的加权融合 - 161
pub fn fuse_exposure(images : Array[@types.Image]) -> @types.Image { - 162●
if images.length() == 0 { - 163○
return @types.Image::{ - 164
width: 1, - 165
height: 1, - 166
channels: 1, - 167○
data: Bytes::from_array([b'\x00']), - 168
} - 169
} - 170●
if images.length() == 1 { - 171○
return images[0] - 172
} - 173
let w = images[0].width - 174
let h = images[0].height - 175
let channels = images[0].channels - 176●
let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00') - 177
// 计算每张图像的权重(基于对比度、饱和度、曝光度) - 178
let weights : Array[Array[Double]] = [] - 179
for img in images { - 180●
let weight_map : Array[Double] = Array::make(w * h, 0.0) - 181
for y in 0..<h { - 182●
for x in 0..<w { - 183●
let idx = (y * w + x) * img.channels - 184●
let r = img.data[idx].to_double() - 185
let g = if img.channels >= 2 { - 186○
img.data[idx + 1].to_double() - 187
} else { - 188●
r - 189
} - 190
let b = if img.channels >= 3 { - 191○
img.data[idx + 2].to_double() - 192
} else { - 193●
r - 194
} - 195
let luminance = 0.299 * r + 0.587 * g + 0.114 * b - 196
// 曝光度权重(高斯,中心在 128) - 197●
let exposure_weight = @math.exp( - 198
-(luminance - 128.0) * (luminance - 128.0) / (2.0 * 64.0 * 64.0), - 199
) - 200
// 对比度权重(拉普拉斯) - 201●
let contrast_weight = compute_sharpness(img, x, y) + 1.0 - 202
weight_map[y * w + x] = exposure_weight * contrast_weight - 203
} - 204
} - 205●
weights.push(weight_map) - 206
} - 207
// 归一化权重并融合 - 208
for y in 0..<h { - 209●
for x in 0..<w { - 210●
let mut weight_sum = 0.0 - 211●
for i in 0..<weights.length() { - 212●
weight_sum += weights[i][y * w + x] - 213
} - 214
let out_idx = (y * w + x) * channels - 215
for c in 0..<channels { - 216●
if weight_sum > 0.0 { - 217●
let mut fused = 0.0 - 218●
for i in 0..<images.length() { - 219●
let idx = (y * w + x) * images[i].channels - 220
if c < images[i].channels { - 221●
fused += images[i].data[idx + c].to_double() * - 222
weights[i][y * w + x] / - 223
weight_sum - 224
} - 225
} - 226●
out_data[out_idx + c] = fused.min(255.0).max(0.0).to_int().to_byte() - 227
} else { - 228○
out_data[out_idx + c] = images[0].data[(y * w + x) * - 229
images[0].channels + - 230
c] - 231
} - 232
} - 233
} - 234
} - 235
@types.Image::{ - 236
width: w, - 237
height: h, - 238
channels, - 239●
data: Bytes::from_array(out_data), - 240
} - 241
} - 242
- 243
///| - 244
/// 通用图像融合接口 - 245
pub fn fuse_images( - 246
img1 : @types.Image, - 247
img2 : @types.Image, - 248
config? : FusionConfig, - 249
) -> @types.Image { - 250●
let cfg = match config { - 251●
Some(c) => c - 252●
None => fusion_default_config() - 253
} - 254●
match cfg.method_type { - 255●
"alpha" => fuse_alpha(img1, img2, alpha=cfg.alpha) - 256●
"multifocus" => fuse_multifocus(img1, img2) - 257○
_ => fuse_alpha(img1, img2, alpha=cfg.alpha) - 258
} - 259
}