image.process\edge\hough_circles.mbt
87.3% · 48 / 55 points · 180 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 circles - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// 霍夫圆检测(纯 MoonBit):hough_circles - 7
// 梯度法:对边缘点沿梯度方向投票 - 8
- 9
///| - 10
/// 检测到的圆 - 11
pub(all) struct Circle { - 12
x : Int32 - 13
y : Int32 - 14
radius : Int32 - 15
votes : Int32 - 16
} - 17
- 18
///| - 19
/// 霍夫圆检测。 - 20
/// dp 为累加器分辨率倒数(1=同分辨率),min_dist 为圆心间最小距离。 - 21
/// param1 为 Canny 高阈值,param2 为圆心投票阈值。 - 22
pub fn hough_circles( - 23
img : @types.Image, - 24
dp~ : Int32 = 1, - 25
min_dist~ : Int32 = 10, - 26
param1~ : Int32 = 100, - 27
param2~ : Int32 = 30, - 28
min_radius~ : Int32 = 0, - 29
max_radius~ : Int32 = 0, - 30
) -> Array[Circle] raise @types.LoadError { - 31
// 输入验证 - 32●
if img.width <= 0 || img.height <= 0 { - 33●
raise @types.LoadError::DecodeFailed( - 34
"hough_circles: 图像尺寸必须大于0", - 35
) - 36
} - 37
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 38●
raise @types.LoadError::DecodeFailed( - 39
"hough_circles: 仅支持 channels=1/3/4", - 40
) - 41
} - 42
let expected_size = img.width * img.height * img.channels - 43●
if img.data.length() != expected_size { - 44●
raise @types.LoadError::DecodeFailed( - 45
"hough_circles: 数据大小不匹配,期望 " + - 46●
expected_size.to_string() + - 47
",实际 " + - 48●
img.data.length().to_string(), - 49
) - 50
} - 51
if dp < 1 { - 52●
raise @types.LoadError::DecodeFailed("hough_circles: dp必须 >= 1") - 53
} - 54
if min_dist < 1 { - 55●
raise @types.LoadError::DecodeFailed("hough_circles: min_dist必须 >= 1") - 56
} - 57
if param1 <= 0 { - 58●
raise @types.LoadError::DecodeFailed("hough_circles: param1必须大于0") - 59
} - 60
if param2 <= 0 { - 61●
raise @types.LoadError::DecodeFailed("hough_circles: param2必须大于0") - 62
} - 63
if min_radius < 0 { - 64●
raise @types.LoadError::DecodeFailed("hough_circles: min_radius必须 >= 0") - 65
} - 66
if max_radius < 0 { - 67●
raise @types.LoadError::DecodeFailed("hough_circles: max_radius必须 >= 0") - 68
} - 69
let w = img.width - 70
let h = img.height - 71
let n = w * h - 72
if n == 0 { - 73○
return [] - 74
} - 75●
ignore(dp) - 76
// 转灰度 - 77●
let gray : Array[Int32] = Array::make(n, 0) - 78
let ch = img.channels - 79
for i in 0..<n { - 80●
gray[i] = if ch == 1 { - 81●
img.data[i].to_int() - 82
} else { - 83○
let r = img.data[i * ch].to_int() - 84○
let g = img.data[i * ch + 1].to_int() - 85○
let b = img.data[i * ch + 2].to_int() - 86
(r * 299 + g * 587 + b * 114) / 1000 - 87
} - 88
} - 89
// Sobel 梯度 - 90●
let gx : Array[Int32] = Array::make(n, 0) - 91●
let gy : Array[Int32] = Array::make(n, 0) - 92
for y in 1..<(h - 1) { - 93●
for x in 1..<(w - 1) { - 94●
let idx = y * w + x - 95
gx[idx] = gray[(y - 1) * w + (x + 1)] + - 96
2 * gray[y * w + (x + 1)] + - 97
gray[(y + 1) * w + (x + 1)] - - 98
gray[(y - 1) * w + (x - 1)] - - 99
2 * gray[y * w + (x - 1)] - - 100
gray[(y + 1) * w + (x - 1)] - 101
gy[idx] = gray[(y + 1) * w + (x - 1)] + - 102
2 * gray[(y + 1) * w + x] + - 103
gray[(y + 1) * w + (x + 1)] - - 104
gray[(y - 1) * w + (x - 1)] - - 105
2 * gray[(y - 1) * w + x] - - 106
gray[(y - 1) * w + (x + 1)] - 107
} - 108
} - 109
// 边缘点(梯度幅值 > param1) - 110●
let r_max = if max_radius > 0 { max_radius } else { (w + h) / 4 } - 111
let r_min = min_radius - 112
if r_max <= r_min { - 113○
return [] - 114
} - 115
// 累加器:3D [w * dp][h * dp][r_max - r_min + 1] - 116
let acc_w = w - 117
let acc_h = h - 118
let num_r = r_max - r_min + 1 - 119●
let acc : Array[Int32] = Array::make(acc_w * acc_h * num_r, 0) - 120
// 对每个边缘点投票 - 121
for y in 1..<(h - 1) { - 122●
for x in 1..<(w - 1) { - 123●
let idx = y * w + x - 124
let mag = gx[idx] * gx[idx] + gy[idx] * gy[idx] - 125
if mag < param1 * param1 { - 126●
continue - 127
} - 128
// 沿梯度方向投票 - 129
let dx = gx[idx] - 130
let dy = gy[idx] - 131●
let mag_sqrt = Float::from_int(mag).sqrt().to_int() - 132
if mag_sqrt == 0 { - 133○
continue - 134
} - 135
for r in r_min..<=r_max { - 136●
let cx_pos = x + dx * r / mag_sqrt - 137
let cy_pos = y + dy * r / mag_sqrt - 138
let cx_neg = x - dx * r / mag_sqrt - 139
let cy_neg = y - dy * r / mag_sqrt - 140
if cx_pos >= 0 && cx_pos < acc_w && cy_pos >= 0 && cy_pos < acc_h { - 141●
acc[(cy_pos * acc_w + cx_pos) * num_r + (r - r_min)] += 1 - 142
} - 143
if cx_neg >= 0 && cx_neg < acc_w && cy_neg >= 0 && cy_neg < acc_h { - 144●
acc[(cy_neg * acc_w + cx_neg) * num_r + (r - r_min)] += 1 - 145
} - 146
} - 147
} - 148
} - 149
// 找峰值 - 150
let candidates : Array[Circle] = [] - 151
for r in r_min..<=r_max { - 152●
for y in 0..<acc_h { - 153●
for x in 0..<acc_w { - 154●
let votes = acc[(y * acc_w + x) * num_r + (r - r_min)] - 155
if votes >= param2 { - 156●
candidates.push(Circle::{ x, y, radius: r, votes, }) - 157
} - 158
} - 159
} - 160
} - 161
// 按投票数排序(降序) - 162●
candidates.sort_by(fn(a, b) { b.votes.compare(a.votes) }) - 163
// 距离过滤 - 164
let result : Array[Circle] = [] - 165
for c in candidates { - 166●
let mut ok = true - 167
for r in result { - 168●
let dx = c.x - r.x - 169
let dy = c.y - r.y - 170
if dx * dx + dy * dy < min_dist * min_dist { - 171●
ok = false - 172
break - 173
} - 174
} - 175
if ok { - 176●
result.push(c) - 177
} - 178
} - 179
result - 180
}