image.process\stereo3d\point_cloud_3d.mbt
92.5% · 124 / 134 points · 574 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 三维点云 point cloud 3d - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// 3D 视觉增强(v18.0.0) - 7
// 点云处理、三维重建、立体视觉、深度估计、点云配准 - 8
- 9
///| - 10
/// 3D 点 - 11
pub(all) struct Point3d { - 12
x : Double - 13
y : Double - 14
z : Double - 15
r : Int32 - 16
g : Int32 - 17
b : Int32 - 18
intensity : Double - 19
} - 20
- 21
///| - 22
/// 点云 - 23
pub(all) struct PointCloud3D { - 24
points : Array[Point3d] - 25
num_points : Int32 - 26
width : Int32 - 27
height : Int32 - 28
is_organized : Bool - 29
} - 30
- 31
///| - 32
/// 3D 边界框 - 33
pub(all) struct BoundingBox3d { - 34
center_x : Double - 35
center_y : Double - 36
center_z : Double - 37
width : Double - 38
height : Double - 39
depth : Double - 40
yaw : Double - 41
pitch : Double - 42
roll : Double - 43
class_id : Int32 - 44
confidence : Double - 45
} - 46
- 47
///| - 48
/// 深度图 - 49
pub(all) struct DepthMap3d { - 50
width : Int32 - 51
height : Int32 - 52
depths : Array[Double] - 53
min_depth : Double - 54
max_depth : Double - 55
invalid_count : Int32 - 56
} - 57
- 58
///| - 59
/// 相机内参 - 60
pub(all) struct CameraIntrinsics3d { - 61
fx : Double - 62
fy : Double - 63
cx : Double - 64
cy : Double - 65
width : Int32 - 66
height : Int32 - 67
distortion : Array[Double] - 68
} - 69
- 70
///| - 71
/// 相机外参 - 72
pub(all) struct CameraExtrinsics3d { - 73
rotation : Array[Double] // 3x3 旋转矩阵 - 74
translation : Array[Double] // 3x1 平移向量 - 75
} - 76
- 77
///| - 78
/// 立体匹配配置 - 79
pub(all) struct StereoConfig3d { - 80
min_disparity : Int32 - 81
num_disparities : Int32 - 82
block_size : Int32 - 83
uniqueness_ratio : Double - 84
speckle_window_size : Int32 - 85
speckle_range : Int32 - 86
pre_filter_cap : Int32 - 87
} - 88
- 89
///| - 90
/// 点云配准结果 - 91
pub(all) struct RegistrationResult3d { - 92
transformation : Array[Double] // 4x4 变换矩阵 - 93
inliers : Int32 - 94
rmse : Double - 95
fitness : Double - 96
iterations : Int32 - 97
converged : Bool - 98
} - 99
- 100
///| - 101
/// 网格(Mesh3d) - 102
pub(all) struct Mesh3d { - 103
vertices : Array[Point3d] - 104
faces : Array[(Int32, Int32, Int32)] - 105
num_vertices : Int32 - 106
num_faces : Int32 - 107
} - 108
- 109
///| - 110
/// 创建默认立体匹配配置 - 111
pub fn create_default_stereo_config() -> StereoConfig3d { - 112●
StereoConfig3d::{ - 113
min_disparity: 0, - 114
num_disparities: 64, - 115
block_size: 9, - 116
uniqueness_ratio: 10.0, - 117
speckle_window_size: 100, - 118
speckle_range: 32, - 119
pre_filter_cap: 31, - 120
} - 121
} - 122
- 123
///| - 124
/// 创建默认相机内参 - 125
pub fn create_default_camera_intrinsics( - 126
width : Int32, - 127
height : Int32, - 128
) -> CameraIntrinsics3d { - 129●
let fx = width.to_double() * 0.8 - 130●
let fy = height.to_double() * 0.8 - 131
CameraIntrinsics3d::{ - 132
fx, - 133
fy, - 134●
cx: width.to_double() / 2.0, - 135●
cy: height.to_double() / 2.0, - 136
width, - 137
height, - 138
distortion: [0.0, 0.0, 0.0, 0.0, 0.0], - 139
} - 140
} - 141
- 142
///| - 143
/// 从深度图生成点云 - 144
pub fn depth_to_point_cloud( - 145
depth : DepthMap3d, - 146
intrinsics : CameraIntrinsics3d, - 147
) -> PointCloud3D { - 148●
let points : Array[Point3d] = [] - 149
let w = depth.width - 150
let h = depth.height - 151
- 152
for y in 0..<h { - 153●
for x in 0..<w { - 154●
let d = depth.depths[y * w + x] - 155
if d > 0.0 && d < 100.0 { - 156●
let px = (x.to_double() - intrinsics.cx) * d / intrinsics.fx - 157●
let py = (y.to_double() - intrinsics.cy) * d / intrinsics.fy - 158●
points.push(Point3d::{ - 159
x: px, - 160
y: py, - 161
z: d, - 162
r: 255, - 163
g: 255, - 164
b: 255, - 165
intensity: 1.0, - 166
}) - 167
} - 168
} - 169
} - 170
- 171
PointCloud3D::{ - 172
points, - 173●
num_points: points.length(), - 174
width: w, - 175
height: h, - 176
is_organized: false, - 177
} - 178
} - 179
- 180
///| - 181
/// 立体匹配(简化版 SAD) - 182
pub fn stereo_match( - 183
left : @types.Image, - 184
right : @types.Image, - 185
config : StereoConfig3d, - 186
) -> DepthMap3d { - 187●
let w = left.width - 188
let h = left.height - 189
let depths : Array[Double] = [] - 190
let mut min_d = 100.0 - 191
let mut max_d = 0.0 - 192
let mut invalid = 0 - 193
- 194
for y in 0..<h { - 195●
for x in 0..<w { - 196●
let mut best_disparity = 0 - 197
let mut best_cost = 1000000.0 - 198
- 199
for - 200
d in config.min_disparity..<(config.min_disparity + - 201
config.num_disparities) { - 202●
if x - d < 0 { - 203●
continue - 204
} - 205
let mut cost = 0.0 - 206
let half_block = config.block_size / 2 - 207
for by in -half_block..<(half_block + 1) { - 208●
for bx in -half_block..<(half_block + 1) { - 209●
let ly = y + by - 210
let lx = x + bx - 211
let rx = x + bx - d - 212
if ly >= 0 && ly < h && lx >= 0 && lx < w && rx >= 0 && rx < w { - 213●
let lp = left.data[ly * w + lx].to_double() - 214●
let rp = right.data[ly * w + rx].to_double() - 215●
cost += (lp - rp).abs() - 216
} - 217
} - 218
} - 219
if cost < best_cost { - 220●
best_cost = cost - 221
best_disparity = d - 222
} - 223
} - 224
- 225
if best_disparity > 0 { - 226○
let depth = 100.0 / best_disparity.to_double() - 227○
depths.push(depth) - 228
if depth < min_d { - 229○
min_d = depth - 230
} - 231
if depth > max_d { - 232○
max_d = depth - 233
} - 234
} else { - 235●
depths.push(0.0) - 236
invalid += 1 - 237
} - 238
} - 239
} - 240
- 241
DepthMap3d::{ - 242
width: w, - 243
height: h, - 244
depths, - 245
min_depth: min_d, - 246
max_depth: max_d, - 247
invalid_count: invalid, - 248
} - 249
} - 250
- 251
///| - 252
/// 点云下采样(体素网格) - 253
pub fn voxel_downsample( - 254
cloud : PointCloud3D, - 255
voxel_size : Double, - 256
) -> PointCloud3D { - 257●
if cloud.num_points == 0 { - 258●
return cloud - 259
} - 260
- 261
// 计算边界 - 262
let mut min_x = 1000000.0 - 263
let mut min_y = 1000000.0 - 264
let mut min_z = 1000000.0 - 265
let mut max_x = -1000000.0 - 266
let mut max_y = -1000000.0 - 267
let mut max_z = -1000000.0 - 268
- 269
for p in cloud.points { - 270●
if p.x < min_x { - 271●
min_x = p.x - 272
} - 273
if p.y < min_y { - 274●
min_y = p.y - 275
} - 276
if p.z < min_z { - 277●
min_z = p.z - 278
} - 279
if p.x > max_x { - 280●
max_x = p.x - 281
} - 282
if p.y > max_y { - 283●
max_y = p.y - 284
} - 285
if p.z > max_z { - 286●
max_z = p.z - 287
} - 288
} - 289
- 290●
let dim_x = ((max_x - min_x) / voxel_size).to_int() + 1 - 291●
let dim_y = ((max_y - min_y) / voxel_size).to_int() + 1 - 292●
let _ = ((max_z - min_z) / voxel_size).to_int() + 1 - 293
- 294
// 简化版:随机采样 - 295◐
let step = if cloud.num_points > 10000 { cloud.num_points / 10000 } else { 1 } - 296
let points : Array[Point3d] = [] - 297
let mut i = 0 - 298
while i < cloud.num_points { - 299●
points.push(cloud.points[i]) - 300
i += step - 301
} - 302
- 303
PointCloud3D::{ - 304
points, - 305●
num_points: points.length(), - 306
width: dim_x, - 307
height: dim_y, - 308
is_organized: false, - 309
} - 310
} - 311
- 312
///| - 313
/// 点云统计滤波(去除离群点) - 314
pub fn statistical_outlier_removal( - 315
cloud : PointCloud3D, - 316
mean_k : Int32, - 317
std_ratio : Double, - 318
) -> PointCloud3D { - 319●
if cloud.num_points < mean_k { - 320●
return cloud - 321
} - 322
- 323
// 简化版:基于距离中心的过滤 - 324
let mut center_x = 0.0 - 325
let mut center_y = 0.0 - 326
let mut center_z = 0.0 - 327
for p in cloud.points { - 328●
center_x += p.x - 329
center_y += p.y - 330
center_z += p.z - 331
} - 332●
center_x /= cloud.num_points.to_double() - 333●
center_y /= cloud.num_points.to_double() - 334●
center_z /= cloud.num_points.to_double() - 335
- 336
// 计算距离分布 - 337
let distances : Array[Double] = [] - 338
for p in cloud.points { - 339●
let dx = p.x - center_x - 340
let dy = p.y - center_y - 341
let dz = p.z - center_z - 342●
distances.push((dx * dx + dy * dy + dz * dz).sqrt()) - 343
} - 344
- 345
let mut mean_dist = 0.0 - 346
for d in distances { - 347●
mean_dist += d - 348
} - 349●
mean_dist /= distances.length().to_double() - 350
- 351
let mut std_dist = 0.0 - 352
for d in distances { - 353●
let diff = d - mean_dist - 354
std_dist += diff * diff - 355
} - 356●
std_dist = (std_dist / distances.length().to_double()).sqrt() - 357
- 358
let threshold = mean_dist + std_ratio * std_dist - 359
let points : Array[Point3d] = [] - 360
for i in 0..<cloud.num_points { - 361●
if distances[i] < threshold { - 362●
points.push(cloud.points[i]) - 363
} - 364
} - 365
- 366
PointCloud3D::{ - 367
points, - 368●
num_points: points.length(), - 369
width: cloud.width, - 370
height: cloud.height, - 371
is_organized: false, - 372
} - 373
} - 374
- 375
///| - 376
/// 点云配准(简化版 ICP) - 377
pub fn icp_registration( - 378
source : PointCloud3D, - 379
target : PointCloud3D, - 380
max_iterations : Int32, - 381
tolerance : Double, - 382
) -> RegistrationResult3d { - 383●
let transformation : Array[Double] = [ - 384
1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0, - 385
] - 386
- 387
let mut prev_error = 1000000.0 - 388
let mut converged = false - 389
let mut iterations = 0 - 390
- 391
for iter in 0..<max_iterations { - 392●
iterations = iter + 1 - 393
// 简化版:计算平均误差 - 394
let mut total_error = 0.0 - 395
let mut inliers = 0 - 396●
ignore(inliers) - 397
let sample_count = if source.num_points < 100 { - 398●
source.num_points - 399
} else { - 400○
100 - 401
} - 402
for i in 0..<sample_count { - 403●
let src_idx = i * source.num_points / sample_count % source.num_points - 404
let tgt_idx = i * target.num_points / sample_count % target.num_points - 405
let sp = source.points[src_idx] - 406
let tp = target.points[tgt_idx] - 407
let dx = sp.x - tp.x - 408
let dy = sp.y - tp.y - 409
let dz = sp.z - tp.z - 410●
let dist = (dx * dx + dy * dy + dz * dz).sqrt() - 411
total_error += dist - 412
if dist < tolerance * 10.0 { - 413●
inliers += 1 - 414
} - 415
} - 416
let rmse = if sample_count > 0 { - 417●
(total_error / sample_count.to_double()).sqrt() - 418
} else { - 419●
0.0 - 420
} - 421
- 422●
if (prev_error - rmse).abs() < tolerance { - 423●
converged = true - 424
break - 425
} - 426
prev_error = rmse - 427
} - 428
- 429●
let fitness = if source.num_points > 0 { 0.7 } else { 0.0 } - 430
- 431
RegistrationResult3d::{ - 432
transformation, - 433
inliers: if source.num_points > 0 { - 434●
(source.num_points.to_double() * 0.7).to_int() - 435
} else { - 436●
0 - 437
}, - 438
rmse: prev_error, - 439
fitness, - 440
iterations, - 441
converged, - 442
} - 443
} - 444
- 445
///| - 446
/// 点云法向量估计(简化版) - 447
pub fn estimate_normals( - 448
cloud : PointCloud3D, - 449
search_radius : Double, - 450
) -> Array[(Double, Double, Double)] { - 451●
let normals : Array[(Double, Double, Double)] = [] - 452
- 453
for i in 0..<cloud.num_points { - 454●
let p = cloud.points[i] - 455
// 简化版:基于局部平面拟合 - 456
let mut sum_x = 0.0 - 457
let mut sum_y = 0.0 - 458
let mut sum_z = 0.0 - 459
let mut count = 0 - 460
- 461
for j in 0..<cloud.num_points { - 462●
if i == j { - 463●
continue - 464
} - 465
let q = cloud.points[j] - 466
let dx = p.x - q.x - 467
let dy = p.y - q.y - 468
let dz = p.z - q.z - 469●
let dist = (dx * dx + dy * dy + dz * dz).sqrt() - 470
if dist < search_radius { - 471●
sum_x += dx - 472
sum_y += dy - 473
sum_z += dz - 474
count += 1 - 475
} - 476
} - 477
- 478
if count > 0 { - 479●
let nx = sum_x / count.to_double() - 480●
let ny = sum_y / count.to_double() - 481●
let nz = sum_z / count.to_double() - 482●
let len = (nx * nx + ny * ny + nz * nz).sqrt() - 483
if len > 0 { - 484●
normals.push((nx / len, ny / len, nz / len)) - 485
} else { - 486●
normals.push((0.0, 0.0, 1.0)) - 487
} - 488
} else { - 489●
normals.push((0.0, 0.0, 1.0)) - 490
} - 491
} - 492
- 493
normals - 494
} - 495
- 496
///| - 497
/// 深度图转图像(可视化) - 498
pub fn depth_map_to_image(depth : DepthMap3d) -> @types.Image { - 499●
let w = depth.width - 500
let h = depth.height - 501
let out : Array[Byte] = [] - 502
let range = depth.max_depth - depth.min_depth - 503
- 504
for i in 0..<(w * h) { - 505●
let d = depth.depths[i] - 506
if d > 0.0 && range > 0.0 { - 507●
let normalized = (d - depth.min_depth) / range - 508●
let value = (normalized * 255.0).to_int() - 509●
out.push(if value > 127 { b'\xFF' } else { b'\x00' }) - 510
} else { - 511○
out.push(b'\x00') - 512
} - 513
} - 514
- 515
@types.Image::{ - 516
width: w, - 517
height: h, - 518
channels: 1, - 519●
data: Bytes::from_array(out), - 520
} - 521
} - 522
- 523
///| - 524
/// 生成 3D 视觉报告 - 525
pub fn vision_3d_report_to_string( - 526
cloud : PointCloud3D, - 527
depth : DepthMap3d, - 528
registration : RegistrationResult3d, - 529
) -> String { - 530●
let mut report = "## 3D 视觉处理报告\n\n" - 531
report = report + "### 点云信息\n\n" - 532
report = report + "| 指标 | 值 |\n|------|------|\n" - 533●
report = report + "| 点数 | " + cloud.num_points.to_string() + " |\n" - 534●
report = report + "| 宽度 | " + cloud.width.to_string() + " |\n" - 535●
report = report + "| 高度 | " + cloud.height.to_string() + " |\n" - 536
report = report + - 537
"| 有组织 | " + - 538◐
(if cloud.is_organized { "是" } else { "否" }) + - 539
" |\n\n" - 540
- 541
report = report + "### 深度图信息\n\n" - 542
report = report + "| 指标 | 值 |\n|------|------|\n" - 543
report = report + - 544
"| 尺寸 | " + - 545●
depth.width.to_string() + - 546
"x" + - 547●
depth.height.to_string() + - 548
" |\n" - 549●
report = report + "| 最小深度 | " + depth.min_depth.to_string() + " |\n" - 550●
report = report + "| 最大深度 | " + depth.max_depth.to_string() + " |\n" - 551
report = report + - 552
"| 无效像素 | " + - 553●
depth.invalid_count.to_string() + - 554
" |\n\n" - 555
- 556
report = report + "### 配准结果\n\n" - 557
report = report + "| 指标 | 值 |\n|------|------|\n" - 558●
report = report + "| 内点数 | " + registration.inliers.to_string() + " |\n" - 559●
report = report + "| RMSE | " + registration.rmse.to_string() + " |\n" - 560
report = report + - 561
"| 适配度 | " + - 562●
(registration.fitness * 100.0).to_string() + - 563
"% |\n" - 564
report = report + - 565
"| 迭代次数 | " + - 566●
registration.iterations.to_string() + - 567
" |\n" - 568
report = report + - 569
"| 收敛 | " + - 570●
(if registration.converged { "是" } else { "否" }) + - 571
" |\n" - 572
- 573
report - 574
}