image.vision\fieldimg\field_ops.mbt

95.5% · 126 / 132 points · 359 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/// # 场运算 fieldimg/field_ops
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 场通用算子:梯度、散度、旋度、拉普拉斯、积分、阈值、掩码、代数运算、等值提取。
  7. 7// Field operators: gradient, divergence, curl, laplacian, integrate, clamp, mask, algebra, isosurface.
  8. 8//
  9. 9// 关键映射(GeoVerse 思想):
  10. 10// gradient = 边缘检测(Sobel/Canny 的数学本质)
  11. 11// laplacian = 模糊/锐化(LoG)
  12. 12// integrate = 直方图/总量积分
  13. 13// field_clamp = 阈值分割
  14. 14// isosurface = 等值线提取 → 分割
  15. 15
  16. 16///|
  17. 17/// 梯度:中心差分计算标量场梯度,返回矢量场(dim=2,[gx, gy])。
  18. 18/// 边界使用单侧差分。
  19. 19/// 梯度:标量场中心差分,返回 VectorField Gradient: central difference of a scalar field, returns VectorField (dim=2, [gx, gy]).
  20. 20/// 边界使用单侧差分 Boundary uses one-sided difference.
  21. 21pub fn field_gradient(
  22. 22 field : ScalarField,
  23. 23) -> VectorField raise @types.LoadError {
  24. 24● let w = field.width
  25. 25 let h = field.height
  26. 26 let n = w * h
  27. 27● let data = Array::make(n * 2, 0.0F)
  28. 28 for y in 0..<h {
  29. 29● for x in 0..<w {
  30. 30● let idx = y * w + x
  31. 31 let gx = if x == 0 {
  32. 32● field.data[idx + 1] - field.data[idx]
  33. 33● } else if x == w - 1 {
  34. 34● field.data[idx] - field.data[idx - 1]
  35. 35 } else {
  36. 36● (field.data[idx + 1] - field.data[idx - 1]) * 0.5F
  37. 37 }
  38. 38 let gy = if y == 0 {
  39. 39● field.data[idx + w] - field.data[idx]
  40. 40● } else if y == h - 1 {
  41. 41● field.data[idx] - field.data[idx - w]
  42. 42 } else {
  43. 43● (field.data[idx + w] - field.data[idx - w]) * 0.5F
  44. 44 }
  45. 45 data[idx * 2] = gx
  46. 46 data[idx * 2 + 1] = gy
  47. 47 }
  48. 48 }
  49. 49● VectorField::make(w, h, 2, data)
  50. 50}
  51. 51
  52. 52///|
  53. 53/// 散度:计算 2D 矢量场(dim=2)的散度,返回标量场。
  54. 54/// div = du/dx + dv/dy。边界使用单侧差分。
  55. 55/// 二维向量场散度,返回 ScalarField Divergence of a 2D vector field (dim=2), returns ScalarField.
  56. 56/// 散度 div = du/dx + dv/dy. Boundary uses one-sided difference.
  57. 57pub fn field_divergence(
  58. 58 field : VectorField,
  59. 59) -> ScalarField raise @types.LoadError {
  60. 60● if field.dim != 2 {
  61. 61● raise @types.LoadError::DecodeFailed(
  62. 62 "field_divergence: 矢量场维度必须为2",
  63. 63 )
  64. 64 }
  65. 65 let w = field.width
  66. 66 let h = field.height
  67. 67 let n = w * h
  68. 68● let data = Array::make(n, 0.0F)
  69. 69 for y in 0..<h {
  70. 70● for x in 0..<w {
  71. 71● let idx = y * w + x
  72. 72 let u_here = field.data[idx * 2]
  73. 73 let v_here = field.data[idx * 2 + 1]
  74. 74 let du_dx = if x == 0 {
  75. 75● field.data[(idx + 1) * 2] - u_here
  76. 76● } else if x == w - 1 {
  77. 77● u_here - field.data[(idx - 1) * 2]
  78. 78 } else {
  79. 79● (field.data[(idx + 1) * 2] - field.data[(idx - 1) * 2]) * 0.5F
  80. 80 }
  81. 81 let dv_dy = if y == 0 {
  82. 82● field.data[(idx + w) * 2 + 1] - v_here
  83. 83● } else if y == h - 1 {
  84. 84● v_here - field.data[(idx - w) * 2 + 1]
  85. 85 } else {
  86. 86● (field.data[(idx + w) * 2 + 1] - field.data[(idx - w) * 2 + 1]) * 0.5F
  87. 87 }
  88. 88 data[idx] = du_dx + dv_dy
  89. 89 }
  90. 90 }
  91. 91● ScalarField::make(w, h, data)
  92. 92}
  93. 93
  94. 94///|
  95. 95/// 旋度:计算 2D 矢量场(dim=2)的标量旋度,返回标量场。
  96. 96/// curl = dv/dx - du/dy(2D 中的标量旋度)。边界使用单侧差分。
  97. 97/// 二维向量场标量旋度,返回 ScalarField Scalar curl of a 2D vector field (dim=2), returns ScalarField.
  98. 98/// 旋度 curl = dv/dx - du/dy. Boundary uses one-sided difference.
  99. 99pub fn field_curl(field : VectorField) -> ScalarField raise @types.LoadError {
  100. 100● if field.dim != 2 {
  101. 101○ raise @types.LoadError::DecodeFailed(
  102. 102 "field_curl: 矢量场维度必须为2",
  103. 103 )
  104. 104 }
  105. 105 let w = field.width
  106. 106 let h = field.height
  107. 107 let n = w * h
  108. 108● let data = Array::make(n, 0.0F)
  109. 109 for y in 0..<h {
  110. 110● for x in 0..<w {
  111. 111● let idx = y * w + x
  112. 112 let u_here = field.data[idx * 2]
  113. 113 let v_here = field.data[idx * 2 + 1]
  114. 114 let dv_dx = if x == 0 {
  115. 115● field.data[(idx + 1) * 2 + 1] - v_here
  116. 116● } else if x == w - 1 {
  117. 117● v_here - field.data[(idx - 1) * 2 + 1]
  118. 118 } else {
  119. 119● (field.data[(idx + 1) * 2 + 1] - field.data[(idx - 1) * 2 + 1]) * 0.5F
  120. 120 }
  121. 121 let du_dy = if y == 0 {
  122. 122● field.data[(idx + w) * 2] - u_here
  123. 123● } else if y == h - 1 {
  124. 124● u_here - field.data[(idx - w) * 2]
  125. 125 } else {
  126. 126● (field.data[(idx + w) * 2] - field.data[(idx - w) * 2]) * 0.5F
  127. 127 }
  128. 128 data[idx] = dv_dx - du_dy
  129. 129 }
  130. 130 }
  131. 131● ScalarField::make(w, h, data)
  132. 132}
  133. 133
  134. 134///|
  135. 135/// 拉普拉斯:计算标量场拉普拉斯,返回标量场。
  136. 136/// 拉普拉斯 lap = f[x+1,y] + f[x-1,y] + f[x,y+1] + f[x,y-1] - 4*f[x,y]。
  137. 137/// 边界使用钳制(复制边界值)。
  138. 138/// 标量场拉普拉斯,返回 ScalarField Laplacian of a scalar field, returns ScalarField.
  139. 139/// 拉普拉斯 lap = f[x+1,y] + f[x-1,y] + f[x,y+1] + f[x,y-1] - 4*f[x,y].
  140. 140/// 边界使用钳位(复制边缘)值 Boundary uses clamped (replicate edge) values.
  141. 141pub fn field_laplacian(
  142. 142 field : ScalarField,
  143. 143) -> ScalarField raise @types.LoadError {
  144. 144● let w = field.width
  145. 145 let h = field.height
  146. 146 let n = w * h
  147. 147● let data = Array::make(n, 0.0F)
  148. 148 for y in 0..<h {
  149. 149● for x in 0..<w {
  150. 150● let idx = y * w + x
  151. 151 let center = field.data[idx]
  152. 152● let left = field.evaluate_clamped(x - 1, y)
  153. 153● let right = field.evaluate_clamped(x + 1, y)
  154. 154● let up = field.evaluate_clamped(x, y - 1)
  155. 155● let down = field.evaluate_clamped(x, y + 1)
  156. 156 data[idx] = left + right + up + down - 4.0F * center
  157. 157 }
  158. 158 }
  159. 159● ScalarField::make(w, h, data)
  160. 160}
  161. 161
  162. 162///|
  163. 163/// 积分:标量场全场求和(离散积分,相当于总量/面积积分)。
  164. 164/// 积分:求和所有值(离散积分 = 总和 / 面积积分) Integrate: sum all values (discrete integral = total / area integral).
  165. 165pub fn field_integrate(field : ScalarField) -> Float {
  166. 166● let mut sum = 0.0F
  167. 167● for i in 0..<field.data.length() {
  168. 168● sum = sum + field.data[i]
  169. 169 }
  170. 170 sum
  171. 171}
  172. 172
  173. 173///|
  174. 174/// 区域积分:对标量场在 [x0,x1)×[y0,y1) 区域求和。
  175. 175/// 区域积分 Regional integrate: sum values in the region [x0,x1)×[y0,y1).
  176. 176pub fn field_integrate_region(
  177. 177 field : ScalarField,
  178. 178 x0 : Int32,
  179. 179 y0 : Int32,
  180. 180 x1 : Int32,
  181. 181 y1 : Int32,
  182. 182) -> Float {
  183. 183● let mut sum = 0.0F
  184. 184● let x_lo = x0.clamp(min=0, max=field.width)
  185. 185● let x_hi = x1.clamp(min=0, max=field.width)
  186. 186● let y_lo = y0.clamp(min=0, max=field.height)
  187. 187● let y_hi = y1.clamp(min=0, max=field.height)
  188. 188 for y in y_lo..<y_hi {
  189. 189● for x in x_lo..<x_hi {
  190. 190● sum = sum + field.data[y * field.width + x]
  191. 191 }
  192. 192 }
  193. 193 sum
  194. 194}
  195. 195
  196. 196///|
  197. 197
  198. 198///|
  199. 199/// 阈值裁剪:将场值限制在 [lo, hi] 范围内。
  200. 200/// 将场值钳位到 [lo, hi] Clamp field values to [lo, hi].
  201. 201pub fn field_clamp(
  202. 202 field : ScalarField,
  203. 203 lo : Float,
  204. 204 hi : Float,
  205. 205) -> ScalarField raise @types.LoadError {
  206. 206● let data = Array::make(field.data.length(), 0.0F)
  207. 207● for i in 0..<field.data.length() {
  208. 208● let v = field.data[i]
  209. 209● data[i] = if v < lo { lo } else if v > hi { hi } else { v }
  210. 210 }
  211. 211● ScalarField::make(field.width, field.height, data)
  212. 212}
  213. 213
  214. 214///|
  215. 215/// 掩码:按阈值生成二值掩码场(value >= threshold → 1.0,否则 0.0)。
  216. 216/// 掩膜:二值掩膜场 Mask: binary mask field (value >= threshold → 1.0, else 0.0).
  217. 217pub fn field_mask(
  218. 218 field : ScalarField,
  219. 219 threshold : Float,
  220. 220) -> ScalarField raise @types.LoadError {
  221. 221● let data = Array::make(field.data.length(), 0.0F)
  222. 222● for i in 0..<field.data.length() {
  223. 223● data[i] = if field.data[i] >= threshold { 1.0F } else { 0.0F }
  224. 224 }
  225. 225● ScalarField::make(field.width, field.height, data)
  226. 226}
  227. 227
  228. 228///|
  229. 229/// 场加法:a + b(逐元素)。两场尺寸必须一致。
  230. 230/// 场加法:a + b(逐元素)。两场须同尺寸 Field addition: a + b (element-wise). Both fields must have the same size.
  231. 231pub fn field_add(
  232. 232 a : ScalarField,
  233. 233 b : ScalarField,
  234. 234) -> ScalarField raise @types.LoadError {
  235. 235● if a.width != b.width || a.height != b.height {
  236. 236● raise @types.LoadError::DecodeFailed("field_add: 两场尺寸必须一致")
  237. 237 }
  238. 238● let data = Array::make(a.data.length(), 0.0F)
  239. 239● for i in 0..<a.data.length() {
  240. 240● data[i] = a.data[i] + b.data[i]
  241. 241 }
  242. 242● ScalarField::make(a.width, a.height, data)
  243. 243}
  244. 244
  245. 245///|
  246. 246/// 场减法:a - b(逐元素)。两场尺寸必须一致。
  247. 247/// 场减法:a - b(逐元素)。两场须同尺寸 Field subtraction: a - b (element-wise). Both fields must have the same size.
  248. 248pub fn field_sub(
  249. 249 a : ScalarField,
  250. 250 b : ScalarField,
  251. 251) -> ScalarField raise @types.LoadError {
  252. 252● if a.width != b.width || a.height != b.height {
  253. 253○ raise @types.LoadError::DecodeFailed("field_sub: 两场尺寸必须一致")
  254. 254 }
  255. 255● let data = Array::make(a.data.length(), 0.0F)
  256. 256● for i in 0..<a.data.length() {
  257. 257● data[i] = a.data[i] - b.data[i]
  258. 258 }
  259. 259● ScalarField::make(a.width, a.height, data)
  260. 260}
  261. 261
  262. 262///|
  263. 263/// 场标量乘法:field * scalar(逐元素)。
  264. 264/// 场标量乘法:field * scalar(逐元素) Field scalar multiplication: field * scalar (element-wise).
  265. 265pub fn field_mul_scalar(
  266. 266 field : ScalarField,
  267. 267 scalar : Float,
  268. 268) -> ScalarField raise @types.LoadError {
  269. 269● let data = Array::make(field.data.length(), 0.0F)
  270. 270● for i in 0..<field.data.length() {
  271. 271● data[i] = field.data[i] * scalar
  272. 272 }
  273. 273● ScalarField::make(field.width, field.height, data)
  274. 274}
  275. 275
  276. 276///|
  277. 277/// 场逐元素乘法:a * b(逐元素)。两场尺寸必须一致。
  278. 278/// 场逐元素乘法:a * b。两场须同尺寸 Field element-wise multiplication: a * b. Both fields must have the same size.
  279. 279pub fn field_mul(
  280. 280 a : ScalarField,
  281. 281 b : ScalarField,
  282. 282) -> ScalarField raise @types.LoadError {
  283. 283● if a.width != b.width || a.height != b.height {
  284. 284○ raise @types.LoadError::DecodeFailed("field_mul: 两场尺寸必须一致")
  285. 285 }
  286. 286● let data = Array::make(a.data.length(), 0.0F)
  287. 287● for i in 0..<a.data.length() {
  288. 288● data[i] = a.data[i] * b.data[i]
  289. 289 }
  290. 290● ScalarField::make(a.width, a.height, data)
  291. 291}
  292. 292
  293. 293///|
  294. 294/// 场混合:(1 - alpha) * a + alpha * b。两场尺寸必须一致,alpha 通常在 [0, 1]。
  295. 295/// 场混合:(1 - alpha) * a + alpha * b。须同尺寸 Field blend: (1 - alpha) * a + alpha * b. Same size required; alpha typically in [0, 1].
  296. 296pub fn field_blend(
  297. 297 a : ScalarField,
  298. 298 b : ScalarField,
  299. 299 alpha : Float,
  300. 300) -> ScalarField raise @types.LoadError {
  301. 301● if a.width != b.width || a.height != b.height {
  302. 302○ raise @types.LoadError::DecodeFailed(
  303. 303 "field_blend: 两场尺寸必须一致",
  304. 304 )
  305. 305 }
  306. 306● let data = Array::make(a.data.length(), 0.0F)
  307. 307 let one_minus = 1.0F - alpha
  308. 308● for i in 0..<a.data.length() {
  309. 309● data[i] = one_minus * a.data[i] + alpha * b.data[i]
  310. 310 }
  311. 311● ScalarField::make(a.width, a.height, data)
  312. 312}
  313. 313
  314. 314///|
  315. 315/// 等值面提取:标记 |value - level| < epsilon 的点为 1.0,否则 0.0。
  316. 316/// 在 2D 中即等值线提取,可用于分割。
  317. 317/// 等值面 Isosurface: mark points where |value - level| < epsilon as 1.0, else 0.0.
  318. 318/// 2D 下为 In 2D this is
  319. 319pub fn field_isosurface(
  320. 320 field : ScalarField,
  321. 321 level : Float,
  322. 322 epsilon : Float,
  323. 323) -> ScalarField raise @types.LoadError {
  324. 324● let data = Array::make(field.data.length(), 0.0F)
  325. 325● for i in 0..<field.data.length() {
  326. 326● let diff = field.data[i] - level
  327. 327● let abs_diff = if diff < 0.0F { -diff } else { diff }
  328. 328● data[i] = if abs_diff < epsilon { 1.0F } else { 0.0F }
  329. 329 }
  330. 330● ScalarField::make(field.width, field.height, data)
  331. 331}
  332. 332
  333. 333///|
  334. 334/// 直方图:统计标量场值落在 [min_val, max_val) 分成 bins 个桶的计数。
  335. 335/// 直方图:统计落入 bins 桶的值 Histogram: count values falling into bins buckets over [min_val, max_val).
  336. 336pub fn field_histogram(
  337. 337 field : ScalarField,
  338. 338 min_val : Float,
  339. 339 max_val : Float,
  340. 340 bins : Int32,
  341. 341) -> Array[Int32] raise @types.LoadError {
  342. 342● if bins <= 0 {
  343. 343○ raise @types.LoadError::DecodeFailed("field_histogram: bins 必须大于0")
  344. 344 }
  345. 345● let hist = Array::make(bins, 0)
  346. 346 let range = max_val - min_val
  347. 347 if range <= 0.0F {
  348. 348○ return hist
  349. 349 }
  350. 350● for i in 0..<field.data.length() {
  351. 351● let v = field.data[i]
  352. 352 if v >= min_val && v < max_val {
  353. 353● let idx = ((v - min_val) / range * Float::from_int(bins)).to_int()
  354. 354● let clamped_idx = idx.clamp(min=0, max=bins - 1)
  355. 355 hist[clamped_idx] = hist[clamped_idx] + 1
  356. 356 }
  357. 357 }
  358. 358 hist
  359. 359}