image.vision\fieldimg\tensor_field.mbt

62.1% · 18 / 29 points · 195 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/tensor_field
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 张量场:2D 张量场,每个点 (x, y) 映射到 rows×cols 矩阵。
  7. 7// Tensor field: 2D tensor field, each point (x, y) maps to a rows×cols matrix.
  8. 8// 典型应用:结构张量(2×2 对称)用于角点检测,梯度张量用于边缘分析。
  9. 9
  10. 10///|
  11. 11/// 张量场:2D 张量场,data 为行主序,长度 = width * height * rows * cols。
  12. 12/// 张量场:2D Tensor field: 2D
  13. 13#derive(Eq, @debug.Debug)
  14. 14pub(all) struct TensorField {
  15. 15 width : Int32
  16. 16 height : Int32
  17. 17 rows : Int32
  18. 18 cols : Int32
  19. 19 data : Array[Float]
  20. 20}
  21. 21
  22. 22///|
  23. 23pub extend TensorField with @debug.Debug::{to_repr}
  24. 24
  25. 25///|
  26. 26pub extend TensorField with Eq::{not_equal, equal}
  27. 27
  28. 28///|
  29. 29/// 创建张量场,数据长度必须等于 width * height * rows * cols。
  30. 30/// 创建张量场;数据长度须等于 width * height * rows * cols Create a tensor field; data length must equal width * height * rows * cols.
  31. 31pub fn TensorField::make(
  32. 32 width : Int32,
  33. 33 height : Int32,
  34. 34 rows : Int32,
  35. 35 cols : Int32,
  36. 36 data : Array[Float],
  37. 37) -> TensorField raise @types.LoadError {
  38. 38● if width <= 0 || height <= 0 || rows <= 0 || cols <= 0 {
  39. 39○ raise @types.LoadError::DecodeFailed(
  40. 40 "TensorField::make: width, height, rows, cols 必须大于0",
  41. 41 )
  42. 42 }
  43. 43● if data.length() != width * height * rows * cols {
  44. 44● raise @types.LoadError::DecodeFailed(
  45. 45 "TensorField::make: 数据长度必须等于 width * height * rows * cols",
  46. 46 )
  47. 47 }
  48. 48 { width, height, rows, cols, data, }
  49. 49}
  50. 50
  51. 51///|
  52. 52/// 创建全零张量场。
  53. 53/// 创建全零张量场 Create an all-zero tensor field.
  54. 54pub fn TensorField::zeros(
  55. 55 width : Int32,
  56. 56 height : Int32,
  57. 57 rows : Int32,
  58. 58 cols : Int32,
  59. 59) -> TensorField raise @types.LoadError {
  60. 60● if width <= 0 || height <= 0 || rows <= 0 || cols <= 0 {
  61. 61○ raise @types.LoadError::DecodeFailed(
  62. 62 "TensorField::zeros: width, height, rows, cols 必须大于0",
  63. 63 )
  64. 64 }
  65. 65 {
  66. 66 width,
  67. 67 height,
  68. 68 rows,
  69. 69 cols,
  70. 70● data: Array::make(width * height * rows * cols, 0.0F),
  71. 71 }
  72. 72}
  73. 73
  74. 74///|
  75. 75/// 求值单元素:获取点 (x, y) 处矩阵第 (r, c) 元素,边界外返回 0。
  76. 76/// 求值元素 (r, c) 于 (x, y);越界返回 0 Evaluate element (r, c) at (x, y); returns 0 for out-of-bounds.
  77. 77pub fn TensorField::evaluate(
  78. 78 self : TensorField,
  79. 79 x : Int32,
  80. 80 y : Int32,
  81. 81 r : Int32,
  82. 82 c : Int32,
  83. 83) -> Float {
  84. 84● if x < 0 ||
  85. 85 x >= self.width ||
  86. 86 y < 0 ||
  87. 87 y >= self.height ||
  88. 88 r < 0 ||
  89. 89 r >= self.rows ||
  90. 90 c < 0 ||
  91. 91 c >= self.cols {
  92. 92○ return 0.0F
  93. 93 }
  94. 94 self.data[((y * self.width + x) * self.rows + r) * self.cols + c]
  95. 95}
  96. 96
  97. 97///|
  98. 98/// 求值单元素(边界钳制)。
  99. 99/// 钳位边界求值元素 Evaluate element with clamped boundary.
  100. 100pub fn TensorField::evaluate_clamped(
  101. 101 self : TensorField,
  102. 102 x : Int32,
  103. 103 y : Int32,
  104. 104 r : Int32,
  105. 105 c : Int32,
  106. 106) -> Float {
  107. 107○ let cx = x.clamp(min=0, max=self.width - 1)
  108. 108○ let cy = y.clamp(min=0, max=self.height - 1)
  109. 109○ let cr = r.clamp(min=0, max=self.rows - 1)
  110. 110○ let cc = c.clamp(min=0, max=self.cols - 1)
  111. 111 self.data[((cy * self.width + cx) * self.rows + cr) * self.cols + cc]
  112. 112}
  113. 113
  114. 114///|
  115. 115/// 设值单元素:设置点 (x, y) 处矩阵第 (r, c) 元素,越界则忽略。
  116. 116/// 设置元素 (r, c) 于 (x, y);越界忽略 Set element (r, c) at (x, y); out-of-bounds is ignored.
  117. 117pub fn TensorField::set(
  118. 118 self : TensorField,
  119. 119 x : Int32,
  120. 120 y : Int32,
  121. 121 r : Int32,
  122. 122 c : Int32,
  123. 123 value : Float,
  124. 124) -> Unit {
  125. 125● if x < 0 ||
  126. 126 x >= self.width ||
  127. 127 y < 0 ||
  128. 128 y >= self.height ||
  129. 129 r < 0 ||
  130. 130 r >= self.rows ||
  131. 131 c < 0 ||
  132. 132 c >= self.cols {
  133. 133○ return
  134. 134 }
  135. 135 self.data[((y * self.width + x) * self.rows + r) * self.cols + c] = value
  136. 136}
  137. 137
  138. 138///|
  139. 139/// 由梯度矢量场构建结构张量场(2×2 对称)。
  140. 140/// 结构张量 J = [[Ix², Ix*Iy], [Ix*Iy, Iy²]],用于角点检测。
  141. 141/// 从梯度向量场构建结构张量场 Build structure tensor field (2×2 symmetric) from a gradient vector field (dim=2).
  142. 142/// 结构张量 J,用于角点检测 Structure tensor J = [[Ix², Ix*Iy], [Ix*Iy, Iy²]], used for corner detection.
  143. 143pub fn TensorField::structure_tensor(
  144. 144 gradient : VectorField,
  145. 145) -> TensorField raise @types.LoadError {
  146. 146● if gradient.dim != 2 {
  147. 147● raise @types.LoadError::DecodeFailed(
  148. 148 "structure_tensor: 梯度场维度必须为2",
  149. 149 )
  150. 150 }
  151. 151 let w = gradient.width
  152. 152 let h = gradient.height
  153. 153 let n = w * h
  154. 154● let data = Array::make(n * 4, 0.0F)
  155. 155 for i in 0..<n {
  156. 156● let ix = gradient.data[i * 2]
  157. 157 let iy = gradient.data[i * 2 + 1]
  158. 158 data[i * 4] = ix * ix
  159. 159 data[i * 4 + 1] = ix * iy
  160. 160 data[i * 4 + 2] = ix * iy
  161. 161 data[i * 4 + 3] = iy * iy
  162. 162 }
  163. 163● TensorField::make(w, h, 2, 2, data)
  164. 164}
  165. 165
  166. 166///|
  167. 167/// 计算结构张量的两个特征值,返回矢量场(dim=2,[λ1, λ2],λ1 ≥ λ2)。
  168. 168/// 特征值用于 Harris 角点响应:R = det(J) - k * trace(J)² = λ1*λ2 - k*(λ1+λ2)²。
  169. 169/// 计算结构张量特征值 Compute eigenvalues of 2×2 structure tensors, returns VectorField (dim=2, [λ1, λ2], λ1 ≥ λ2).
  170. 170pub fn TensorField::eigenvalues_2x2(
  171. 171 self : TensorField,
  172. 172) -> VectorField raise @types.LoadError {
  173. 173● if self.rows != 2 || self.cols != 2 {
  174. 174○ raise @types.LoadError::DecodeFailed(
  175. 175 "eigenvalues_2x2: 张量必须是 2×2",
  176. 176 )
  177. 177 }
  178. 178 let n = self.width * self.height
  179. 179● let data = Array::make(n * 2, 0.0F)
  180. 180 for i in 0..<n {
  181. 181● let a = self.data[i * 4]
  182. 182 let b = self.data[i * 4 + 1]
  183. 183 let c = self.data[i * 4 + 2]
  184. 184 let d = self.data[i * 4 + 3]
  185. 185 let trace = a + d
  186. 186 let det = a * d - b * c
  187. 187 let disc = trace * trace - 4.0F * det
  188. 188◐ let sq = if disc >= 0.0F { disc.sqrt() } else { 0.0F }
  189. 189 let l1 = (trace + sq) * 0.5F
  190. 190 let l2 = (trace - sq) * 0.5F
  191. 191 data[i * 2] = l1
  192. 192 data[i * 2 + 1] = l2
  193. 193 }
  194. 194● VectorField::make(self.width, self.height, 2, data)
  195. 195}