image.process\edge\edge_detect_16f.mbt
91.2% · 206 / 226 points · 627 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 边缘检测 16-bit/float 变体 Edge detection 16-bit/float variants - 3
/// - 4
/// 16位/浮点 Sobel 边缘检测 edge_detect_sobel_16 / edge_detect_sobel_f - 5
/// 16位/浮点 Sobel 边缘检测 16-bit/float Sobel edge detection - 6
// 边缘检测 16-bit/float 变体:edge_detect_sobel - 7
// 支持 Image16 / ImageF 两种类型 - 8
- 9
///| - 10
fn read_u16_le_e(data : Bytes, offset : Int32) -> Int32 { - 11●
data[offset].to_int() + data[offset + 1].to_int() * 256 - 12
} - 13
- 14
///| - 15
fn write_u16_le_e(buf : Array[Byte], offset : Int32, val : Int32) -> Unit { - 16●
buf[offset] = (val & 0xFF).to_byte() - 17●
buf[offset + 1] = ((val >> 8) & 0xFF).to_byte() - 18
} - 19
- 20
///| - 21
fn read_f32_le_e(data : Bytes, offset : Int32) -> Float { - 22●
let b0 = data[offset].to_int() - 23●
let b1 = data[offset + 1].to_int() - 24●
let b2 = data[offset + 2].to_int() - 25●
let b3 = data[offset + 3].to_int() - 26
let bits = b0 | (b1 << 8) | (b2 << 16) | (b3 << 24) - 27●
Float::reinterpret_from_int(bits) - 28
} - 29
- 30
///| - 31
fn write_f32_le_e(buf : Array[Byte], offset : Int32, val : Float) -> Unit { - 32●
let bits = val.reinterpret_as_int() - 33●
buf[offset] = (bits & 0xFF).to_byte() - 34●
buf[offset + 1] = ((bits >> 8) & 0xFF).to_byte() - 35●
buf[offset + 2] = ((bits >> 16) & 0xFF).to_byte() - 36●
buf[offset + 3] = ((bits >> 24) & 0xFF).to_byte() - 37
} - 38
- 39
///| - 40
/// 16-bit Sobel 边缘检测。先转灰度,然后计算梯度幅值。 - 41
/// 输出为单通道灰度图(channels=1)的 Image16。 - 42
/// @see edge_detect_sobel 8-bit 版本,本函数为 Image16 变体 - 43
pub fn edge_detect_sobel_16( - 44
img : @types.Image16, - 45
) -> @types.Image16 raise @types.LoadError { - 46
// 输入验证 - 47●
if img.width <= 0 || img.height <= 0 { - 48●
raise @types.LoadError::DecodeFailed( - 49
"edge_detect_sobel_16: 图像尺寸必须大于0", - 50
) - 51
} - 52
if img.channels < 1 || img.channels > 4 { - 53●
raise @types.LoadError::DecodeFailed( - 54
"edge_detect_sobel_16: 通道数必须在1-4之间", - 55
) - 56
} - 57
let expected_size = img.width * img.height * img.channels * 2 - 58●
if img.data.length() != expected_size { - 59●
raise @types.LoadError::DecodeFailed( - 60
"edge_detect_sobel_16: 数据大小不匹配,期望 " + - 61●
expected_size.to_string() + - 62
",实际 " + - 63●
img.data.length().to_string(), - 64
) - 65
} - 66
- 67
let w = img.width - 68
let h = img.height - 69
let ch = img.channels - 70
let n = w * h - 71
let bpp = ch * 2 - 72
// 先转灰度 - 73●
let gray : Array[Int32] = Array::make(n, 0) - 74
for i in 0..<n { - 75●
if ch == 1 { - 76●
gray[i] = read_u16_le_e(img.data, i * bpp) - 77
} else { - 78●
let r = read_u16_le_e(img.data, i * bpp) - 79●
let g = read_u16_le_e(img.data, i * bpp + 2) - 80●
let b = read_u16_le_e(img.data, i * bpp + 4) - 81
gray[i] = (r * 299 + g * 587 + b * 114) / 1000 - 82
} - 83
} - 84
// Sobel 卷积 - 85●
let out : Array[Byte] = Array::make(n * 2, b'\x00') - 86
// 内部区域 - 87
for y in 1..<(h - 1) { - 88●
for x in 1..<(w - 1) { - 89●
let idx = y * w + x - 90
let idx_l = idx - 1 - 91
let idx_r = idx + 1 - 92
let idx_t = idx - w - 93
let idx_b = idx + w - 94
let gx = -gray[idx_t - 1] + - 95
gray[idx_t + 1] - - 96
2 * gray[idx_l] + - 97
2 * gray[idx_r] - - 98
gray[idx_b - 1] + - 99
gray[idx_b + 1] - 100
let gy = -gray[idx_t - 1] - - 101
2 * gray[idx_t] - - 102
gray[idx_t + 1] + - 103
gray[idx_b - 1] + - 104
2 * gray[idx_b] + - 105
gray[idx_b + 1] - 106●
let mag = Float::from_int(gx * gx + gy * gy).sqrt() - 107●
let v = (mag + 0.5F).to_int().clamp(min=0, max=65535) - 108●
write_u16_le_e(out, idx * 2, v) - 109
} - 110
} - 111
// 边界区域 - 112
for y in 0..<h { - 113●
for x in 0..<w { - 114●
if x >= 1 && x < w - 1 && y >= 1 && y < h - 1 { - 115●
continue - 116
} - 117●
let x_l = @types.clamp_int(x - 1, 0, w - 1) - 118●
let x_r = @types.clamp_int(x + 1, 0, w - 1) - 119●
let y_t = @types.clamp_int(y - 1, 0, h - 1) - 120●
let y_b = @types.clamp_int(y + 1, 0, h - 1) - 121
let gx = -gray[y_t * w + x_l] + - 122
gray[y_t * w + x_r] - - 123
2 * gray[y * w + x_l] + - 124
2 * gray[y * w + x_r] - - 125
gray[y_b * w + x_l] + - 126
gray[y_b * w + x_r] - 127
let gy = -gray[y_t * w + x_l] - - 128
2 * gray[y_t * w + x] - - 129
gray[y_t * w + x_r] + - 130
gray[y_b * w + x_l] + - 131
2 * gray[y_b * w + x] + - 132
gray[y_b * w + x_r] - 133●
let mag = Float::from_int(gx * gx + gy * gy).sqrt() - 134●
let v = (mag + 0.5F).to_int().clamp(min=0, max=65535) - 135●
write_u16_le_e(out, (y * w + x) * 2, v) - 136
} - 137
} - 138
@types.Image16::{ - 139
width: w, - 140
height: h, - 141
channels: 1, - 142●
data: Bytes::from_array(out), - 143
} - 144
} - 145
- 146
///| - 147
/// float Sobel 边缘检测。先转灰度,然后计算梯度幅值。 - 148
/// 输出为单通道灰度图(channels=1)的 ImageF。 - 149
/// @see edge_detect_sobel 8-bit 版本,本函数为 ImageF 变体 - 150
pub fn edge_detect_sobel_f( - 151
img : @types.ImageF, - 152
) -> @types.ImageF raise @types.LoadError { - 153
// 输入验证 - 154●
if img.width <= 0 || img.height <= 0 { - 155●
raise @types.LoadError::DecodeFailed( - 156
"edge_detect_sobel_f: 图像尺寸必须大于0", - 157
) - 158
} - 159
if img.channels < 1 || img.channels > 4 { - 160●
raise @types.LoadError::DecodeFailed( - 161
"edge_detect_sobel_f: 通道数必须在1-4之间", - 162
) - 163
} - 164
let expected_size = img.width * img.height * img.channels * 4 - 165●
if img.data.length() != expected_size { - 166●
raise @types.LoadError::DecodeFailed( - 167
"edge_detect_sobel_f: 数据大小不匹配,期望 " + - 168●
expected_size.to_string() + - 169
",实际 " + - 170●
img.data.length().to_string(), - 171
) - 172
} - 173
- 174
let w = img.width - 175
let h = img.height - 176
let ch = img.channels - 177
let n = w * h - 178
let bpp = ch * 4 - 179
// 先转灰度 - 180●
let gray : Array[Float] = Array::make(n, 0.0F) - 181
for i in 0..<n { - 182●
if ch == 1 { - 183●
gray[i] = read_f32_le_e(img.data, i * bpp) - 184
} else { - 185●
let r = read_f32_le_e(img.data, i * bpp) - 186●
let g = read_f32_le_e(img.data, i * bpp + 4) - 187●
let b = read_f32_le_e(img.data, i * bpp + 8) - 188
gray[i] = r * 0.299F + g * 0.587F + b * 0.114F - 189
} - 190
} - 191
// Sobel 卷积 - 192●
let out : Array[Byte] = Array::make(n * 4, b'\x00') - 193
// 内部区域 - 194
for y in 1..<(h - 1) { - 195●
for x in 1..<(w - 1) { - 196●
let idx = y * w + x - 197
let idx_l = idx - 1 - 198
let idx_r = idx + 1 - 199
let idx_t = idx - w - 200
let idx_b = idx + w - 201
let gx = -gray[idx_t - 1] + - 202
gray[idx_t + 1] - - 203
2.0F * gray[idx_l] + - 204
2.0F * gray[idx_r] - - 205
gray[idx_b - 1] + - 206
gray[idx_b + 1] - 207
let gy = -gray[idx_t - 1] - - 208
2.0F * gray[idx_t] - - 209
gray[idx_t + 1] + - 210
gray[idx_b - 1] + - 211
2.0F * gray[idx_b] + - 212
gray[idx_b + 1] - 213●
let mag = (gx * gx + gy * gy).sqrt() - 214●
write_f32_le_e(out, idx * 4, mag) - 215
} - 216
} - 217
// 边界区域 - 218
for y in 0..<h { - 219●
for x in 0..<w { - 220●
if x >= 1 && x < w - 1 && y >= 1 && y < h - 1 { - 221●
continue - 222
} - 223●
let x_l = @types.clamp_int(x - 1, 0, w - 1) - 224●
let x_r = @types.clamp_int(x + 1, 0, w - 1) - 225●
let y_t = @types.clamp_int(y - 1, 0, h - 1) - 226●
let y_b = @types.clamp_int(y + 1, 0, h - 1) - 227
let gx = -gray[y_t * w + x_l] + - 228
gray[y_t * w + x_r] - - 229
2.0F * gray[y * w + x_l] + - 230
2.0F * gray[y * w + x_r] - - 231
gray[y_b * w + x_l] + - 232
gray[y_b * w + x_r] - 233
let gy = -gray[y_t * w + x_l] - - 234
2.0F * gray[y_t * w + x] - - 235
gray[y_t * w + x_r] + - 236
gray[y_b * w + x_l] + - 237
2.0F * gray[y_b * w + x] + - 238
gray[y_b * w + x_r] - 239●
let mag = (gx * gx + gy * gy).sqrt() - 240●
write_f32_le_e(out, (y * w + x) * 4, mag) - 241
} - 242
} - 243
@types.ImageF::{ - 244
width: w, - 245
height: h, - 246
channels: 1, - 247●
data: Bytes::from_array(out), - 248
} - 249
} - 250
- 251
///| - 252
/// 16-bit Laplacian 边缘检测。使用 3x3 Laplacian 核: - 253
/// 拉普拉斯核 [0, 1, 0; 1, -4, 1; 0, 1, 0] - 254
/// 输出单通道灰度图(channels=1)的 Image16,值为梯度绝对值。 - 255
/// @see edge_detect_laplacian 8-bit 版本,本函数为 Image16 变体 - 256
pub fn edge_detect_laplacian_16( - 257
img : @types.Image16, - 258
) -> @types.Image16 raise @types.LoadError { - 259
// 输入验证 - 260●
if img.width <= 0 || img.height <= 0 { - 261●
raise @types.LoadError::DecodeFailed( - 262
"edge_detect_laplacian_16: 图像尺寸必须大于0", - 263
) - 264
} - 265
if img.channels < 1 || img.channels > 4 { - 266●
raise @types.LoadError::DecodeFailed( - 267
"edge_detect_laplacian_16: 通道数必须在1-4之间", - 268
) - 269
} - 270
let expected_size = img.width * img.height * img.channels * 2 - 271●
if img.data.length() != expected_size { - 272○
raise @types.LoadError::DecodeFailed( - 273
"edge_detect_laplacian_16: 数据大小不匹配,期望 " + - 274○
expected_size.to_string() + - 275
",实际 " + - 276○
img.data.length().to_string(), - 277
) - 278
} - 279
- 280
let w = img.width - 281
let h = img.height - 282
let ch = img.channels - 283
let n = w * h - 284
let bpp = ch * 2 - 285
// 先转灰度 - 286●
let gray : Array[Int32] = Array::make(n, 0) - 287
for i in 0..<n { - 288●
if ch == 1 { - 289○
gray[i] = read_u16_le_e(img.data, i * bpp) - 290
} else { - 291●
let r = read_u16_le_e(img.data, i * bpp) - 292●
let g = read_u16_le_e(img.data, i * bpp + 2) - 293●
let b = read_u16_le_e(img.data, i * bpp + 4) - 294
gray[i] = (r * 77 + g * 150 + b * 29) / 256 - 295
} - 296
} - 297●
let out : Array[Byte] = Array::make(n * 2, b'\x00') - 298
// 内部区域 - 299
for y in 1..<(h - 1) { - 300●
for x in 1..<(w - 1) { - 301●
let idx = y * w + x - 302
let center = gray[idx] - 303
let u = gray[idx - w] - 304
let d = gray[idx + w] - 305
let l = gray[idx - 1] - 306
let r = gray[idx + 1] - 307
let lap = u + d + l + r - 4 * center - 308●
let val = lap.abs() - 309●
let clamped = if val > 65535 { 65535 } else { val } - 310●
write_u16_le_e(out, idx * 2, clamped) - 311
} - 312
} - 313
// 边界区域 - 314
for y in 0..<h { - 315●
for x in 0..<w { - 316●
if x >= 1 && x < w - 1 && y >= 1 && y < h - 1 { - 317●
continue - 318
} - 319●
let cx = @types.clamp_int(x, 0, w - 1) - 320●
let cy = @types.clamp_int(y, 0, h - 1) - 321●
let up = @types.clamp_int(y - 1, 0, h - 1) - 322●
let down = @types.clamp_int(y + 1, 0, h - 1) - 323●
let left = @types.clamp_int(x - 1, 0, w - 1) - 324●
let right = @types.clamp_int(x + 1, 0, w - 1) - 325
let center = gray[cy * w + cx] - 326
let u = gray[up * w + cx] - 327
let d = gray[down * w + cx] - 328
let l = gray[cy * w + left] - 329
let r = gray[cy * w + right] - 330
let lap = u + d + l + r - 4 * center - 331●
let val = lap.abs() - 332●
let clamped = if val > 65535 { 65535 } else { val } - 333●
write_u16_le_e(out, (y * w + x) * 2, clamped) - 334
} - 335
} - 336
@types.Image16::{ - 337
width: w, - 338
height: h, - 339
channels: 1, - 340●
data: Bytes::from_array(out), - 341
} - 342
} - 343
- 344
///| - 345
/// float Laplacian 边缘检测。使用 3x3 Laplacian 核: - 346
/// 拉普拉斯核 [0, 1, 0; 1, -4, 1; 0, 1, 0] - 347
/// 输出单通道灰度图(channels=1)的 ImageF,值为梯度绝对值。 - 348
/// @see edge_detect_laplacian 8-bit 版本,本函数为 ImageF 变体 - 349
pub fn edge_detect_laplacian_f( - 350
img : @types.ImageF, - 351
) -> @types.ImageF raise @types.LoadError { - 352
// 输入验证 - 353●
if img.width <= 0 || img.height <= 0 { - 354●
raise @types.LoadError::DecodeFailed( - 355
"edge_detect_laplacian_f: 图像尺寸必须大于0", - 356
) - 357
} - 358
if img.channels < 1 || img.channels > 4 { - 359●
raise @types.LoadError::DecodeFailed( - 360
"edge_detect_laplacian_f: 通道数必须在1-4之间", - 361
) - 362
} - 363
let expected_size = img.width * img.height * img.channels * 4 - 364●
if img.data.length() != expected_size { - 365○
raise @types.LoadError::DecodeFailed( - 366
"edge_detect_laplacian_f: 数据大小不匹配,期望 " + - 367○
expected_size.to_string() + - 368
",实际 " + - 369○
img.data.length().to_string(), - 370
) - 371
} - 372
- 373
let w = img.width - 374
let h = img.height - 375
let ch = img.channels - 376
let n = w * h - 377
let bpp = ch * 4 - 378
// 先转灰度 - 379●
let gray : Array[Float] = Array::make(n, 0.0F) - 380
for i in 0..<n { - 381●
if ch == 1 { - 382○
gray[i] = read_f32_le_e(img.data, i * bpp) - 383
} else { - 384●
let r = read_f32_le_e(img.data, i * bpp) - 385●
let g = read_f32_le_e(img.data, i * bpp + 4) - 386●
let b = read_f32_le_e(img.data, i * bpp + 8) - 387
gray[i] = r * 0.299F + g * 0.587F + b * 0.114F - 388
} - 389
} - 390●
let out : Array[Byte] = Array::make(n * 4, b'\x00') - 391
// 内部区域 - 392
for y in 1..<(h - 1) { - 393●
for x in 1..<(w - 1) { - 394●
let idx = y * w + x - 395
let center = gray[idx] - 396
let u = gray[idx - w] - 397
let d = gray[idx + w] - 398
let l = gray[idx - 1] - 399
let r = gray[idx + 1] - 400
let lap = u + d + l + r - 4.0F * center - 401●
write_f32_le_e(out, idx * 4, lap.abs()) - 402
} - 403
} - 404
// 边界区域 - 405
for y in 0..<h { - 406●
for x in 0..<w { - 407●
if x >= 1 && x < w - 1 && y >= 1 && y < h - 1 { - 408●
continue - 409
} - 410●
let cx = @types.clamp_int(x, 0, w - 1) - 411●
let cy = @types.clamp_int(y, 0, h - 1) - 412●
let up = @types.clamp_int(y - 1, 0, h - 1) - 413●
let down = @types.clamp_int(y + 1, 0, h - 1) - 414●
let left = @types.clamp_int(x - 1, 0, w - 1) - 415●
let right = @types.clamp_int(x + 1, 0, w - 1) - 416
let center = gray[cy * w + cx] - 417
let u = gray[up * w + cx] - 418
let d = gray[down * w + cx] - 419
let l = gray[cy * w + left] - 420
let r = gray[cy * w + right] - 421
let lap = u + d + l + r - 4.0F * center - 422●
write_f32_le_e(out, (y * w + x) * 4, lap.abs()) - 423
} - 424
} - 425
@types.ImageF::{ - 426
width: w, - 427
height: h, - 428
channels: 1, - 429●
data: Bytes::from_array(out), - 430
} - 431
} - 432
- 433
///| - 434
/// 16-bit Prewitt 边缘检测。使用两个 3x3 Prewitt 核计算梯度幅值。 - 435
/// Prewitt 水平梯度 Gx = [-1, 0, 1; -1, 0, 1; -1, 0, 1] - 436
/// Prewitt 垂直梯度 Gy = [-1, -1, -1; 0, 0, 0; 1, 1, 1] - 437
/// 输出单通道灰度图(channels=1)的 Image16。 - 438
/// @see edge_detect_prewitt 8-bit 版本,本函数为 Image16 变体 - 439
pub fn edge_detect_prewitt_16( - 440
img : @types.Image16, - 441
) -> @types.Image16 raise @types.LoadError { - 442
// 输入验证 - 443●
if img.width <= 0 || img.height <= 0 { - 444●
raise @types.LoadError::DecodeFailed( - 445
"edge_detect_prewitt_16: 图像尺寸必须大于0", - 446
) - 447
} - 448
if img.channels < 1 || img.channels > 4 { - 449○
raise @types.LoadError::DecodeFailed( - 450
"edge_detect_prewitt_16: 通道数必须在1-4之间", - 451
) - 452
} - 453
let expected_size = img.width * img.height * img.channels * 2 - 454●
if img.data.length() != expected_size { - 455●
raise @types.LoadError::DecodeFailed( - 456
"edge_detect_prewitt_16: 数据大小不匹配,期望 " + - 457●
expected_size.to_string() + - 458
",实际 " + - 459●
img.data.length().to_string(), - 460
) - 461
} - 462
- 463
let w = img.width - 464
let h = img.height - 465
let ch = img.channels - 466
let n = w * h - 467
let bpp = ch * 2 - 468
// 先转灰度 - 469●
let gray : Array[Int32] = Array::make(n, 0) - 470
for i in 0..<n { - 471●
if ch == 1 { - 472○
gray[i] = read_u16_le_e(img.data, i * bpp) - 473
} else { - 474●
let r = read_u16_le_e(img.data, i * bpp) - 475●
let g = read_u16_le_e(img.data, i * bpp + 2) - 476●
let b = read_u16_le_e(img.data, i * bpp + 4) - 477
gray[i] = (r * 77 + g * 150 + b * 29) / 256 - 478
} - 479
} - 480●
let out : Array[Byte] = Array::make(n * 2, b'\x00') - 481
// 内部区域 - 482
for y in 1..<(h - 1) { - 483●
for x in 1..<(w - 1) { - 484●
let idx = y * w + x - 485
let ul = gray[idx - w - 1] - 486
let ur = gray[idx - w + 1] - 487
let ll = gray[idx + w - 1] - 488
let lr = gray[idx + w + 1] - 489
let l = gray[idx - 1] - 490
let r = gray[idx + 1] - 491
let u = gray[idx - w] - 492
let d = gray[idx + w] - 493
let gx = ur + r + lr - ul - l - ll - 494
let gy = ll + d + lr - ul - u - ur - 495●
let mag = Float::from_int(gx * gx + gy * gy).sqrt().to_int() - 496◐
let clamped = if mag > 65535 { 65535 } else { mag } - 497●
write_u16_le_e(out, idx * 2, clamped) - 498
} - 499
} - 500
// 边界区域 - 501
for y in 0..<h { - 502●
for x in 0..<w { - 503●
if x >= 1 && x < w - 1 && y >= 1 && y < h - 1 { - 504●
continue - 505
} - 506●
let up = @types.clamp_int(y - 1, 0, h - 1) - 507●
let down = @types.clamp_int(y + 1, 0, h - 1) - 508●
let left = @types.clamp_int(x - 1, 0, w - 1) - 509●
let right = @types.clamp_int(x + 1, 0, w - 1) - 510
let ul = gray[up * w + left] - 511
let ur = gray[up * w + right] - 512
let ll = gray[down * w + left] - 513
let lr = gray[down * w + right] - 514
let l = gray[y * w + left] - 515
let r = gray[y * w + right] - 516
let u = gray[up * w + x] - 517
let d = gray[down * w + x] - 518
let gx = ur + r + lr - ul - l - ll - 519
let gy = ll + d + lr - ul - u - ur - 520●
let mag = Float::from_int(gx * gx + gy * gy).sqrt().to_int() - 521◐
let clamped = if mag > 65535 { 65535 } else { mag } - 522●
write_u16_le_e(out, (y * w + x) * 2, clamped) - 523
} - 524
} - 525
@types.Image16::{ - 526
width: w, - 527
height: h, - 528
channels: 1, - 529●
data: Bytes::from_array(out), - 530
} - 531
} - 532
- 533
///| - 534
/// float Prewitt 边缘检测。使用两个 3x3 Prewitt 核计算梯度幅值。 - 535
/// 输出单通道灰度图(channels=1)的 ImageF。 - 536
/// @see edge_detect_prewitt 8-bit 版本,本函数为 ImageF 变体 - 537
pub fn edge_detect_prewitt_f( - 538
img : @types.ImageF, - 539
) -> @types.ImageF raise @types.LoadError { - 540
// 输入验证 - 541●
if img.width <= 0 || img.height <= 0 { - 542●
raise @types.LoadError::DecodeFailed( - 543
"edge_detect_prewitt_f: 图像尺寸必须大于0", - 544
) - 545
} - 546
if img.channels < 1 || img.channels > 4 { - 547○
raise @types.LoadError::DecodeFailed( - 548
"edge_detect_prewitt_f: 通道数必须在1-4之间", - 549
) - 550
} - 551
let expected_size = img.width * img.height * img.channels * 4 - 552●
if img.data.length() != expected_size { - 553●
raise @types.LoadError::DecodeFailed( - 554
"edge_detect_prewitt_f: 数据大小不匹配,期望 " + - 555●
expected_size.to_string() + - 556
",实际 " + - 557●
img.data.length().to_string(), - 558
) - 559
} - 560
- 561
let w = img.width - 562
let h = img.height - 563
let ch = img.channels - 564
let n = w * h - 565
let bpp = ch * 4 - 566
// 先转灰度 - 567●
let gray : Array[Float] = Array::make(n, 0.0F) - 568
for i in 0..<n { - 569●
if ch == 1 { - 570○
gray[i] = read_f32_le_e(img.data, i * bpp) - 571
} else { - 572●
let r = read_f32_le_e(img.data, i * bpp) - 573●
let g = read_f32_le_e(img.data, i * bpp + 4) - 574●
let b = read_f32_le_e(img.data, i * bpp + 8) - 575
gray[i] = r * 0.299F + g * 0.587F + b * 0.114F - 576
} - 577
} - 578●
let out : Array[Byte] = Array::make(n * 4, b'\x00') - 579
// 内部区域 - 580
for y in 1..<(h - 1) { - 581●
for x in 1..<(w - 1) { - 582●
let idx = y * w + x - 583
let ul = gray[idx - w - 1] - 584
let ur = gray[idx - w + 1] - 585
let ll = gray[idx + w - 1] - 586
let lr = gray[idx + w + 1] - 587
let l = gray[idx - 1] - 588
let r = gray[idx + 1] - 589
let u = gray[idx - w] - 590
let d = gray[idx + w] - 591
let gx = ur + r + lr - ul - l - ll - 592
let gy = ll + d + lr - ul - u - ur - 593●
let mag = (gx * gx + gy * gy).sqrt() - 594●
write_f32_le_e(out, idx * 4, mag) - 595
} - 596
} - 597
// 边界区域 - 598
for y in 0..<h { - 599●
for x in 0..<w { - 600●
if x >= 1 && x < w - 1 && y >= 1 && y < h - 1 { - 601●
continue - 602
} - 603●
let up = @types.clamp_int(y - 1, 0, h - 1) - 604●
let down = @types.clamp_int(y + 1, 0, h - 1) - 605●
let left = @types.clamp_int(x - 1, 0, w - 1) - 606●
let right = @types.clamp_int(x + 1, 0, w - 1) - 607
let ul = gray[up * w + left] - 608
let ur = gray[up * w + right] - 609
let ll = gray[down * w + left] - 610
let lr = gray[down * w + right] - 611
let l = gray[y * w + left] - 612
let r = gray[y * w + right] - 613
let u = gray[up * w + x] - 614
let d = gray[down * w + x] - 615
let gx = ur + r + lr - ul - l - ll - 616
let gy = ll + d + lr - ul - u - ur - 617●
let mag = (gx * gx + gy * gy).sqrt() - 618●
write_f32_le_e(out, (y * w + x) * 4, mag) - 619
} - 620
} - 621
@types.ImageF::{ - 622
width: w, - 623
height: h, - 624
channels: 1, - 625●
data: Bytes::from_array(out), - 626
} - 627
}