image.process\stereo3d\stereo_3d.mbt

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