image.process\stereo3d\point_cloud_3d.mbt

92.5% · 124 / 134 points · 574 lines

Background color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.

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