image.domain\registration\image_registration.mbt

90.4% · 85 / 94 points · 329 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/// # 图像配准 registration/image_registration
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 图像配准(v12.7.0)
  7. 7// 基于相位相关的平移估计 + 仿射变换 + 图像对齐
  8. 8
  9. 9///|
  10. 10/// 配准结果
  11. 11pub(all) struct RegistrationResult {
  12. 12 dx : Double // x 方向平移
  13. 13 dy : Double // y 方向平移
  14. 14 rotation : Double // 旋转角度(弧度)
  15. 15 scale : Double // 缩放因子
  16. 16 confidence : Double // 配准置信度
  17. 17}
  18. 18
  19. 19///|
  20. 20/// 仿射变换矩阵
  21. 21pub(all) struct AffineTransform {
  22. 22 a : Double // 缩放+旋转
  23. 23 b : Double // 剪切+旋转
  24. 24 c : Double // x 平移
  25. 25 d : Double // 剪切+旋转
  26. 26 e : Double // 缩放+旋转
  27. 27 f : Double // y 平移
  28. 28}
  29. 29
  30. 30///|
  31. 31/// 创建恒等变换
  32. 32pub fn affine_identity() -> AffineTransform {
  33. 33● AffineTransform::{ a: 1.0, b: 0.0, c: 0.0, d: 0.0, e: 1.0, f: 0.0, }
  34. 34}
  35. 35
  36. 36///|
  37. 37/// 创建平移变换
  38. 38pub fn affine_translation(dx : Double, dy : Double) -> AffineTransform {
  39. 39● AffineTransform::{ a: 1.0, b: 0.0, c: dx, d: 0.0, e: 1.0, f: dy, }
  40. 40}
  41. 41
  42. 42///|
  43. 43/// 创建旋转变换(围绕原点)
  44. 44pub fn affine_rotation(angle : Double) -> AffineTransform {
  45. 45● let cos_a = @math.cos(angle)
  46. 46● let sin_a = @math.sin(angle)
  47. 47 AffineTransform::{ a: cos_a, b: -sin_a, c: 0.0, d: sin_a, e: cos_a, f: 0.0, }
  48. 48}
  49. 49
  50. 50///|
  51. 51/// 创建缩放变换
  52. 52pub fn affine_scale(sx : Double, sy : Double) -> AffineTransform {
  53. 53● AffineTransform::{ a: sx, b: 0.0, c: 0.0, d: 0.0, e: sy, f: 0.0, }
  54. 54}
  55. 55
  56. 56///|
  57. 57/// 组合两个仿射变换(先应用 t1,再应用 t2)
  58. 58pub fn affine_compose(
  59. 59 t1 : AffineTransform,
  60. 60 t2 : AffineTransform,
  61. 61) -> AffineTransform {
  62. 62● AffineTransform::{
  63. 63 a: t2.a * t1.a + t2.b * t1.d,
  64. 64 b: t2.a * t1.b + t2.b * t1.e,
  65. 65 c: t2.a * t1.c + t2.b * t1.f + t2.c,
  66. 66 d: t2.d * t1.a + t2.e * t1.d,
  67. 67 e: t2.d * t1.b + t2.e * t1.e,
  68. 68 f: t2.d * t1.c + t2.e * t1.f + t2.f,
  69. 69 }
  70. 70}
  71. 71
  72. 72///|
  73. 73/// 应用仿射变换到点
  74. 74pub fn affine_apply(
  75. 75 t : AffineTransform,
  76. 76 x : Double,
  77. 77 y : Double,
  78. 78) -> (Double, Double) {
  79. 79● let nx = t.a * x + t.b * y + t.c
  80. 80 let ny = t.d * x + t.e * y + t.f
  81. 81 (nx, ny)
  82. 82}
  83. 83
  84. 84///|
  85. 85/// 计算图像的灰度图
  86. 86fn to_gray(img : @types.Image) -> Array[Double] {
  87. 87● let w = img.width
  88. 88 let h = img.height
  89. 89 let channels = img.channels
  90. 90● let gray : Array[Double] = Array::make(w * h, 0.0)
  91. 91 for i in 0..<(w * h) {
  92. 92● let idx = i * channels
  93. 93● let r = img.data[idx].to_double()
  94. 94◐ let g = if channels >= 2 { img.data[idx + 1].to_double() } else { r }
  95. 95◐ let b = if channels >= 3 { img.data[idx + 2].to_double() } else { r }
  96. 96 gray[i] = 0.299 * r + 0.587 * g + 0.114 * b
  97. 97 }
  98. 98 gray
  99. 99}
  100. 100
  101. 101///|
  102. 102/// 基于相位相关的平移估计(简化版,使用空间域互相关)
  103. 103pub fn estimate_translation(
  104. 104 img1 : @types.Image,
  105. 105 img2 : @types.Image,
  106. 106 max_offset~ : Int32 = 20,
  107. 107) -> RegistrationResult raise @types.LoadError {
  108. 108 // 输入验证
  109. 109● if img1.width <= 0 || img1.height <= 0 {
  110. 110● raise @types.LoadError::DecodeFailed(
  111. 111 "estimate_translation: 图像尺寸必须大于0",
  112. 112 )
  113. 113 }
  114. 114 if img1.channels < 1 || img1.channels > 4 {
  115. 115● raise @types.LoadError::DecodeFailed(
  116. 116 "estimate_translation: 通道数必须在1-4之间",
  117. 117 )
  118. 118 }
  119. 119 let expected_size = img1.width * img1.height * img1.channels
  120. 120● if img1.data.length() != expected_size {
  121. 121● raise @types.LoadError::DecodeFailed(
  122. 122 "estimate_translation: 数据大小不匹配",
  123. 123 )
  124. 124 }
  125. 125 if img2.width <= 0 || img2.height <= 0 {
  126. 126● raise @types.LoadError::DecodeFailed(
  127. 127 "estimate_translation: 图像尺寸必须大于0",
  128. 128 )
  129. 129 }
  130. 130 if img2.channels < 1 || img2.channels > 4 {
  131. 131● raise @types.LoadError::DecodeFailed(
  132. 132 "estimate_translation: 通道数必须在1-4之间",
  133. 133 )
  134. 134 }
  135. 135 let expected_size2 = img2.width * img2.height * img2.channels
  136. 136● if img2.data.length() != expected_size2 {
  137. 137● raise @types.LoadError::DecodeFailed(
  138. 138 "estimate_translation: 数据大小不匹配",
  139. 139 )
  140. 140 }
  141. 141 let max_off = max_offset
  142. 142 if img1.width != img2.width || img1.height != img2.height {
  143. 143○ return RegistrationResult::{
  144. 144 dx: 0.0,
  145. 145 dy: 0.0,
  146. 146 rotation: 0.0,
  147. 147 scale: 1.0,
  148. 148 confidence: 0.0,
  149. 149 }
  150. 150 }
  151. 151 let w = img1.width
  152. 152 let h = img1.height
  153. 153● let gray1 = to_gray(img1)
  154. 154● let gray2 = to_gray(img2)
  155. 155 // 计算均值
  156. 156 let mut mean1 = 0.0
  157. 157 let mut mean2 = 0.0
  158. 158 for i in 0..<(w * h) {
  159. 159● mean1 += gray1[i]
  160. 160 mean2 += gray2[i]
  161. 161 }
  162. 162● mean1 = mean1 / (w * h).to_double()
  163. 163● mean2 = mean2 / (w * h).to_double()
  164. 164 // 搜索最佳平移
  165. 165 let mut best_dx = 0
  166. 166 let mut best_dy = 0
  167. 167 let mut best_corr = -10000000000.0
  168. 168 for dy in -max_off..<(max_off + 1) {
  169. 169● for dx in -max_off..<(max_off + 1) {
  170. 170● let mut corr = 0.0
  171. 171 let mut var1 = 0.0
  172. 172 let mut var2 = 0.0
  173. 173 let mut count = 0
  174. 174 for y in 0..<h {
  175. 175● for x in 0..<w {
  176. 176● let x2 = x + dx
  177. 177 let y2 = y + dy
  178. 178 if x2 >= 0 && x2 < w && y2 >= 0 && y2 < h {
  179. 179● let v1 = gray1[y * w + x] - mean1
  180. 180 let v2 = gray2[y2 * w + x2] - mean2
  181. 181 corr += v1 * v2
  182. 182 var1 += v1 * v1
  183. 183 var2 += v2 * v2
  184. 184 count += 1
  185. 185 }
  186. 186 }
  187. 187 }
  188. 188 if count > 0 && var1 > 0.0 && var2 > 0.0 {
  189. 189● let normalized_corr = corr / (var1.sqrt() * var2.sqrt())
  190. 190 if normalized_corr > best_corr {
  191. 191● best_corr = normalized_corr
  192. 192 best_dx = dx
  193. 193 best_dy = dy
  194. 194 }
  195. 195 }
  196. 196 }
  197. 197 }
  198. 198 RegistrationResult::{
  199. 199● dx: best_dx.to_double(),
  200. 200● dy: best_dy.to_double(),
  201. 201 rotation: 0.0,
  202. 202 scale: 1.0,
  203. 203 confidence: if best_corr > 0.0 {
  204. 204● best_corr
  205. 205 } else {
  206. 206○ 0.0
  207. 207 },
  208. 208 }
  209. 209}
  210. 210
  211. 211///|
  212. 212/// 使用仿射变换对齐图像(反向映射 + 双线性插值)
  213. 213pub fn warp_affine_transform(
  214. 214 img : @types.Image,
  215. 215 transform : AffineTransform,
  216. 216 out_width~ : Int32 = 0,
  217. 217 out_height~ : Int32 = 0,
  218. 218) -> @types.Image raise @types.LoadError {
  219. 219 // 输入验证
  220. 220● if img.width <= 0 || img.height <= 0 {
  221. 221● raise @types.LoadError::DecodeFailed(
  222. 222 "warp_affine_transform: 图像尺寸必须大于0",
  223. 223 )
  224. 224 }
  225. 225 if img.channels < 1 || img.channels > 4 {
  226. 226● raise @types.LoadError::DecodeFailed(
  227. 227 "warp_affine_transform: 通道数必须在1-4之间",
  228. 228 )
  229. 229 }
  230. 230 let expected_size = img.width * img.height * img.channels
  231. 231● if img.data.length() != expected_size {
  232. 232● raise @types.LoadError::DecodeFailed(
  233. 233 "warp_affine_transform: 数据大小不匹配",
  234. 234 )
  235. 235 }
  236. 236◐ let ow = if out_width == 0 { img.width } else { out_width }
  237. 237◐ let oh = if out_height == 0 { img.height } else { out_height }
  238. 238 let w = img.width
  239. 239 let h = img.height
  240. 240 let channels = img.channels
  241. 241● let out_data : Array[Byte] = Array::make(ow * oh * channels, b'\x00')
  242. 242 // 计算逆变换
  243. 243 let det = transform.a * transform.e - transform.b * transform.d
  244. 244● if det.abs() < 0.000001 {
  245. 245○ return img
  246. 246 }
  247. 247 let inv_a = transform.e / det
  248. 248 let inv_b = -transform.b / det
  249. 249 let inv_c = (transform.b * transform.f - transform.e * transform.c) / det
  250. 250 let inv_d = -transform.d / det
  251. 251 let inv_e = transform.a / det
  252. 252 let inv_f = (transform.d * transform.c - transform.a * transform.f) / det
  253. 253 for y in 0..<oh {
  254. 254● for x in 0..<ow {
  255. 255 // 反向映射
  256. 256● let src_x = inv_a * x.to_double() + inv_b * y.to_double() + inv_c
  257. 257● let src_y = inv_d * x.to_double() + inv_e * y.to_double() + inv_f
  258. 258 // 双线性插值
  259. 259● let x0 = src_x.floor().to_int()
  260. 260● let y0 = src_y.floor().to_int()
  261. 261 let x1 = x0 + 1
  262. 262 let y1 = y0 + 1
  263. 263● let fx = src_x - x0.to_double()
  264. 264● let fy = src_y - y0.to_double()
  265. 265 let out_idx = (y * ow + x) * channels
  266. 266 for c in 0..<channels {
  267. 267● if x0 >= 0 && x1 < w && y0 >= 0 && y1 < h {
  268. 268● let v00 = img.data[(y0 * w + x0) * channels + c].to_double()
  269. 269● let v01 = img.data[(y0 * w + x1) * channels + c].to_double()
  270. 270● let v10 = img.data[(y1 * w + x0) * channels + c].to_double()
  271. 271● let v11 = img.data[(y1 * w + x1) * channels + c].to_double()
  272. 272 let top = v00 * (1.0 - fx) + v01 * fx
  273. 273 let bottom = v10 * (1.0 - fx) + v11 * fx
  274. 274 let value = top * (1.0 - fy) + bottom * fy
  275. 275● out_data[out_idx + c] = value.min(255.0).max(0.0).to_int().to_byte()
  276. 276 } else {
  277. 277● out_data[out_idx + c] = b'\x00'
  278. 278 }
  279. 279 }
  280. 280 }
  281. 281 }
  282. 282 @types.Image::{
  283. 283 width: ow,
  284. 284 height: oh,
  285. 285 channels,
  286. 286● data: Bytes::from_array(out_data),
  287. 287 }
  288. 288}
  289. 289
  290. 290///|
  291. 291/// 图像对齐(将 img2 对齐到 img1)
  292. 292pub fn align_images(
  293. 293 img1 : @types.Image,
  294. 294 img2 : @types.Image,
  295. 295) -> (@types.Image, RegistrationResult) raise @types.LoadError {
  296. 296 // 输入验证
  297. 297● if img1.width <= 0 || img1.height <= 0 {
  298. 298● raise @types.LoadError::DecodeFailed(
  299. 299 "align_images: 图像尺寸必须大于0",
  300. 300 )
  301. 301 }
  302. 302 if img1.channels < 1 || img1.channels > 4 {
  303. 303● raise @types.LoadError::DecodeFailed(
  304. 304 "align_images: 通道数必须在1-4之间",
  305. 305 )
  306. 306 }
  307. 307 let expected_size = img1.width * img1.height * img1.channels
  308. 308● if img1.data.length() != expected_size {
  309. 309● raise @types.LoadError::DecodeFailed("align_images: 数据大小不匹配")
  310. 310 }
  311. 311 if img2.width <= 0 || img2.height <= 0 {
  312. 312● raise @types.LoadError::DecodeFailed(
  313. 313 "align_images: 图像尺寸必须大于0",
  314. 314 )
  315. 315 }
  316. 316 if img2.channels < 1 || img2.channels > 4 {
  317. 317● raise @types.LoadError::DecodeFailed(
  318. 318 "align_images: 通道数必须在1-4之间",
  319. 319 )
  320. 320 }
  321. 321 let expected_size2 = img2.width * img2.height * img2.channels
  322. 322● if img2.data.length() != expected_size2 {
  323. 323● raise @types.LoadError::DecodeFailed("align_images: 数据大小不匹配")
  324. 324 }
  325. 325● let result = estimate_translation(img1, img2)
  326. 326● let transform = affine_translation(-result.dx, -result.dy)
  327. 327● let aligned = warp_affine_transform(img2, transform)
  328. 328 (aligned, result)
  329. 329}