image.process\transform\perspective.mbt

93.3% · 56 / 60 points · 189 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/// # 透视变换 perspective
  3. 3///
  4. 4/// 图像处理算法模块
  5. 5/// 图像处理算法模块 Image processing algorithm module
  6. 6// 透视变换(纯 MoonBit):get_perspective_transform + warp_perspective
  7. 7
  8. 8///|
  9. 9/// 3×3 透视变换矩阵 (m00, m01, m02, m10, m11, m12, m20, m21, m22)
  10. 10pub(all) struct PerspectiveMatrix {
  11. 11 m00 : Float
  12. 12 m01 : Float
  13. 13 m02 : Float
  14. 14 m10 : Float
  15. 15 m11 : Float
  16. 16 m12 : Float
  17. 17 m20 : Float
  18. 18 m21 : Float
  19. 19 m22 : Float
  20. 20}
  21. 21
  22. 22///|
  23. 23/// 高斯消元法求解线性方程组 Ax=b,A 为 n×n 矩阵,b 为 n 维向量
  24. 24fn solve_linear_system(
  25. 25 a : Array[Array[Float]],
  26. 26 b : Array[Float],
  27. 27 n : Int32,
  28. 28) -> Array[Float]? {
  29. 29● let mat : Array[Array[Float]] = Array::make(n, [])
  30. 30 for i in 0..<n {
  31. 31● mat[i] = Array::make(n + 1, 0.0F)
  32. 32 for j in 0..<n {
  33. 33● mat[i][j] = a[i][j]
  34. 34 }
  35. 35 mat[i][n] = b[i]
  36. 36 }
  37. 37 // 前向消元
  38. 38 for col in 0..<n {
  39. 39 // 选主元
  40. 40● let mut pivot = col
  41. 41 for row in (col + 1)..<n {
  42. 42● if mat[row][col].abs() > mat[pivot][col].abs() {
  43. 43● pivot = row
  44. 44 }
  45. 45 }
  46. 46● if mat[pivot][col].abs() < 0.0000000001F {
  47. 47○ return None
  48. 48 }
  49. 49 if pivot != col {
  50. 50● let tmp = mat[pivot]
  51. 51 mat[pivot] = mat[col]
  52. 52 mat[col] = tmp
  53. 53 }
  54. 54 // 消元
  55. 55 for row in (col + 1)..<n {
  56. 56● let factor = mat[row][col] / mat[col][col]
  57. 57 for j in col..<=n {
  58. 58● mat[row][j] -= factor * mat[col][j]
  59. 59 }
  60. 60 }
  61. 61 }
  62. 62 // 回代
  63. 63● let x : Array[Float] = Array::make(n, 0.0F)
  64. 64 for k in 0..<n {
  65. 65● let i = n - 1 - k
  66. 66 let mut sum = mat[i][n]
  67. 67 for j in (i + 1)..<n {
  68. 68● sum -= mat[i][j] * x[j]
  69. 69 }
  70. 70 x[i] = sum / mat[i][i]
  71. 71 }
  72. 72 Some(x)
  73. 73}
  74. 74
  75. 75///|
  76. 76/// 从 4 对源点→目标点计算 3×3 透视变换矩阵。
  77. 77/// src 和 dst 均为 4 个 (Float, Float) 点。
  78. 78pub fn get_perspective_transform(
  79. 79 src : Array[(Float, Float)],
  80. 80 dst : Array[(Float, Float)],
  81. 81) -> PerspectiveMatrix? {
  82. 82● if src.length() != 4 || dst.length() != 4 {
  83. 83○ return None
  84. 84 }
  85. 85 // 构建 8×8 线性方程组
  86. 86● let a : Array[Array[Float]] = Array::make(8, [])
  87. 87● let b : Array[Float] = Array::make(8, 0.0F)
  88. 88 for i in 0..<4 {
  89. 89● let (sx, sy) = src[i]
  90. 90 let (dx, dy) = dst[i]
  91. 91 // 方程 1: m00*sx + m01*sy + m02 - m20*sx*dx - m21*sy*dx = dx
  92. 92 a[i * 2] = [sx, sy, 1.0F, 0.0F, 0.0F, 0.0F, -sx * dx, -sy * dx]
  93. 93 b[i * 2] = dx
  94. 94 // 方程 2: m10*sx + m11*sy + m12 - m20*sx*dy - m21*sy*dy = dy
  95. 95 a[i * 2 + 1] = [0.0F, 0.0F, 0.0F, sx, sy, 1.0F, -sx * dy, -sy * dy]
  96. 96 b[i * 2 + 1] = dy
  97. 97 }
  98. 98● let sol = solve_linear_system(a, b, 8)
  99. 99● match sol {
  100. 100○ None => None
  101. 101 Some(v) =>
  102. 102● Some(PerspectiveMatrix::{
  103. 103 m00: v[0],
  104. 104 m01: v[1],
  105. 105 m02: v[2],
  106. 106 m10: v[3],
  107. 107 m11: v[4],
  108. 108 m12: v[5],
  109. 109 m20: v[6],
  110. 110 m21: v[7],
  111. 111 m22: 1.0F,
  112. 112 })
  113. 113 }
  114. 114}
  115. 115
  116. 116///|
  117. 117/// 透视变换。使用 3×3 矩阵将图像变换到 new_w × new_h。
  118. 118/// 采用 dst→src 映射 + 双线性插值。
  119. 119pub fn warp_perspective(
  120. 120 img : @types.Image,
  121. 121 matrix : PerspectiveMatrix,
  122. 122 new_w : Int32,
  123. 123 new_h : Int32,
  124. 124) -> @types.Image {
  125. 125● let ch = img.channels
  126. 126 let w = img.width
  127. 127 let h = img.height
  128. 128● let data_int : Array[Int32] = Array::make(w * h * ch, 0)
  129. 129 for i in 0..<(w * h * ch) {
  130. 130● data_int[i] = img.data[i].to_int()
  131. 131 }
  132. 132● let out : Array[Byte] = Array::make(new_w * new_h * ch, b'\x00')
  133. 133 for dy in 0..<new_h {
  134. 134● let fdx = 0.0F
  135. 135● let fdy = Float::from_int(dy)
  136. 136 for dx in 0..<new_w {
  137. 137● let fdx2 = if dx == 0 { fdx } else { Float::from_int(dx) }
  138. 138 // dst→src: 求逆映射。这里假设 matrix 是 src→dst,需要求逆。
  139. 139 // 简化:直接用 matrix 做 dst→src 映射(前向映射的逆)
  140. 140 // src_x = (m00*dx + m01*dy + m02) / (m20*dx + m21*dy + m22)
  141. 141 // src_y = (m10*dx + m11*dy + m12) / (m20*dx + m21*dy + m22)
  142. 142 let denom = matrix.m20 * fdx2 + matrix.m21 * fdy + matrix.m22
  143. 143● if denom.abs() < 0.0000000001F {
  144. 144○ continue
  145. 145 }
  146. 146 let sx = (matrix.m00 * fdx2 + matrix.m01 * fdy + matrix.m02) / denom
  147. 147 let sy = (matrix.m10 * fdx2 + matrix.m11 * fdy + matrix.m12) / denom
  148. 148 for ci in 0..<ch {
  149. 149● let val = persp_sample_bilinear(data_int, w, h, ch, ci, sx, sy)
  150. 150● out[(dy * new_w + dx) * ch + ci] = val.to_byte()
  151. 151 }
  152. 152 }
  153. 153 }
  154. 154 @types.Image::{
  155. 155 width: new_w,
  156. 156 height: new_h,
  157. 157 channels: ch,
  158. 158● data: Bytes::from_array(out),
  159. 159 }
  160. 160}
  161. 161
  162. 162///|
  163. 163fn persp_sample_bilinear(
  164. 164 data : Array[Int32],
  165. 165 w : Int32,
  166. 166 h : Int32,
  167. 167 ch : Int32,
  168. 168 c : Int32,
  169. 169 x : Float,
  170. 170 y : Float,
  171. 171) -> Int32 {
  172. 172● let x0 = x.to_int()
  173. 173● let y0 = y.to_int()
  174. 174 let w1 = w - 1
  175. 175 let h1 = h - 1
  176. 176● let x0c = x0.clamp(min=0, max=w1)
  177. 177● let y0c = y0.clamp(min=0, max=h1)
  178. 178● let x1 = (x0 + 1).clamp(min=0, max=w1)
  179. 179● let y1 = (y0 + 1).clamp(min=0, max=h1)
  180. 180● let fx = x - Float::from_int(x0)
  181. 181● let fy = y - Float::from_int(y0)
  182. 182 let p00 = data[(y0c * w + x0c) * ch + c]
  183. 183 let p10 = data[(y0c * w + x1) * ch + c]
  184. 184 let p01 = data[(y1 * w + x0c) * ch + c]
  185. 185 let p11 = data[(y1 * w + x1) * ch + c]
  186. 186● let top = Float::from_int(p00) * (1.0F - fx) + Float::from_int(p10) * fx
  187. 187● let bot = Float::from_int(p01) * (1.0F - fx) + Float::from_int(p11) * fx
  188. 188● (top * (1.0F - fy) + bot * fy + 0.5F).to_int().clamp(min=0, max=255)
  189. 189}