image.process\edge\hough.mbt
95% · 57 / 60 points · 169 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 霍夫变换 hough - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// 霍夫变换(直线检测,纯 MoonBit) - 7
- 8
///| - 9
/// 检测到的直线 - 10
pub(all) struct HoughLine { - 11
rho : Float // 极径(原点到直线的垂直距离) - 12
theta : Float // 极角(弧度) - 13
votes : Int32 // 累加器票数 - 14
} - 15
- 16
///| - 17
/// 霍夫直线变换。对二值图(非零为边缘点)检测直线。 - 18
/// theta_resolution 为角度分辨率(度数,默认 1.0),rho_resolution 为极径分辨率(像素,默认 1.0)。 - 19
/// threshold 为最小票数阈值,返回票数 >= threshold 的直线,按票数降序排列。 - 20
pub fn hough_lines( - 21
img : @types.Image, - 22
threshold : Int32, - 23
theta_resolution~ : Float = 1.0F, - 24
rho_resolution~ : Float = 1.0F, - 25
) -> Array[HoughLine] raise @types.LoadError { - 26
// 输入验证 - 27●
if img.width <= 0 || img.height <= 0 { - 28●
raise @types.LoadError::DecodeFailed( - 29
"hough_lines: 图像尺寸必须大于0", - 30
) - 31
} - 32
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 33●
raise @types.LoadError::DecodeFailed( - 34
"hough_lines: 仅支持 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
"hough_lines: 数据大小不匹配,期望 " + - 41●
expected_size.to_string() + - 42
",实际 " + - 43●
img.data.length().to_string(), - 44
) - 45
} - 46
if threshold <= 0 { - 47●
raise @types.LoadError::DecodeFailed("hough_lines: threshold必须大于0") - 48
} - 49
if theta_resolution <= 0.0F { - 50●
raise @types.LoadError::DecodeFailed( - 51
"hough_lines: theta_resolution必须大于0", - 52
) - 53
} - 54
if rho_resolution <= 0.0F { - 55●
raise @types.LoadError::DecodeFailed( - 56
"hough_lines: rho_resolution必须大于0", - 57
) - 58
} - 59
let w = img.width - 60
let h = img.height - 61
let ch = img.channels - 62
// 角度范围 0~180 度 - 63●
let n_theta = (180.0F / theta_resolution).to_int() - 64●
let diag = Float::from_int(w * w + h * h).sqrt() - 65●
let max_rho = (diag / rho_resolution).to_int() + 1 - 66
let n_rho = 2 * max_rho + 1 - 67
// 累加器(一维数组,提高缓存友好性) - 68●
let accumulator : Array[Int32] = Array::make(n_rho * n_theta, 0) - 69
// 预计算 sin/cos 表 - 70●
let cos_table : Array[Float] = Array::make(n_theta, 0.0F) - 71●
let sin_table : Array[Float] = Array::make(n_theta, 0.0F) - 72
let angle_scale = theta_resolution * 3.141592653589793F / 180.0F - 73
for t in 0..<n_theta { - 74●
let angle = Float::from_int(t) * angle_scale - 75●
cos_table[t] = @math.cosf(angle) - 76●
sin_table[t] = @math.sinf(angle) - 77
} - 78
// 预计算 1/rho_resolution,用乘法替代除法 - 79
let inv_rho_res = 1.0F / rho_resolution - 80
let rho_max_idx = n_rho - 1 - 81
// 预计算首通道 to_int(只看第一个通道判断边缘) - 82●
let data_int : Array[Int32] = Array::make(w * h, 0) - 83
for i in 0..<(w * h) { - 84●
data_int[i] = img.data[i * ch].to_int() - 85
} - 86
// 遍历边缘点 - 87
for y in 0..<h { - 88●
let fy = Float::from_int(y) - 89
let row_base = y * w - 90
for x in 0..<w { - 91●
if data_int[row_base + x] == 0 { - 92●
continue - 93
} - 94●
let fx = Float::from_int(x) - 95
for t in 0..<n_theta { - 96●
let rho = fx * cos_table[t] + fy * sin_table[t] - 97●
let raw_idx = (rho * inv_rho_res).to_int() + max_rho - 98
// 内联 clamp,避免方法调用开销 - 99
let rho_idx = if raw_idx < 0 { - 100○
0 - 101●
} else if raw_idx > rho_max_idx { - 102○
rho_max_idx - 103
} else { - 104●
raw_idx - 105
} - 106
accumulator[rho_idx * n_theta + t] += 1 - 107
} - 108
} - 109
} - 110
// 收集结果 - 111
let lines : Array[HoughLine] = [] - 112●
let max_rho_f = Float::from_int(max_rho) - 113
for r in 0..<n_rho { - 114●
let row_base = r * n_theta - 115
for t in 0..<n_theta { - 116●
let votes = accumulator[row_base + t] - 117
if votes >= threshold { - 118●
lines.push(HoughLine::{ - 119●
rho: (Float::from_int(r) - max_rho_f) * rho_resolution, - 120●
theta: Float::from_int(t) * angle_scale, - 121
votes, - 122
}) - 123
} - 124
} - 125
} - 126
// 按票数降序排序 - 127●
lines.sort_by(fn(a, b) { b.votes - a.votes }) - 128
lines - 129
} - 130
- 131
///| - 132
/// 非极大值抑制。对霍夫变换结果做 NMS,去除相近的重复直线。 - 133
pub fn hough_lines_nms( - 134
lines : Array[HoughLine], - 135
rho_threshold~ : Float = 5.0F, - 136
theta_threshold~ : Float = 0.1F, - 137
) -> Array[HoughLine] raise @types.LoadError { - 138
// 参数验证 - 139●
if rho_threshold <= 0.0F { - 140●
raise @types.LoadError::DecodeFailed( - 141
"hough_lines_nms: rho_threshold必须大于0", - 142
) - 143
} - 144
if theta_threshold <= 0.0F { - 145●
raise @types.LoadError::DecodeFailed( - 146
"hough_lines_nms: theta_threshold必须大于0", - 147
) - 148
} - 149
- 150
let result : Array[HoughLine] = [] - 151●
let suppressed : Array[Bool] = Array::make(lines.length(), false) - 152●
for i in 0..<lines.length() { - 153●
if suppressed[i] { - 154●
continue - 155
} - 156●
result.push(lines[i]) - 157●
for j in (i + 1)..<lines.length() { - 158●
if suppressed[j] { - 159○
continue - 160
} - 161●
let dr = (lines[i].rho - lines[j].rho).abs() - 162●
let dt = (lines[i].theta - lines[j].theta).abs() - 163
if dr < rho_threshold && dt < theta_threshold { - 164●
suppressed[j] = true - 165
} - 166
} - 167
} - 168
result - 169
}