image.process\feature\harris.mbt
82.6% · 71 / 86 points · 287 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # Harris 角点 harris - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// Harris 角点检测(纯 MoonBit) - 7
- 8
///| - 9
/// 角点信息 - 10
pub(all) struct CornerPoint { - 11
x : Int32 - 12
y : Int32 - 13
response : Float // Harris 响应值 - 14
} - 15
- 16
///| - 17
/// Harris 角点检测。 - 18
/// 适用于灰度图(channels=1)或 RGB/RGBA 图(channels=3/4,自动转灰度)。 - 19
/// k 为 Harris 参数(默认 0.04),threshold 为响应阈值,min_distance 为角点间最小距离。 - 20
pub fn harris_corners( - 21
img : @types.Image, - 22
k~ : Float = 0.04F, - 23
threshold~ : Float = 1000.0F, - 24
min_distance~ : Int32 = 5, - 25
) -> Array[CornerPoint] raise @types.LoadError { - 26
// 输入验证 - 27●
if img.width <= 0 || img.height <= 0 { - 28●
raise @types.LoadError::DecodeFailed( - 29
"harris_corners: 图像尺寸必须大于0", - 30
) - 31
} - 32
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 33○
raise @types.LoadError::DecodeFailed( - 34
"harris_corners: 仅支持 channels=1/3/4", - 35
) - 36
} - 37
let expected_size = img.width * img.height * img.channels - 38●
if img.data.length() != expected_size { - 39○
raise @types.LoadError::DecodeFailed( - 40
"harris_corners: 数据大小不匹配,期望 " + - 41○
expected_size.to_string() + - 42
",实际 " + - 43○
img.data.length().to_string(), - 44
) - 45
} - 46
if k <= 0.0F { - 47●
raise @types.LoadError::DecodeFailed("harris_corners: k必须大于0") - 48
} - 49
if threshold < 0.0F { - 50●
raise @types.LoadError::DecodeFailed("harris_corners: threshold必须 >= 0") - 51
} - 52
if min_distance < 0 { - 53○
raise @types.LoadError::DecodeFailed( - 54
"harris_corners: min_distance必须 >= 0", - 55
) - 56
} - 57
let w = img.width - 58
let h = img.height - 59
let n = w * h - 60
if n == 0 { - 61○
return [] - 62
} - 63
// 转灰度 - 64●
let gray : Array[Float] = Array::make(n, 0.0F) - 65
let ch = img.channels - 66
for i in 0..<n { - 67●
let v = if ch == 1 { - 68●
img.data[i].to_int() - 69
} else { - 70●
let r = img.data[i * ch].to_int() - 71●
let g = img.data[i * ch + 1].to_int() - 72●
let b = img.data[i * ch + 2].to_int() - 73
(r * 299 + g * 587 + b * 114) / 1000 - 74
} - 75●
gray[i] = Float::from_int(v) - 76
} - 77
// Sobel 梯度 - 78●
let ix : Array[Float] = Array::make(n, 0.0F) - 79●
let iy : Array[Float] = Array::make(n, 0.0F) - 80
for y in 1..<(h - 1) { - 81●
for x in 1..<(w - 1) { - 82●
let idx = y * w + x - 83
ix[idx] = ( - 84
gray[(y - 1) * w + (x + 1)] + - 85
2.0F * gray[y * w + (x + 1)] + - 86
gray[(y + 1) * w + (x + 1)] - - 87
gray[(y - 1) * w + (x - 1)] - - 88
2.0F * gray[y * w + (x - 1)] - - 89
gray[(y + 1) * w + (x - 1)] - 90
) / - 91
8.0F - 92
iy[idx] = ( - 93
gray[(y + 1) * w + (x - 1)] + - 94
2.0F * gray[(y + 1) * w + x] + - 95
gray[(y + 1) * w + (x + 1)] - - 96
gray[(y - 1) * w + (x - 1)] - - 97
2.0F * gray[(y - 1) * w + x] - - 98
gray[(y - 1) * w + (x + 1)] - 99
) / - 100
8.0F - 101
} - 102
} - 103
// 计算 Ix², Iy², IxIy - 104●
let ixx : Array[Float] = Array::make(n, 0.0F) - 105●
let iyy : Array[Float] = Array::make(n, 0.0F) - 106●
let ixy : Array[Float] = Array::make(n, 0.0F) - 107
for i in 0..<n { - 108●
ixx[i] = ix[i] * ix[i] - 109
iyy[i] = iy[i] * iy[i] - 110
ixy[i] = ix[i] * iy[i] - 111
} - 112
// 高斯加权(sigma=1, 3x3 核) - 113●
let sxx = harris_gaussian_blur(ixx, w, h) - 114●
let syy = harris_gaussian_blur(iyy, w, h) - 115●
let sxy = harris_gaussian_blur(ixy, w, h) - 116
// Harris 响应: R = det(M) - k * trace(M)² - 117●
let response : Array[Float] = Array::make(n, 0.0F) - 118
for i in 0..<n { - 119●
let det = sxx[i] * syy[i] - sxy[i] * sxy[i] - 120
let trace = sxx[i] + syy[i] - 121
response[i] = det - k * trace * trace - 122
} - 123
// 非极大值抑制 + 阈值 - 124
let candidates : Array[CornerPoint] = [] - 125
for y in 1..<(h - 1) { - 126●
for x in 1..<(w - 1) { - 127●
let idx = y * w + x - 128
let r = response[idx] - 129
if r < threshold { - 130●
continue - 131
} - 132
// 检查是否为局部最大值 - 133
let mut is_max = true - 134
for dy in -1..<=1 { - 135●
for dx in -1..<=1 { - 136●
if dx == 0 && dy == 0 { - 137●
continue - 138
} - 139
if response[(y + dy) * w + (x + dx)] > r { - 140●
is_max = false - 141
break - 142
} - 143
} - 144
if !is_max { - 145●
break - 146
} - 147
} - 148
if is_max { - 149●
candidates.push(CornerPoint::{ x, y, response: r, }) - 150
} - 151
} - 152
} - 153
// 按响应值排序(降序) - 154●
candidates.sort_by(fn(a, b) { b.response.compare(a.response) }) - 155
// 距离过滤 - 156
let result : Array[CornerPoint] = [] - 157
for c in candidates { - 158●
let mut ok = true - 159
for r in result { - 160●
let dx = c.x - r.x - 161
let dy = c.y - r.y - 162
if dx * dx + dy * dy < min_distance * min_distance { - 163●
ok = false - 164
break - 165
} - 166
} - 167
if ok { - 168●
result.push(c) - 169
} - 170
} - 171
result - 172
} - 173
- 174
///| - 175
/// 3x3 高斯模糊(sigma=1.0),分离内部/边界区域以消除热点 clamp 调用 - 176
fn harris_gaussian_blur( - 177
data : Array[Float], - 178
w : Int32, - 179
h : Int32, - 180
) -> Array[Float] { - 181●
let n = w * h - 182●
let out : Array[Float] = Array::make(n, 0.0F) - 183
let kernel : Array[Float] = [ - 184
0.0625F, 0.125F, 0.0625F, 0.125F, 0.25F, 0.125F, 0.0625F, 0.125F, 0.0625F, - 185
] - 186
// 内部区域:x in [1, w-1), y in [1, h-1),3x3 核完全在图像内 - 187
for y in 1..<(h - 1) { - 188●
for x in 1..<(w - 1) { - 189●
let base = (y - 1) * w + (x - 1) - 190
out[y * w + x] = data[base] * kernel[0] + - 191
data[base + 1] * kernel[1] + - 192
data[base + 2] * kernel[2] + - 193
data[base + w] * kernel[3] + - 194
data[base + w + 1] * kernel[4] + - 195
data[base + w + 2] * kernel[5] + - 196
data[base + 2 * w] * kernel[6] + - 197
data[base + 2 * w + 1] * kernel[7] + - 198
data[base + 2 * w + 2] * kernel[8] - 199
} - 200
} - 201
// 边界区域:需要 clamp - 202
for y in 0..<h { - 203●
let y_inner = y >= 1 && y < h - 1 - 204
for x in 0..<w { - 205●
if y_inner && x >= 1 && x < w - 1 { - 206●
continue - 207
} - 208
let mut sum = 0.0F - 209
for ky in 0..<3 { - 210●
for kx in 0..<3 { - 211●
let px = (x + kx - 1).clamp(min=0, max=w - 1) - 212●
let py = (y + ky - 1).clamp(min=0, max=h - 1) - 213
sum += data[py * w + px] * kernel[ky * 3 + kx] - 214
} - 215
} - 216
out[y * w + x] = sum - 217
} - 218
} - 219
out - 220
} - 221
- 222
///| - 223
/// 在图像上绘制角点。 - 224
pub fn draw_corners( - 225
img : @types.Image, - 226
corners : Array[CornerPoint], - 227
color~ : Array[Byte] = [b'\xFF', b'\x00', b'\x00'], - 228
radius~ : Int32 = 2, - 229
) -> @types.Image raise @types.LoadError { - 230
// 输入验证 - 231●
if img.width <= 0 || img.height <= 0 { - 232○
raise @types.LoadError::DecodeFailed( - 233
"draw_corners: 图像尺寸必须大于0", - 234
) - 235
} - 236
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 237○
raise @types.LoadError::DecodeFailed( - 238
"draw_corners: 仅支持 channels=1/3/4", - 239
) - 240
} - 241
let expected_size = img.width * img.height * img.channels - 242●
if img.data.length() != expected_size { - 243○
raise @types.LoadError::DecodeFailed( - 244
"draw_corners: 数据大小不匹配,期望 " + - 245○
expected_size.to_string() + - 246
",实际 " + - 247○
img.data.length().to_string(), - 248
) - 249
} - 250●
if color.length() == 0 { - 251○
raise @types.LoadError::DecodeFailed( - 252
"draw_corners: color数组不能为空", - 253
) - 254
} - 255
if radius < 0 { - 256○
raise @types.LoadError::DecodeFailed("draw_corners: radius必须 >= 0") - 257
} - 258
let ch = img.channels - 259
let w = img.width - 260
let h = img.height - 261●
let out : Array[Byte] = Array::make(w * h * ch, b'\x00') - 262
for i in 0..<(w * h * ch) { - 263●
out[i] = img.data[i] - 264
} - 265●
let draw_ch = if ch >= 3 { 3 } else { 1 } - 266
for corner in corners { - 267●
for dy in -radius..<=radius { - 268●
for dx in -radius..<=radius { - 269●
let nx = corner.x + dx - 270
let ny = corner.y + dy - 271
if nx >= 0 && nx < w && ny >= 0 && ny < h { - 272●
for c in 0..<draw_ch { - 273●
if c < color.length() { - 274●
out[(ny * w + nx) * ch + c] = color[c] - 275
} - 276
} - 277
} - 278
} - 279
} - 280
} - 281
@types.Image::{ - 282
width: w, - 283
height: h, - 284
channels: ch, - 285●
data: Bytes::from_array(out), - 286
} - 287
}