image.process\edge\hough_16f.mbt
93.8% · 91 / 97 points · 222 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 16f - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// 霍夫变换 16-bit/float 变体 - 7
// hough_lines / hough_circles - 8
- 9
///| - 10
/// 霍夫直线变换 16-bit 版本。 - 11
/// @see hough_lines 8-bit 版本,本函数为 Image16 变体 - 12
pub fn hough_lines_16( - 13
img : @types.Image16, - 14
threshold : Int32, - 15
theta_resolution~ : Float = 1.0F, - 16
rho_resolution~ : Float = 1.0F, - 17
) -> Array[HoughLine] raise @types.LoadError { - 18
// 输入验证 - 19●
if img.width <= 0 || img.height <= 0 { - 20●
raise @types.LoadError::DecodeFailed( - 21
"hough_lines_16: 图像尺寸必须大于0", - 22
) - 23
} - 24
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 25●
raise @types.LoadError::DecodeFailed( - 26
"hough_lines_16: 仅支持 channels=1/3/4", - 27
) - 28
} - 29
let expected_size = img.width * img.height * img.channels * 2 - 30●
if img.data.length() != expected_size { - 31●
raise @types.LoadError::DecodeFailed( - 32
"hough_lines_16: 数据大小不匹配,期望 " + - 33●
expected_size.to_string() + - 34
",实际 " + - 35●
img.data.length().to_string(), - 36
) - 37
} - 38
if threshold <= 0 { - 39●
raise @types.LoadError::DecodeFailed( - 40
"hough_lines_16: threshold必须大于0", - 41
) - 42
} - 43
if theta_resolution <= 0.0F { - 44●
raise @types.LoadError::DecodeFailed( - 45
"hough_lines_16: theta_resolution必须大于0", - 46
) - 47
} - 48
if rho_resolution <= 0.0F { - 49○
raise @types.LoadError::DecodeFailed( - 50
"hough_lines_16: rho_resolution必须大于0", - 51
) - 52
} - 53
let w = img.width - 54
let h = img.height - 55
let ch = img.channels - 56●
let n_theta = (180.0F / theta_resolution).to_int() - 57●
let diag = Float::from_int(w * w + h * h).sqrt() - 58●
let max_rho = (diag / rho_resolution).to_int() + 1 - 59
let n_rho = 2 * max_rho + 1 - 60●
let accumulator : Array[Int32] = Array::make(n_rho * n_theta, 0) - 61●
let cos_table : Array[Float] = Array::make(n_theta, 0.0F) - 62●
let sin_table : Array[Float] = Array::make(n_theta, 0.0F) - 63
let angle_scale = theta_resolution * 3.141592653589793F / 180.0F - 64
for t in 0..<n_theta { - 65●
let angle = Float::from_int(t) * angle_scale - 66●
cos_table[t] = @math.cosf(angle) - 67●
sin_table[t] = @math.sinf(angle) - 68
} - 69
let inv_rho_res = 1.0F / rho_resolution - 70
let rho_max_idx = n_rho - 1 - 71●
let data_int : Array[Int32] = Array::make(w * h, 0) - 72
for i in 0..<(w * h) { - 73●
data_int[i] = img.data[i * ch * 2].to_int() | - 74●
(img.data[i * ch * 2 + 1].to_int() << 8) - 75
} - 76
for y in 0..<h { - 77●
let fy = Float::from_int(y) - 78
let row_base = y * w - 79
for x in 0..<w { - 80●
if data_int[row_base + x] == 0 { - 81●
continue - 82
} - 83●
let fx = Float::from_int(x) - 84
for t in 0..<n_theta { - 85●
let rho = fx * cos_table[t] + fy * sin_table[t] - 86●
let raw_idx = (rho * inv_rho_res).to_int() + max_rho - 87
let rho_idx = if raw_idx < 0 { - 88○
0 - 89●
} else if raw_idx > rho_max_idx { - 90○
rho_max_idx - 91
} else { - 92●
raw_idx - 93
} - 94
accumulator[rho_idx * n_theta + t] += 1 - 95
} - 96
} - 97
} - 98
let lines : Array[HoughLine] = [] - 99●
let max_rho_f = Float::from_int(max_rho) - 100
for r in 0..<n_rho { - 101●
let row_base = r * n_theta - 102
for t in 0..<n_theta { - 103●
let votes = accumulator[row_base + t] - 104
if votes >= threshold { - 105●
let rho = (Float::from_int(r) - max_rho_f) * rho_resolution - 106●
let theta = Float::from_int(t) * angle_scale - 107●
lines.push(HoughLine::{ rho, theta, votes, }) - 108
} - 109
} - 110
} - 111●
lines.sort_by(fn(a, b) { b.votes.compare(a.votes) }) - 112
lines - 113
} - 114
- 115
///| - 116
/// 霍夫直线变换 float 版本。 - 117
/// @see hough_lines 8-bit 版本,本函数为 ImageF 变体 - 118
pub fn hough_lines_f( - 119
img : @types.ImageF, - 120
threshold : Int32, - 121
theta_resolution~ : Float = 1.0F, - 122
rho_resolution~ : Float = 1.0F, - 123
) -> Array[HoughLine] raise @types.LoadError { - 124
// 输入验证 - 125●
if img.width <= 0 || img.height <= 0 { - 126●
raise @types.LoadError::DecodeFailed( - 127
"hough_lines_f: 图像尺寸必须大于0", - 128
) - 129
} - 130
if img.channels != 1 && img.channels != 3 && img.channels != 4 { - 131●
raise @types.LoadError::DecodeFailed( - 132
"hough_lines_f: 仅支持 channels=1/3/4", - 133
) - 134
} - 135
let expected_size = img.width * img.height * img.channels * 4 - 136●
if img.data.length() != expected_size { - 137●
raise @types.LoadError::DecodeFailed( - 138
"hough_lines_f: 数据大小不匹配,期望 " + - 139●
expected_size.to_string() + - 140
",实际 " + - 141●
img.data.length().to_string(), - 142
) - 143
} - 144
if threshold <= 0 { - 145●
raise @types.LoadError::DecodeFailed( - 146
"hough_lines_f: threshold必须大于0", - 147
) - 148
} - 149
if theta_resolution <= 0.0F { - 150●
raise @types.LoadError::DecodeFailed( - 151
"hough_lines_f: theta_resolution必须大于0", - 152
) - 153
} - 154
if rho_resolution <= 0.0F { - 155○
raise @types.LoadError::DecodeFailed( - 156
"hough_lines_f: rho_resolution必须大于0", - 157
) - 158
} - 159
let w = img.width - 160
let h = img.height - 161
let ch = img.channels - 162●
let n_theta = (180.0F / theta_resolution).to_int() - 163●
let diag = Float::from_int(w * w + h * h).sqrt() - 164●
let max_rho = (diag / rho_resolution).to_int() + 1 - 165
let n_rho = 2 * max_rho + 1 - 166●
let accumulator : Array[Int32] = Array::make(n_rho * n_theta, 0) - 167●
let cos_table : Array[Float] = Array::make(n_theta, 0.0F) - 168●
let sin_table : Array[Float] = Array::make(n_theta, 0.0F) - 169
let angle_scale = theta_resolution * 3.141592653589793F / 180.0F - 170
for t in 0..<n_theta { - 171●
let angle = Float::from_int(t) * angle_scale - 172●
cos_table[t] = @math.cosf(angle) - 173●
sin_table[t] = @math.sinf(angle) - 174
} - 175
let inv_rho_res = 1.0F / rho_resolution - 176
let rho_max_idx = n_rho - 1 - 177●
let data_f : Array[Float] = Array::make(w * h, 0.0F) - 178
for i in 0..<(w * h) { - 179●
let bits = img.data[i * ch * 4].to_int() | - 180●
(img.data[i * ch * 4 + 1].to_int() << 8) | - 181●
(img.data[i * ch * 4 + 2].to_int() << 16) | - 182●
(img.data[i * ch * 4 + 3].to_int() << 24) - 183●
data_f[i] = Float::reinterpret_from_int(bits) - 184
} - 185
for y in 0..<h { - 186●
let fy = Float::from_int(y) - 187
let row_base = y * w - 188
for x in 0..<w { - 189●
if data_f[row_base + x] == 0.0F { - 190●
continue - 191
} - 192●
let fx = Float::from_int(x) - 193
for t in 0..<n_theta { - 194●
let rho = fx * cos_table[t] + fy * sin_table[t] - 195●
let raw_idx = (rho * inv_rho_res).to_int() + max_rho - 196
let rho_idx = if raw_idx < 0 { - 197○
0 - 198●
} else if raw_idx > rho_max_idx { - 199○
rho_max_idx - 200
} else { - 201●
raw_idx - 202
} - 203
accumulator[rho_idx * n_theta + t] += 1 - 204
} - 205
} - 206
} - 207
let lines : Array[HoughLine] = [] - 208●
let max_rho_f = Float::from_int(max_rho) - 209
for r in 0..<n_rho { - 210●
let row_base = r * n_theta - 211
for t in 0..<n_theta { - 212●
let votes = accumulator[row_base + t] - 213
if votes >= threshold { - 214●
let rho = (Float::from_int(r) - max_rho_f) * rho_resolution - 215●
let theta = Float::from_int(t) * angle_scale - 216●
lines.push(HoughLine::{ rho, theta, votes, }) - 217
} - 218
} - 219
} - 220●
lines.sort_by(fn(a, b) { b.votes.compare(a.votes) }) - 221
lines - 222
}