image.process\feature\shi_tomasi.mbt
85.7% · 60 / 70 points · 229 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # Shi-Tomasi 角点 shi tomasi - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// Shi-Tomasi 角点检测 (good_features_to_track) - 7
// 基于 Harris 的结构张量,用 min eigenvalue 代替 Harris 响应 - 8
- 9
///| - 10
/// Shi-Tomasi 角点检测(goodFeaturesToTrack)。 - 11
/// 使用结构张量的最小特征值作为响应:R = min(λ1, λ2)。 - 12
/// 适用于光流跟踪的前置角点检测。 - 13
pub fn good_features_to_track( - 14
img : @types.Image, - 15
max_corners : Int32, - 16
quality_level : Float, - 17
min_distance~ : Int32 = 5, - 18
) -> Array[CornerPoint] raise @types.LoadError { - 19
// 输入验证 - 20●
if img.width <= 0 || img.height <= 0 { - 21●
raise @types.LoadError::DecodeFailed( - 22
"good_features_to_track: 图像尺寸必须大于0", - 23
) - 24
} - 25
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 26○
raise @types.LoadError::DecodeFailed( - 27
"good_features_to_track: 仅支持 channels=1/3/4", - 28
) - 29
} - 30
let expected_size = img.width * img.height * img.channels - 31●
if img.data.length() != expected_size { - 32○
raise @types.LoadError::DecodeFailed( - 33
"good_features_to_track: 数据大小不匹配,期望 " + - 34○
expected_size.to_string() + - 35
",实际 " + - 36○
img.data.length().to_string(), - 37
) - 38
} - 39
if max_corners <= 0 { - 40○
raise @types.LoadError::DecodeFailed( - 41
"good_features_to_track: max_corners必须大于0", - 42
) - 43
} - 44
if quality_level <= 0.0F || quality_level > 1.0F { - 45○
raise @types.LoadError::DecodeFailed( - 46
"good_features_to_track: quality_level必须在(0, 1]范围内", - 47
) - 48
} - 49
if min_distance < 0 { - 50○
raise @types.LoadError::DecodeFailed( - 51
"good_features_to_track: min_distance必须 >= 0", - 52
) - 53
} - 54
let w = img.width - 55
let h = img.height - 56
let n = w * h - 57
if n == 0 { - 58○
return [] - 59
} - 60
// 转灰度 - 61●
let gray : Array[Float] = Array::make(n, 0.0F) - 62
let ch = img.channels - 63
for i in 0..<n { - 64●
let v = if ch == 1 { - 65●
img.data[i].to_int() - 66
} else { - 67●
let r = img.data[i * ch].to_int() - 68●
let g = img.data[i * ch + 1].to_int() - 69●
let b = img.data[i * ch + 2].to_int() - 70
(r * 299 + g * 587 + b * 114) / 1000 - 71
} - 72●
gray[i] = Float::from_int(v) - 73
} - 74
// Sobel 梯度 - 75●
let ix : Array[Float] = Array::make(n, 0.0F) - 76●
let iy : Array[Float] = Array::make(n, 0.0F) - 77
for y in 1..<(h - 1) { - 78●
for x in 1..<(w - 1) { - 79●
let idx = y * w + x - 80
ix[idx] = ( - 81
gray[(y - 1) * w + (x + 1)] + - 82
2.0F * gray[y * w + (x + 1)] + - 83
gray[(y + 1) * w + (x + 1)] - - 84
gray[(y - 1) * w + (x - 1)] - - 85
2.0F * gray[y * w + (x - 1)] - - 86
gray[(y + 1) * w + (x - 1)] - 87
) / - 88
8.0F - 89
iy[idx] = ( - 90
gray[(y + 1) * w + (x - 1)] + - 91
2.0F * gray[(y + 1) * w + x] + - 92
gray[(y + 1) * w + (x + 1)] - - 93
gray[(y - 1) * w + (x - 1)] - - 94
2.0F * gray[(y - 1) * w + x] - - 95
gray[(y - 1) * w + (x + 1)] - 96
) / - 97
8.0F - 98
} - 99
} - 100
// 结构张量分量 - 101●
let ixx : Array[Float] = Array::make(n, 0.0F) - 102●
let iyy : Array[Float] = Array::make(n, 0.0F) - 103●
let ixy : Array[Float] = Array::make(n, 0.0F) - 104
for i in 0..<n { - 105●
ixx[i] = ix[i] * ix[i] - 106
iyy[i] = iy[i] * iy[i] - 107
ixy[i] = ix[i] * iy[i] - 108
} - 109
// 高斯加权 - 110●
let sxx = shi_tomasi_gaussian_blur(ixx, w, h) - 111●
let syy = shi_tomasi_gaussian_blur(iyy, w, h) - 112●
let sxy = shi_tomasi_gaussian_blur(ixy, w, h) - 113
// Shi-Tomasi 响应: R = min(λ1, λ2) = (trace - sqrt(trace² - 4*det)) / 2 - 114●
let response : Array[Float] = Array::make(n, 0.0F) - 115
for i in 0..<n { - 116●
let det = sxx[i] * syy[i] - sxy[i] * sxy[i] - 117
let trace = sxx[i] + syy[i] - 118
let disc = trace * trace - 4.0F * det - 119
if disc < 0.0F { - 120○
response[i] = 0.0F - 121
} else { - 122●
response[i] = (trace - disc.sqrt()) / 2.0F - 123
} - 124
} - 125
// 找最大响应值用于 quality threshold - 126
let mut max_response = 0.0F - 127
for i in 0..<n { - 128●
if response[i] > max_response { - 129●
max_response = response[i] - 130
} - 131
} - 132
let threshold = max_response * quality_level - 133
// 非极大值抑制 + 阈值 - 134
let candidates : Array[CornerPoint] = [] - 135
for y in 1..<(h - 1) { - 136●
for x in 1..<(w - 1) { - 137●
let idx = y * w + x - 138
let r = response[idx] - 139
if r <= threshold { - 140●
continue - 141
} - 142
let mut is_max = true - 143
for dy in -1..<=1 { - 144●
for dx in -1..<=1 { - 145●
if dx == 0 && dy == 0 { - 146●
continue - 147
} - 148
if response[(y + dy) * w + (x + dx)] > r { - 149●
is_max = false - 150
break - 151
} - 152
} - 153
if !is_max { - 154●
break - 155
} - 156
} - 157
if is_max { - 158●
candidates.push(CornerPoint::{ x, y, response: r, }) - 159
} - 160
} - 161
} - 162
// 按响应值排序(降序) - 163●
candidates.sort_by(fn(a, b) { b.response.compare(a.response) }) - 164
// 距离过滤 - 165
let result : Array[CornerPoint] = [] - 166
for c in candidates { - 167●
if result.length() >= max_corners { - 168●
break - 169
} - 170
let mut ok = true - 171
for r in result { - 172●
let dx = c.x - r.x - 173
let dy = c.y - r.y - 174
if dx * dx + dy * dy < min_distance * min_distance { - 175●
ok = false - 176
break - 177
} - 178
} - 179
if ok { - 180●
result.push(c) - 181
} - 182
} - 183
result - 184
} - 185
- 186
///| - 187
fn shi_tomasi_gaussian_blur( - 188
data : Array[Float], - 189
w : Int32, - 190
h : Int32, - 191
) -> Array[Float] { - 192●
let n = w * h - 193●
let out : Array[Float] = Array::make(n, 0.0F) - 194
let kernel : Array[Float] = [ - 195
0.0625F, 0.125F, 0.0625F, 0.125F, 0.25F, 0.125F, 0.0625F, 0.125F, 0.0625F, - 196
] - 197
for y in 1..<(h - 1) { - 198●
for x in 1..<(w - 1) { - 199●
let base = (y - 1) * w + (x - 1) - 200
out[y * w + x] = data[base] * kernel[0] + - 201
data[base + 1] * kernel[1] + - 202
data[base + 2] * kernel[2] + - 203
data[base + w] * kernel[3] + - 204
data[base + w + 1] * kernel[4] + - 205
data[base + w + 2] * kernel[5] + - 206
data[base + 2 * w] * kernel[6] + - 207
data[base + 2 * w + 1] * kernel[7] + - 208
data[base + 2 * w + 2] * kernel[8] - 209
} - 210
} - 211
for y in 0..<h { - 212●
let y_inner = y >= 1 && y < h - 1 - 213
for x in 0..<w { - 214●
if y_inner && x >= 1 && x < w - 1 { - 215●
continue - 216
} - 217
let mut sum = 0.0F - 218
for ky in 0..<3 { - 219●
for kx in 0..<3 { - 220●
let px = (x + kx - 1).clamp(min=0, max=w - 1) - 221●
let py = (y + ky - 1).clamp(min=0, max=h - 1) - 222
sum += data[py * w + px] * kernel[ky * 3 + kx] - 223
} - 224
} - 225
out[y * w + x] = sum - 226
} - 227
} - 228
out - 229
}