image.process\edge\hough_circles_16f.mbt
82.5% · 113 / 137 points · 347 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 霍夫圆检测 16f hough circles 16f - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// 霍夫圆检测 16-bit/float 变体 - 7
- 8
///| - 9
/// 霍夫圆检测 16-bit 版本。 - 10
/// @see hough_circles 8-bit 版本,本函数为 Image16 变体 - 11
pub fn hough_circles_16( - 12
img : @types.Image16, - 13
dp~ : Int32 = 1, - 14
min_dist~ : Int32 = 10, - 15
param1~ : Int32 = 100, - 16
param2~ : Int32 = 30, - 17
min_radius~ : Int32 = 0, - 18
max_radius~ : Int32 = 0, - 19
) -> Array[Circle] raise @types.LoadError { - 20
// 输入验证 - 21●
if img.width <= 0 || img.height <= 0 { - 22●
raise @types.LoadError::DecodeFailed( - 23
"hough_circles_16: 图像尺寸必须大于0", - 24
) - 25
} - 26
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 27●
raise @types.LoadError::DecodeFailed( - 28
"hough_circles_16: 仅支持 channels=1/3/4", - 29
) - 30
} - 31
let expected_size = img.width * img.height * img.channels * 2 - 32●
if img.data.length() != expected_size { - 33●
raise @types.LoadError::DecodeFailed( - 34
"hough_circles_16: 数据大小不匹配,期望 " + - 35●
expected_size.to_string() + - 36
",实际 " + - 37●
img.data.length().to_string(), - 38
) - 39
} - 40
if dp < 1 { - 41●
raise @types.LoadError::DecodeFailed("hough_circles_16: dp必须 >= 1") - 42
} - 43
if min_dist < 1 { - 44●
raise @types.LoadError::DecodeFailed( - 45
"hough_circles_16: min_dist必须 >= 1", - 46
) - 47
} - 48
if param1 <= 0 { - 49●
raise @types.LoadError::DecodeFailed( - 50
"hough_circles_16: param1必须大于0", - 51
) - 52
} - 53
if param2 <= 0 { - 54●
raise @types.LoadError::DecodeFailed( - 55
"hough_circles_16: param2必须大于0", - 56
) - 57
} - 58
if min_radius < 0 { - 59●
raise @types.LoadError::DecodeFailed( - 60
"hough_circles_16: min_radius必须 >= 0", - 61
) - 62
} - 63
if max_radius < 0 { - 64●
raise @types.LoadError::DecodeFailed( - 65
"hough_circles_16: max_radius必须 >= 0", - 66
) - 67
} - 68
let w = img.width - 69
let h = img.height - 70
let n = w * h - 71
if n == 0 { - 72○
return [] - 73
} - 74●
ignore(dp) - 75
let ch = img.channels - 76●
let gray : Array[Int32] = Array::make(n, 0) - 77
for i in 0..<n { - 78●
gray[i] = if ch == 1 { - 79●
img.data[i * 2].to_int() | (img.data[i * 2 + 1].to_int() << 8) - 80
} else { - 81●
let r = img.data[i * ch * 2].to_int() | - 82●
(img.data[i * ch * 2 + 1].to_int() << 8) - 83●
let g = img.data[(i * ch + 1) * 2].to_int() | - 84●
(img.data[(i * ch + 1) * 2 + 1].to_int() << 8) - 85●
let b = img.data[(i * ch + 2) * 2].to_int() | - 86●
(img.data[(i * ch + 2) * 2 + 1].to_int() << 8) - 87
(r * 299 + g * 587 + b * 114) / 1000 - 88
} - 89
} - 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●
let r_max = if max_radius > 0 { max_radius } else { (w + h) / 4 } - 110
let r_min = min_radius - 111
if r_max <= r_min { - 112○
return [] - 113
} - 114
let acc_w = w - 115
let acc_h = h - 116
let num_r = r_max - r_min + 1 - 117●
let acc : Array[Int32] = Array::make(acc_w * acc_h * num_r, 0) - 118
for y in 1..<(h - 1) { - 119●
for x in 1..<(w - 1) { - 120●
let idx = y * w + x - 121
let mag = gx[idx] * gx[idx] + gy[idx] * gy[idx] - 122
if mag < param1 * param1 { - 123●
continue - 124
} - 125
let dx = gx[idx] - 126
let dy = gy[idx] - 127●
let mag_sqrt = Float::from_int(mag).sqrt().to_int() - 128
if mag_sqrt == 0 { - 129○
continue - 130
} - 131
for r in r_min..<=r_max { - 132●
let cx_pos = x + dx * r / mag_sqrt - 133
let cy_pos = y + dy * r / mag_sqrt - 134
let cx_neg = x - dx * r / mag_sqrt - 135
let cy_neg = y - dy * r / mag_sqrt - 136
if cx_pos >= 0 && cx_pos < acc_w && cy_pos >= 0 && cy_pos < acc_h { - 137●
acc[(cy_pos * acc_w + cx_pos) * num_r + (r - r_min)] += 1 - 138
} - 139
if cx_neg >= 0 && cx_neg < acc_w && cy_neg >= 0 && cy_neg < acc_h { - 140●
acc[(cy_neg * acc_w + cx_neg) * num_r + (r - r_min)] += 1 - 141
} - 142
} - 143
} - 144
} - 145
let candidates : Array[Circle] = [] - 146
for r in r_min..<=r_max { - 147●
for y in 0..<acc_h { - 148●
for x in 0..<acc_w { - 149●
let votes = acc[(y * acc_w + x) * num_r + (r - r_min)] - 150
if votes >= param2 { - 151○
candidates.push(Circle::{ x, y, radius: r, votes, }) - 152
} - 153
} - 154
} - 155
} - 156◐
candidates.sort_by(fn(a, b) { b.votes.compare(a.votes) }) - 157
let result : Array[Circle] = [] - 158
for c in candidates { - 159○
let mut ok = true - 160
for r in result { - 161○
let dx = c.x - r.x - 162
let dy = c.y - r.y - 163
if dx * dx + dy * dy < min_dist * min_dist { - 164○
ok = false - 165
break - 166
} - 167
} - 168
if ok { - 169○
result.push(c) - 170
} - 171
} - 172
result - 173
} - 174
- 175
///| - 176
/// 霍夫圆检测 float 版本。 - 177
/// @see hough_circles 8-bit 版本,本函数为 ImageF 变体 - 178
pub fn hough_circles_f( - 179
img : @types.ImageF, - 180
dp~ : Int32 = 1, - 181
min_dist~ : Int32 = 10, - 182
param1~ : Int32 = 100, - 183
param2~ : Int32 = 30, - 184
min_radius~ : Int32 = 0, - 185
max_radius~ : Int32 = 0, - 186
) -> Array[Circle] raise @types.LoadError { - 187
// 输入验证 - 188●
if img.width <= 0 || img.height <= 0 { - 189●
raise @types.LoadError::DecodeFailed( - 190
"hough_circles_f: 图像尺寸必须大于0", - 191
) - 192
} - 193
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 194●
raise @types.LoadError::DecodeFailed( - 195
"hough_circles_f: 仅支持 channels=1/3/4", - 196
) - 197
} - 198
let expected_size = img.width * img.height * img.channels * 4 - 199●
if img.data.length() != expected_size { - 200●
raise @types.LoadError::DecodeFailed( - 201
"hough_circles_f: 数据大小不匹配,期望 " + - 202●
expected_size.to_string() + - 203
",实际 " + - 204●
img.data.length().to_string(), - 205
) - 206
} - 207
if dp < 1 { - 208●
raise @types.LoadError::DecodeFailed("hough_circles_f: dp必须 >= 1") - 209
} - 210
if min_dist < 1 { - 211●
raise @types.LoadError::DecodeFailed("hough_circles_f: min_dist必须 >= 1") - 212
} - 213
if param1 <= 0 { - 214●
raise @types.LoadError::DecodeFailed("hough_circles_f: param1必须大于0") - 215
} - 216
if param2 <= 0 { - 217●
raise @types.LoadError::DecodeFailed("hough_circles_f: param2必须大于0") - 218
} - 219
if min_radius < 0 { - 220●
raise @types.LoadError::DecodeFailed( - 221
"hough_circles_f: min_radius必须 >= 0", - 222
) - 223
} - 224
if max_radius < 0 { - 225●
raise @types.LoadError::DecodeFailed( - 226
"hough_circles_f: max_radius必须 >= 0", - 227
) - 228
} - 229
let w = img.width - 230
let h = img.height - 231
let n = w * h - 232
if n == 0 { - 233○
return [] - 234
} - 235●
ignore(dp) - 236
let ch = img.channels - 237●
let gray : Array[Float] = Array::make(n, 0.0F) - 238
for i in 0..<n { - 239●
gray[i] = if ch == 1 { - 240●
let bits = img.data[i * 4].to_int() | - 241●
(img.data[i * 4 + 1].to_int() << 8) | - 242●
(img.data[i * 4 + 2].to_int() << 16) | - 243●
(img.data[i * 4 + 3].to_int() << 24) - 244●
Float::reinterpret_from_int(bits) - 245
} else { - 246●
let bits_r = img.data[i * ch * 4].to_int() | - 247●
(img.data[i * ch * 4 + 1].to_int() << 8) | - 248●
(img.data[i * ch * 4 + 2].to_int() << 16) | - 249●
(img.data[i * ch * 4 + 3].to_int() << 24) - 250●
let bits_g = img.data[(i * ch + 1) * 4].to_int() | - 251●
(img.data[(i * ch + 1) * 4 + 1].to_int() << 8) | - 252●
(img.data[(i * ch + 1) * 4 + 2].to_int() << 16) | - 253●
(img.data[(i * ch + 1) * 4 + 3].to_int() << 24) - 254●
let bits_b = img.data[(i * ch + 2) * 4].to_int() | - 255●
(img.data[(i * ch + 2) * 4 + 1].to_int() << 8) | - 256●
(img.data[(i * ch + 2) * 4 + 2].to_int() << 16) | - 257●
(img.data[(i * ch + 2) * 4 + 3].to_int() << 24) - 258●
Float::reinterpret_from_int(bits_r) * 0.299F + - 259●
Float::reinterpret_from_int(bits_g) * 0.587F + - 260●
Float::reinterpret_from_int(bits_b) * 0.114F - 261
} - 262
} - 263●
let gx : Array[Float] = Array::make(n, 0.0F) - 264●
let gy : Array[Float] = Array::make(n, 0.0F) - 265
for y in 1..<(h - 1) { - 266●
for x in 1..<(w - 1) { - 267●
let idx = y * w + x - 268
gx[idx] = gray[(y - 1) * w + (x + 1)] + - 269
2.0F * gray[y * w + (x + 1)] + - 270
gray[(y + 1) * w + (x + 1)] - - 271
gray[(y - 1) * w + (x - 1)] - - 272
2.0F * gray[y * w + (x - 1)] - - 273
gray[(y + 1) * w + (x - 1)] - 274
gy[idx] = gray[(y + 1) * w + (x - 1)] + - 275
2.0F * gray[(y + 1) * w + x] + - 276
gray[(y + 1) * w + (x + 1)] - - 277
gray[(y - 1) * w + (x - 1)] - - 278
2.0F * gray[(y - 1) * w + x] - - 279
gray[(y - 1) * w + (x + 1)] - 280
} - 281
} - 282●
let r_max = if max_radius > 0 { max_radius } else { (w + h) / 4 } - 283
let r_min = min_radius - 284
if r_max <= r_min { - 285○
return [] - 286
} - 287
let acc_w = w - 288
let acc_h = h - 289
let num_r = r_max - r_min + 1 - 290●
let acc : Array[Int32] = Array::make(acc_w * acc_h * num_r, 0) - 291●
let param1_f = Float::from_int(param1) / 255.0F - 292
for y in 1..<(h - 1) { - 293●
for x in 1..<(w - 1) { - 294●
let idx = y * w + x - 295
let mag = gx[idx] * gx[idx] + gy[idx] * gy[idx] - 296
if mag < param1_f * param1_f { - 297●
continue - 298
} - 299
let dx = gx[idx] - 300
let dy = gy[idx] - 301●
let mag_sqrt = mag.sqrt() - 302
if mag_sqrt == 0.0F { - 303○
continue - 304
} - 305
for r in r_min..<=r_max { - 306●
let cx_pos = x + (dx * Float::from_int(r) / mag_sqrt).to_int() - 307●
let cy_pos = y + (dy * Float::from_int(r) / mag_sqrt).to_int() - 308●
let cx_neg = x - (dx * Float::from_int(r) / mag_sqrt).to_int() - 309●
let cy_neg = y - (dy * Float::from_int(r) / mag_sqrt).to_int() - 310
if cx_pos >= 0 && cx_pos < acc_w && cy_pos >= 0 && cy_pos < acc_h { - 311●
acc[(cy_pos * acc_w + cx_pos) * num_r + (r - r_min)] += 1 - 312
} - 313
if cx_neg >= 0 && cx_neg < acc_w && cy_neg >= 0 && cy_neg < acc_h { - 314●
acc[(cy_neg * acc_w + cx_neg) * num_r + (r - r_min)] += 1 - 315
} - 316
} - 317
} - 318
} - 319
let candidates : Array[Circle] = [] - 320
for r in r_min..<=r_max { - 321●
for y in 0..<acc_h { - 322●
for x in 0..<acc_w { - 323●
let votes = acc[(y * acc_w + x) * num_r + (r - r_min)] - 324
if votes >= param2 { - 325○
candidates.push(Circle::{ x, y, radius: r, votes, }) - 326
} - 327
} - 328
} - 329
} - 330◐
candidates.sort_by(fn(a, b) { b.votes.compare(a.votes) }) - 331
let result : Array[Circle] = [] - 332
for c in candidates { - 333○
let mut ok = true - 334
for r in result { - 335○
let dx = c.x - r.x - 336
let dy = c.y - r.y - 337
if dx * dx + dy * dy < min_dist * min_dist { - 338○
ok = false - 339
break - 340
} - 341
} - 342
if ok { - 343○
result.push(c) - 344
} - 345
} - 346
result - 347
}