image.photography\superres\super_resolution.mbt
83.7% · 103 / 123 points · 337 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 超分辨率 superres/super_resolution - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// 超分辨率(v11.2.0) - 7
// 双三次插值 + Lanczos 插值 + 边缘保持超分 - 8
- 9
///| - 10
/// 超分辨率配置 - 11
#derive(Eq, @debug.Debug) - 12
pub(all) struct SuperResolutionConfig { - 13
scale : Double // 放大倍数 - 14
method_type : String // "bicubic" / "lanczos" / "edge_preserving" - 15
lanczos_a : Int32 // Lanczos 参数(2或3) - 16
edge_threshold : Double // 边缘阈值 - 17
sharpen_amount : Double // 锐化强度 - 18
} - 19
- 20
///| - 21
pub extend SuperResolutionConfig with Eq::{not_equal, equal} - 22
- 23
///| - 24
pub extend SuperResolutionConfig with @debug.Debug::{to_repr} - 25
- 26
///| - 27
/// 创建默认超分辨率配置 - 28
pub fn sr_default_config() -> SuperResolutionConfig { - 29●
SuperResolutionConfig::{ - 30
scale: 2.0, - 31
method_type: "bicubic", - 32
lanczos_a: 3, - 33
edge_threshold: 30.0, - 34
sharpen_amount: 0.5, - 35
} - 36
} - 37
- 38
///| - 39
/// 双三次插值核 - 40
fn bicubic_kernel(x : Double) -> Double { - 41●
let a = -0.5 - 42●
let abs_x = x.abs() - 43
if abs_x <= 1.0 { - 44●
(a + 2.0) * abs_x * abs_x * abs_x - (a + 3.0) * abs_x * abs_x + 1.0 - 45●
} else if abs_x <= 2.0 { - 46●
a * abs_x * abs_x * abs_x - - 47
5.0 * a * abs_x * abs_x + - 48
8.0 * a * abs_x - - 49
4.0 * a - 50
} else { - 51●
0.0 - 52
} - 53
} - 54
- 55
///| - 56
/// Lanczos 插值核 - 57
fn lanczos_kernel(x : Double, a : Int32) -> Double { - 58●
let a_d = a.to_double() - 59●
let abs_x = x.abs() - 60
if abs_x < 0.0001 { - 61○
1.0 - 62●
} else if abs_x < a_d { - 63●
let pi_x = 3.141592653589793 * x - 64
let pi_x_a = pi_x / a_d - 65●
@math.sin(pi_x) * @math.sin(pi_x_a) / (pi_x * pi_x_a) - 66
} else { - 67●
0.0 - 68
} - 69
} - 70
- 71
///| - 72
/// 双三次插值超分辨率 - 73
pub fn super_resolution_bicubic( - 74
img : @types.Image, - 75
scale : Double, - 76
) -> @types.Image { - 77●
let w = img.width - 78
let h = img.height - 79
let channels = img.channels - 80
let data = img.data - 81●
let out_w = (w.to_double() * scale).to_int() - 82●
let out_h = (h.to_double() * scale).to_int() - 83●
let out_data : Array[Byte] = Array::make(out_w * out_h * channels, b'\x00') - 84
for y in 0..<out_h { - 85●
for x in 0..<out_w { - 86
// 反向映射到原图坐标 - 87●
let src_x = (x.to_double() + 0.5) / scale - 0.5 - 88●
let src_y = (y.to_double() + 0.5) / scale - 0.5 - 89●
let x0 = src_x.to_int() - 90●
let y0 = src_y.to_int() - 91
for c in 0..<channels { - 92●
let mut sum = 0.0 - 93
let mut weight_sum = 0.0 - 94
// 4x4 邻域 - 95
for dy in -1..<3 { - 96●
for dx in -1..<3 { - 97●
let px = x0 + dx - 98
let py = y0 + dy - 99
if px >= 0 && px < w && py >= 0 && py < h { - 100●
let wx = bicubic_kernel(src_x - px.to_double()) - 101●
let wy = bicubic_kernel(src_y - py.to_double()) - 102
let weight = wx * wy - 103●
sum += data[(py * w + px) * channels + c].to_double() * weight - 104
weight_sum += weight - 105
} - 106
} - 107
} - 108◐
let value = if weight_sum > 0.0 { sum / weight_sum } else { 0.0 } - 109
let clamped = if value < 0.0 { - 110●
0.0 - 111●
} else if value > 255.0 { - 112○
255.0 - 113
} else { - 114●
value - 115
} - 116●
out_data[(y * out_w + x) * channels + c] = clamped.to_int().to_byte() - 117
} - 118
} - 119
} - 120
@types.Image::{ - 121
width: out_w, - 122
height: out_h, - 123
channels, - 124●
data: Bytes::from_array(out_data), - 125
} - 126
} - 127
- 128
///| - 129
/// Lanczos 插值超分辨率 - 130
pub fn super_resolution_lanczos( - 131
img : @types.Image, - 132
scale : Double, - 133
a~ : Int32 = 3, - 134
) -> @types.Image { - 135●
let lanczos_a = a - 136
let w = img.width - 137
let h = img.height - 138
let channels = img.channels - 139
let data = img.data - 140●
let out_w = (w.to_double() * scale).to_int() - 141●
let out_h = (h.to_double() * scale).to_int() - 142●
let out_data : Array[Byte] = Array::make(out_w * out_h * channels, b'\x00') - 143
let radius = lanczos_a - 144
for y in 0..<out_h { - 145●
for x in 0..<out_w { - 146●
let src_x = (x.to_double() + 0.5) / scale - 0.5 - 147●
let src_y = (y.to_double() + 0.5) / scale - 0.5 - 148●
let x0 = src_x.to_int() - 149●
let y0 = src_y.to_int() - 150
for c in 0..<channels { - 151●
let mut sum = 0.0 - 152
let mut weight_sum = 0.0 - 153
for dy in -radius..<(radius + 1) { - 154●
for dx in -radius..<(radius + 1) { - 155●
let px = x0 + dx - 156
let py = y0 + dy - 157
if px >= 0 && px < w && py >= 0 && py < h { - 158●
let wx = lanczos_kernel(src_x - px.to_double(), lanczos_a) - 159●
let wy = lanczos_kernel(src_y - py.to_double(), lanczos_a) - 160
let weight = wx * wy - 161●
sum += data[(py * w + px) * channels + c].to_double() * weight - 162
weight_sum += weight - 163
} - 164
} - 165
} - 166◐
let value = if weight_sum > 0.0 { sum / weight_sum } else { 0.0 } - 167
let clamped = if value < 0.0 { - 168●
0.0 - 169●
} else if value > 255.0 { - 170○
255.0 - 171
} else { - 172●
value - 173
} - 174●
out_data[(y * out_w + x) * channels + c] = clamped.to_int().to_byte() - 175
} - 176
} - 177
} - 178
@types.Image::{ - 179
width: out_w, - 180
height: out_h, - 181
channels, - 182●
data: Bytes::from_array(out_data), - 183
} - 184
} - 185
- 186
///| - 187
/// 边缘保持超分辨率(双三次 + 边缘检测 + 锐化) - 188
pub fn super_resolution_edge_preserving( - 189
img : @types.Image, - 190
scale : Double, - 191
edge_threshold~ : Double = 30.0, - 192
sharpen_amount~ : Double = 0.5, - 193
) -> @types.Image { - 194●
let et = edge_threshold - 195
let sa = sharpen_amount - 196
// 第一步:双三次插值 - 197●
let upscaled = super_resolution_bicubic(img, scale) - 198
// 第二步:边缘检测(Sobel) - 199●
let edges = detect_edges_sobel(upscaled) - 200
// 第三步:在边缘区域进行锐化 - 201
let w = upscaled.width - 202
let h = upscaled.height - 203
let channels = upscaled.channels - 204
let data = upscaled.data - 205●
let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00') - 206
for y in 0..<h { - 207●
for x in 0..<w { - 208●
for c in 0..<channels { - 209●
let idx = (y * w + x) * channels + c - 210
let edge_strength = edges[y * w + x] - 211
if edge_strength > et && x > 0 && x < w - 1 && y > 0 && y < h - 1 { - 212
// 锐化:中心像素加权,减去邻域模糊 - 213○
let center = data[idx].to_double() - 214
let mut neighbor_sum = 0.0 - 215
let neighbors = [(-1, 0), (1, 0), (0, -1), (0, 1)] - 216
for _tup0 in neighbors { - 217○
let (dx, dy) = _tup0 - 218○
neighbor_sum += data[((y + dy) * w + (x + dx)) * channels + c].to_double() - 219
} - 220
let sharpened = center * (1.0 + sa) - neighbor_sum * sa / 4.0 - 221
let clamped = if sharpened < 0.0 { - 222○
0.0 - 223○
} else if sharpened > 255.0 { - 224○
255.0 - 225
} else { - 226○
sharpened - 227
} - 228○
out_data[idx] = clamped.to_int().to_byte() - 229
} else { - 230●
out_data[idx] = data[idx] - 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
/// Sobel 边缘检测 - 245
fn detect_edges_sobel(img : @types.Image) -> Array[Double] { - 246●
let w = img.width - 247
let h = img.height - 248
let data = img.data - 249
let channels = img.channels - 250●
let edges : Array[Double] = Array::make(w * h, 0.0) - 251
// 转换为灰度 - 252
let gray : Array[Double] = [] - 253
for i in 0..<(w * h) { - 254●
if channels >= 3 { - 255○
let r = data[i * channels].to_double() - 256○
let g = data[i * channels + 1].to_double() - 257○
let b = data[i * channels + 2].to_double() - 258○
gray.push(0.299 * r + 0.587 * g + 0.114 * b) - 259
} else { - 260●
gray.push(data[i * channels].to_double()) - 261
} - 262
} - 263
// Sobel 算子 - 264
let gx_kernel = [ - 265
(-1, -1, -1), - 266
(0, -1, 0), - 267
(1, -1, 1), - 268
(-1, 0, -2), - 269
(0, 0, 0), - 270
(1, 0, 2), - 271
(-1, 1, -1), - 272
(0, 1, 0), - 273
(1, 1, 1), - 274
] - 275
let gy_kernel = [ - 276
(-1, -1, -1), - 277
(0, -1, -2), - 278
(1, -1, -1), - 279
(-1, 0, 0), - 280
(0, 0, 0), - 281
(1, 0, 0), - 282
(-1, 1, 1), - 283
(0, 1, 2), - 284
(1, 1, 1), - 285
] - 286
for y in 1..<(h - 1) { - 287●
for x in 1..<(w - 1) { - 288●
let mut gx = 0.0 - 289
let mut gy = 0.0 - 290
for i in 0..<9 { - 291●
let (dx, dy, weight) = gx_kernel[i] - 292●
gx += gray[(y + dy) * w + (x + dx)] * weight.to_double() - 293
} - 294
for i in 0..<9 { - 295●
let (dx, dy, weight) = gy_kernel[i] - 296●
gy += gray[(y + dy) * w + (x + dx)] * weight.to_double() - 297
} - 298●
edges[y * w + x] = (gx * gx + gy * gy).sqrt() - 299
} - 300
} - 301
edges - 302
} - 303
- 304
///| - 305
/// 通用超分辨率接口 - 306
pub fn super_resolution( - 307
img : @types.Image, - 308
config? : SuperResolutionConfig, - 309
) -> @types.Image { - 310●
let cfg = match config { - 311●
Some(c) => c - 312●
None => sr_default_config() - 313
} - 314●
match cfg.method_type { - 315●
"lanczos" => super_resolution_lanczos(img, cfg.scale, a=cfg.lanczos_a) - 316
"edge_preserving" => - 317●
super_resolution_edge_preserving( - 318
img, - 319
cfg.scale, - 320
edge_threshold=cfg.edge_threshold, - 321
sharpen_amount=cfg.sharpen_amount, - 322
) - 323●
_ => super_resolution_bicubic(img, cfg.scale) - 324
} - 325
} - 326
- 327
///| - 328
/// 2倍超分辨率(便捷接口) - 329
pub fn upscale_2x(img : @types.Image) -> @types.Image { - 330●
super_resolution_bicubic(img, 2.0) - 331
} - 332
- 333
///| - 334
/// 4倍超分辨率(便捷接口) - 335
pub fn upscale_4x(img : @types.Image) -> @types.Image { - 336●
super_resolution_bicubic(img, 4.0) - 337
}