image.process\stereo3d\stereo_3d.mbt
89.7% · 104 / 116 points · 253 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 立体视觉 stereo 3d - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// 3D 视觉基础(v12.9.0) - 7
// 深度图处理、点云可视化、立体视觉基础 - 8
- 9
///| - 10
/// 点云数据 - 11
pub(all) struct PointCloud { - 12
points : Array[(Double, Double, Double)] // (x, y, z) - 13
colors : Array[(Int32, Int32, Int32)] // (r, g, b) - 14
num_points : Int32 - 15
} - 16
- 17
///| - 18
/// 创建空点云 - 19
pub fn point_cloud_new() -> PointCloud { - 20●
PointCloud::{ points: [], colors: [], num_points: 0, } - 21
} - 22
- 23
///| - 24
/// 深度图可视化(伪彩色映射) - 25
pub fn depth_to_colormap( - 26
depth : @types.Image, - 27
colormap~ : String = "jet", - 28
) -> @types.Image { - 29●
let cmap = colormap - 30
let w = depth.width - 31
let h = depth.height - 32
let channels = depth.channels - 33●
let out_data : Array[Byte] = Array::make(w * h * 3, b'\x00') - 34
// 找到深度范围 - 35
let mut min_depth = 10000000000.0 - 36
let mut max_depth = -10000000000.0 - 37
for y in 0..<h { - 38●
for x in 0..<w { - 39●
let idx = (y * w + x) * channels - 40●
let v = depth.data[idx].to_double() - 41
if v < min_depth { - 42●
min_depth = v - 43
} - 44
if v > max_depth { - 45●
max_depth = v - 46
} - 47
} - 48
} - 49◐
let range = if max_depth > min_depth { max_depth - min_depth } else { 1.0 } - 50
// 伪彩色映射 - 51
for y in 0..<h { - 52●
for x in 0..<w { - 53●
let idx = (y * w + x) * channels - 54●
let v = depth.data[idx].to_double() - 55
let normalized = (v - min_depth) / range - 56
let out_idx = (y * w + x) * 3 - 57
// Jet 颜色映射(蓝 -> 青 -> 绿 -> 黄 -> 红) - 58
let (r, g, b) = if cmap == "jet" { - 59●
if normalized < 0.25 { - 60●
(0, (normalized * 4.0 * 255.0).to_int(), 255) - 61○
} else if normalized < 0.5 { - 62○
(0, 255, ((0.5 - normalized) * 4.0 * 255.0).to_int()) - 63○
} else if normalized < 0.75 { - 64○
(((normalized - 0.5) * 4.0 * 255.0).to_int(), 255, 0) - 65
} else { - 66○
(255, ((1.0 - normalized) * 4.0 * 255.0).to_int(), 0) - 67
} - 68
} else { - 69
// 灰度映射 - 70●
let gray = (normalized * 255.0).to_int() - 71
(gray, gray, gray) - 72
} - 73●
out_data[out_idx] = r.min(255).max(0).to_byte() - 74●
out_data[out_idx + 1] = g.min(255).max(0).to_byte() - 75●
out_data[out_idx + 2] = b.min(255).max(0).to_byte() - 76
} - 77
} - 78
@types.Image::{ - 79
width: w, - 80
height: h, - 81
channels: 3, - 82●
data: Bytes::from_array(out_data), - 83
} - 84
} - 85
- 86
///| - 87
/// 深度图转点云 - 88
pub fn depth_to_pointcloud( - 89
depth : @types.Image, - 90
fx~ : Double = 500.0, - 91
fy~ : Double = 500.0, - 92
cx~ : Double = 0.0, - 93
cy~ : Double = 0.0, - 94
scale~ : Double = 1.0, - 95
) -> PointCloud { - 96●
let focal_x = fx - 97
let focal_y = fy - 98
let center_x = cx - 99
let center_y = cy - 100
let depth_scale = scale - 101
let w = depth.width - 102
let h = depth.height - 103
let channels = depth.channels - 104
let points : Array[(Double, Double, Double)] = [] - 105
let colors : Array[(Int32, Int32, Int32)] = [] - 106
for y in 0..<h { - 107●
for x in 0..<w { - 108●
let idx = (y * w + x) * channels - 109●
let z = depth.data[idx].to_double() * depth_scale - 110
if z > 0.0 { - 111●
let px = (x.to_double() - center_x) * z / focal_x - 112●
let py = (y.to_double() - center_y) * z / focal_y - 113●
points.push((px, py, z)) - 114
// 颜色基于深度 - 115●
let normalized = z.min(255.0) / 255.0 - 116●
let r = (normalized * 255.0).to_int() - 117●
let g = ((1.0 - normalized.abs() - 0.5) * 255.0).to_int() - 118●
let b = ((1.0 - normalized) * 255.0).to_int() - 119●
colors.push((r.min(255).max(0), g.min(255).max(0), b.min(255).max(0))) - 120
} - 121
} - 122
} - 123●
PointCloud::{ points, colors, num_points: points.length(), } - 124
} - 125
- 126
///| - 127
/// 点云体素下采样(简化版) - 128
pub fn pointcloud_voxel_downsample( - 129
cloud : PointCloud, - 130
voxel_size~ : Double = 1.0, - 131
) -> PointCloud { - 132●
let vs = voxel_size - 133
// 简单的体素下采样:每个体素保留第一个点 - 134
let seen : Array[String] = [] - 135
let points : Array[(Double, Double, Double)] = [] - 136
let colors : Array[(Int32, Int32, Int32)] = [] - 137●
for i in 0..<cloud.points.length() { - 138●
let (x, y, z) = cloud.points[i] - 139●
let vx = (x / vs).floor().to_int() - 140●
let vy = (y / vs).floor().to_int() - 141●
let vz = (z / vs).floor().to_int() - 142●
let key = vx.to_string() + "_" + vy.to_string() + "_" + vz.to_string() - 143
let mut found = false - 144
for k in seen { - 145●
if k == key { - 146●
found = true - 147
} - 148
} - 149
if !found { - 150●
seen.push(key) - 151●
points.push((x, y, z)) - 152●
if i < cloud.colors.length() { - 153●
colors.push(cloud.colors[i]) - 154
} else { - 155○
colors.push((255, 255, 255)) - 156
} - 157
} - 158
} - 159●
PointCloud::{ points, colors, num_points: points.length(), } - 160
} - 161
- 162
///| - 163
/// 立体视觉:计算视差图(简化版,基于块匹配) - 164
pub fn compute_disparity( - 165
left : @types.Image, - 166
right : @types.Image, - 167
max_disparity~ : Int32 = 64, - 168
block_size~ : Int32 = 7, - 169
) -> @types.Image { - 170●
let max_disp = max_disparity - 171
let bs = block_size - 172
if left.width != right.width || left.height != right.height { - 173○
return left - 174
} - 175
let w = left.width - 176
let h = left.height - 177●
let channels = left.channels.min(right.channels) - 178
let half = bs / 2 - 179●
let disparity : Array[Int32] = Array::make(w * h, 0) - 180
for y in half..<(h - half) { - 181●
for x in half..<(w - half) { - 182●
let mut best_disp = 0 - 183
let mut best_cost = 10000000000.0 - 184
for d in 0..<max_disp { - 185●
if x - d < half { - 186●
continue - 187
} - 188
// 计算 SAD(绝对差之和) - 189
let mut cost = 0.0 - 190
for dy in -half..<(half + 1) { - 191●
for dx in -half..<(half + 1) { - 192●
let l_idx = ((y + dy) * w + (x + dx)) * channels - 193
let r_idx = ((y + dy) * w + (x + dx - d)) * channels - 194
for c in 0..<channels { - 195●
let diff = (left.data[l_idx + c].to_double() - - 196●
right.data[r_idx + c].to_double()).abs() - 197
cost += diff - 198
} - 199
} - 200
} - 201
if cost < best_cost { - 202●
best_cost = cost - 203
best_disp = d - 204
} - 205
} - 206
disparity[y * w + x] = best_disp - 207
} - 208
} - 209
// 转换为 8 位深度图 - 210●
let out_data : Array[Byte] = Array::make(w * h, b'\x00') - 211
for i in 0..<(w * h) { - 212●
let value = (disparity[i].to_double() / max_disp.to_double() * 255.0).to_int() - 213●
out_data[i] = value.min(255).max(0).to_byte() - 214
} - 215
@types.Image::{ - 216
width: w, - 217
height: h, - 218
channels: 1, - 219●
data: Bytes::from_array(out_data), - 220
} - 221
} - 222
- 223
///| - 224
/// 视差图转深度图 - 225
pub fn disparity_to_depth( - 226
disparity : @types.Image, - 227
baseline~ : Double = 1.0, - 228
focal_length~ : Double = 500.0, - 229
) -> @types.Image { - 230●
let b = baseline - 231
let f = focal_length - 232
let w = disparity.width - 233
let h = disparity.height - 234●
let out_data : Array[Byte] = Array::make(w * h, b'\x00') - 235
for y in 0..<h { - 236●
for x in 0..<w { - 237●
let idx = y * w + x - 238●
let d = disparity.data[idx].to_double() - 239
if d > 0.0 { - 240●
let depth = (b * f / d).min(255.0).max(0.0) - 241●
out_data[idx] = depth.to_int().to_byte() - 242
} else { - 243●
out_data[idx] = b'\x00' - 244
} - 245
} - 246
} - 247
@types.Image::{ - 248
width: w, - 249
height: h, - 250
channels: 1, - 251●
data: Bytes::from_array(out_data), - 252
} - 253
}