image.process\edge\edge_detect.mbt
98.1% · 103 / 105 points · 297 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 边缘检测 edge detect - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// 边缘检测扩展(纯 MoonBit):edge_detect_laplacian / edge_detect_prewitt / edge_detect_sobel - 7
// @types.clamp_byte / @types.clamp_int 来自 walkzzz/image.core/types - 8
- 9
///| - 10
/// Sobel 边缘检测。先转灰度,然后计算梯度幅值。 - 11
/// 输出为单通道灰度图(channels=1)。 - 12
pub fn edge_detect_sobel( - 13
img : @types.Image, - 14
) -> @types.Image raise @types.LoadError { - 15
// 输入验证 - 16●
if img.width <= 0 || img.height <= 0 { - 17●
raise @types.LoadError::DecodeFailed( - 18
"edge_detect_sobel: 图像尺寸必须大于0", - 19
) - 20
} - 21
if img.channels < 1 || img.channels > 4 { - 22●
raise @types.LoadError::DecodeFailed( - 23
"edge_detect_sobel: 通道数必须在1-4之间", - 24
) - 25
} - 26
let expected_size = img.width * img.height * img.channels - 27●
if img.data.length() != expected_size { - 28●
raise @types.LoadError::DecodeFailed( - 29
"edge_detect_sobel: 数据大小不匹配,期望 " + - 30●
expected_size.to_string() + - 31
",实际 " + - 32●
img.data.length().to_string(), - 33
) - 34
} - 35
- 36
let w = img.width - 37
let h = img.height - 38
let ch = img.channels - 39
let n = w * h - 40
// 先转灰度 - 41●
let gray : Array[Int32] = Array::make(n, 0) - 42
for i in 0..<n { - 43●
if ch == 1 { - 44○
gray[i] = img.data[i].to_int() - 45
} else { - 46●
let r = img.data[i * ch].to_int() - 47●
let g = img.data[i * ch + 1].to_int() - 48●
let b = img.data[i * ch + 2].to_int() - 49
gray[i] = (r * 299 + g * 587 + b * 114) / 1000 - 50
} - 51
} - 52
// Sobel 卷积 - 53●
let out : Array[Byte] = Array::make(n, b'\x00') - 54
// 内部区域:1 <= x < w-1 && 1 <= y < h-1,无需 clamp - 55
for y in 1..<(h - 1) { - 56●
for x in 1..<(w - 1) { - 57●
let idx = y * w + x - 58
let idx_l = idx - 1 - 59
let idx_r = idx + 1 - 60
let idx_t = idx - w - 61
let idx_b = idx + w - 62
// Gx = [-1 0 1; -2 0 2; -1 0 1] - 63
let gx = -gray[idx_t - 1] + - 64
gray[idx_t + 1] - - 65
2 * gray[idx_l] + - 66
2 * gray[idx_r] - - 67
gray[idx_b - 1] + - 68
gray[idx_b + 1] - 69
// Gy = [-1 -2 -1; 0 0 0; 1 2 1] - 70
let gy = -gray[idx_t - 1] - - 71
2 * gray[idx_t] - - 72
gray[idx_t + 1] + - 73
gray[idx_b - 1] + - 74
2 * gray[idx_b] + - 75
gray[idx_b + 1] - 76●
let mag = Float::from_int(gx * gx + gy * gy).sqrt() - 77●
out[idx] = (mag + 0.5F).to_int().clamp(min=0, max=255).to_byte() - 78
} - 79
} - 80
// 边界区域 - 81
for y in 0..<h { - 82●
for x in 0..<w { - 83●
if x >= 1 && x < w - 1 && y >= 1 && y < h - 1 { - 84●
continue - 85
} - 86●
let x_l = @types.clamp_int(x - 1, 0, w - 1) - 87●
let x_r = @types.clamp_int(x + 1, 0, w - 1) - 88●
let y_t = @types.clamp_int(y - 1, 0, h - 1) - 89●
let y_b = @types.clamp_int(y + 1, 0, h - 1) - 90
let gx = -gray[y_t * w + x_l] + - 91
gray[y_t * w + x_r] - - 92
2 * gray[y * w + x_l] + - 93
2 * gray[y * w + x_r] - - 94
gray[y_b * w + x_l] + - 95
gray[y_b * w + x_r] - 96
let gy = -gray[y_t * w + x_l] - - 97
2 * gray[y_t * w + x] - - 98
gray[y_t * w + x_r] + - 99
gray[y_b * w + x_l] + - 100
2 * gray[y_b * w + x] + - 101
gray[y_b * w + x_r] - 102●
let mag = Float::from_int(gx * gx + gy * gy).sqrt() - 103●
out[y * w + x] = (mag + 0.5F).to_int().clamp(min=0, max=255).to_byte() - 104
} - 105
} - 106
@types.Image::{ - 107
width: w, - 108
height: h, - 109
channels: 1, - 110●
data: Bytes::from_array(out), - 111
} - 112
} - 113
- 114
///| - 115
/// Laplacian 边缘检测。使用 3x3 Laplacian 核: - 116
/// 拉普拉斯核 [0, 1, 0; 1, -4, 1; 0, 1, 0] - 117
/// 返回灰度图(channels=1)。 - 118
pub fn edge_detect_laplacian( - 119
img : @types.Image, - 120
) -> @types.Image raise @types.LoadError { - 121
// 输入验证 - 122●
if img.width <= 0 || img.height <= 0 { - 123●
raise @types.LoadError::DecodeFailed( - 124
"edge_detect_laplacian: 图像尺寸必须大于0", - 125
) - 126
} - 127
if img.channels < 1 || img.channels > 4 { - 128●
raise @types.LoadError::DecodeFailed( - 129
"edge_detect_laplacian: 通道数必须在1-4之间", - 130
) - 131
} - 132
let expected_size = img.width * img.height * img.channels - 133●
if img.data.length() != expected_size { - 134●
raise @types.LoadError::DecodeFailed( - 135
"edge_detect_laplacian: 数据大小不匹配,期望 " + - 136●
expected_size.to_string() + - 137
",实际 " + - 138●
img.data.length().to_string(), - 139
) - 140
} - 141
- 142
let ch = img.channels - 143
let w = img.width - 144
let h = img.height - 145
let n = w * h - 146●
let out : Array[Byte] = Array::make(n, b'\x00') - 147
// 预计算灰度 Int 数组 - 148●
let gray : Array[Int32] = Array::make(n, 0) - 149
for i in 0..<n { - 150●
if ch >= 3 { - 151●
let r = img.data[i * ch].to_int() - 152●
let g = img.data[i * ch + 1].to_int() - 153●
let b = img.data[i * ch + 2].to_int() - 154
gray[i] = (r * 77 + g * 150 + b * 29) / 256 - 155
} else { - 156●
gray[i] = img.data[i * ch].to_int() - 157
} - 158
} - 159
// 内部区域:x in [1, w-1), y in [1, h-1),直接访问 4 邻域 - 160
for y in 1..<(h - 1) { - 161●
for x in 1..<(w - 1) { - 162●
let idx = y * w + x - 163
let center = gray[idx] - 164
let u = gray[idx - w] - 165
let d = gray[idx + w] - 166
let l = gray[idx - 1] - 167
let r = gray[idx + 1] - 168
let lap = u + d + l + r - 4 * center - 169●
out[idx] = @types.clamp_byte(lap.abs()) - 170
} - 171
} - 172
// 边界区域 - 173
for y in 0..<h { - 174●
let y_inner = y >= 1 && y < h - 1 - 175
for x in 0..<w { - 176●
if y_inner && x >= 1 && x < w - 1 { - 177●
continue - 178
} - 179●
let cx = @types.clamp_int(x, 0, w - 1) - 180●
let cy = @types.clamp_int(y, 0, h - 1) - 181●
let up = @types.clamp_int(y - 1, 0, h - 1) - 182●
let down = @types.clamp_int(y + 1, 0, h - 1) - 183●
let left = @types.clamp_int(x - 1, 0, w - 1) - 184●
let right = @types.clamp_int(x + 1, 0, w - 1) - 185
let center = gray[cy * w + cx] - 186
let u = gray[up * w + cx] - 187
let d = gray[down * w + cx] - 188
let l = gray[cy * w + left] - 189
let r = gray[cy * w + right] - 190
let lap = u + d + l + r - 4 * center - 191●
out[y * w + x] = @types.clamp_byte(lap.abs()) - 192
} - 193
} - 194
@types.Image::{ - 195
width: w, - 196
height: h, - 197
channels: 1, - 198●
data: Bytes::from_array(out), - 199
} - 200
} - 201
- 202
///| - 203
/// Prewitt 边缘检测。使用两个 3x3 Prewitt 核计算梯度幅值。 - 204
/// Prewitt 水平梯度 Gx = [-1, 0, 1; -1, 0, 1; -1, 0, 1] - 205
/// Prewitt 垂直梯度 Gy = [-1, -1, -1; 0, 0, 0; 1, 1, 1] - 206
/// 返回灰度图(channels=1)。 - 207
pub fn edge_detect_prewitt( - 208
img : @types.Image, - 209
) -> @types.Image raise @types.LoadError { - 210
// 输入验证 - 211●
if img.width <= 0 || img.height <= 0 { - 212●
raise @types.LoadError::DecodeFailed( - 213
"edge_detect_prewitt: 图像尺寸必须大于0", - 214
) - 215
} - 216
if img.channels < 1 || img.channels > 4 { - 217●
raise @types.LoadError::DecodeFailed( - 218
"edge_detect_prewitt: 通道数必须在1-4之间", - 219
) - 220
} - 221
let expected_size = img.width * img.height * img.channels - 222●
if img.data.length() != expected_size { - 223●
raise @types.LoadError::DecodeFailed( - 224
"edge_detect_prewitt: 数据大小不匹配,期望 " + - 225●
expected_size.to_string() + - 226
",实际 " + - 227●
img.data.length().to_string(), - 228
) - 229
} - 230
- 231
let ch = img.channels - 232
let w = img.width - 233
let h = img.height - 234
let n = w * h - 235●
let out : Array[Byte] = Array::make(n, b'\x00') - 236
// 预计算灰度 Int 数组 - 237●
let gray : Array[Int32] = Array::make(n, 0) - 238
for i in 0..<n { - 239●
if ch >= 3 { - 240●
let r = img.data[i * ch].to_int() - 241●
let g = img.data[i * ch + 1].to_int() - 242●
let b = img.data[i * ch + 2].to_int() - 243
gray[i] = (r * 77 + g * 150 + b * 29) / 256 - 244
} else { - 245●
gray[i] = img.data[i * ch].to_int() - 246
} - 247
} - 248
// 内部区域:x in [1, w-1), y in [1, h-1) - 249
for y in 1..<(h - 1) { - 250●
for x in 1..<(w - 1) { - 251●
let idx = y * w + x - 252
let ul = gray[idx - w - 1] - 253
let ur = gray[idx - w + 1] - 254
let ll = gray[idx + w - 1] - 255
let lr = gray[idx + w + 1] - 256
let l = gray[idx - 1] - 257
let r = gray[idx + 1] - 258
let u = gray[idx - w] - 259
let d = gray[idx + w] - 260
let gx = ur + r + lr - ul - l - ll - 261
let gy = ll + d + lr - ul - u - ur - 262●
let mag = Float::from_int(gx * gx + gy * gy).sqrt() - 263●
out[idx] = @types.clamp_byte(mag.to_int()) - 264
} - 265
} - 266
// 边界区域 - 267
for y in 0..<h { - 268●
let y_inner = y >= 1 && y < h - 1 - 269
for x in 0..<w { - 270●
if y_inner && x >= 1 && x < w - 1 { - 271●
continue - 272
} - 273●
let up = @types.clamp_int(y - 1, 0, h - 1) - 274●
let down = @types.clamp_int(y + 1, 0, h - 1) - 275●
let left = @types.clamp_int(x - 1, 0, w - 1) - 276●
let right = @types.clamp_int(x + 1, 0, w - 1) - 277
let ul = gray[up * w + left] - 278
let ur = gray[up * w + right] - 279
let ll = gray[down * w + left] - 280
let lr = gray[down * w + right] - 281
let l = gray[y * w + left] - 282
let r = gray[y * w + right] - 283
let u = gray[up * w + x] - 284
let d = gray[down * w + x] - 285
let gx = ur + r + lr - ul - l - ll - 286
let gy = ll + d + lr - ul - u - ur - 287●
let mag = Float::from_int(gx * gx + gy * gy).sqrt() - 288●
out[y * w + x] = @types.clamp_byte(mag.to_int()) - 289
} - 290
} - 291
@types.Image::{ - 292
width: w, - 293
height: h, - 294
channels: 1, - 295●
data: Bytes::from_array(out), - 296
} - 297
}