image.photography\fusion\image_fusion.mbt

77.2% · 78 / 101 points · 259 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/// # 图像融合 fusion/image_fusion
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 图像融合(v12.6.0)
  7. 7// 多焦点融合、曝光融合、Alpha 融合、拉普拉斯金字塔融合
  8. 8
  9. 9///|
  10. 10/// 图像融合配置
  11. 11#derive(Eq, @debug.Debug)
  12. 12pub(all) struct FusionConfig {
  13. 13 method_type : String // "alpha" / "multifocus" / "exposure" / "laplacian"
  14. 14 alpha : Double // Alpha 融合的权重(0-1)
  15. 15 pyramid_levels : Int32 // 拉普拉斯金字塔层数
  16. 16}
  17. 17
  18. 18///|
  19. 19pub extend FusionConfig with Eq::{not_equal, equal}
  20. 20
  21. 21///|
  22. 22pub extend FusionConfig with @debug.Debug::{to_repr}
  23. 23
  24. 24///|
  25. 25/// 创建默认融合配置
  26. 26pub fn fusion_default_config() -> FusionConfig {
  27. 27● FusionConfig::{ method_type: "alpha", alpha: 0.5, pyramid_levels: 3, }
  28. 28}
  29. 29
  30. 30///|
  31. 31/// Alpha 融合
  32. 32/// 线性融合 out = img1 * alpha + img2 * (1 - alpha)
  33. 33pub fn fuse_alpha(
  34. 34 img1 : @types.Image,
  35. 35 img2 : @types.Image,
  36. 36 alpha~ : Double = 0.5,
  37. 37) -> @types.Image {
  38. 38● let a = alpha
  39. 39 if img1.width != img2.width || img1.height != img2.height {
  40. 40○ return img1
  41. 41 }
  42. 42 let w = img1.width
  43. 43 let h = img1.height
  44. 44● let channels = img1.channels.min(img2.channels)
  45. 45● let out_channels = img1.channels.max(img2.channels)
  46. 46● let out_data : Array[Byte] = Array::make(w * h * out_channels, b'\x00')
  47. 47 for y in 0..<h {
  48. 48● for x in 0..<w {
  49. 49● let idx1 = (y * w + x) * img1.channels
  50. 50 let idx2 = (y * w + x) * img2.channels
  51. 51 let out_idx = (y * w + x) * out_channels
  52. 52 for c in 0..<channels {
  53. 53● let v1 = img1.data[idx1 + c].to_double()
  54. 54● let v2 = img2.data[idx2 + c].to_double()
  55. 55 let fused = v1 * a + v2 * (1.0 - a)
  56. 56● out_data[out_idx + c] = fused.min(255.0).max(0.0).to_int().to_byte()
  57. 57 }
  58. 58 // 复制额外通道
  59. 59 for c in channels..<out_channels {
  60. 60○ if c < img1.channels {
  61. 61○ out_data[out_idx + c] = img1.data[idx1 + c]
  62. 62○ } else if c < img2.channels {
  63. 63○ out_data[out_idx + c] = img2.data[idx2 + c]
  64. 64 }
  65. 65 }
  66. 66 }
  67. 67 }
  68. 68 @types.Image::{
  69. 69 width: w,
  70. 70 height: h,
  71. 71 channels: out_channels,
  72. 72● data: Bytes::from_array(out_data),
  73. 73 }
  74. 74}
  75. 75
  76. 76///|
  77. 77/// 计算图像的清晰度(拉普拉斯方差)
  78. 78fn compute_sharpness(img : @types.Image, x : Int32, y : Int32) -> Double {
  79. 79● let w = img.width
  80. 80 let h = img.height
  81. 81 let channels = img.channels
  82. 82 if x <= 0 || x >= w - 1 || y <= 0 || y >= h - 1 {
  83. 83● return 0.0
  84. 84 }
  85. 85 let _ = (y * w + x) * channels
  86. 86● let gray = (px : Int32, py : Int32) => {
  87. 87 let i = (py * w + px) * channels
  88. 88● let r = img.data[i].to_double()
  89. 89◐ let g = if channels >= 2 { img.data[i + 1].to_double() } else { r }
  90. 90◐ let b = if channels >= 3 { img.data[i + 2].to_double() } else { r }
  91. 91 0.299 * r + 0.587 * g + 0.114 * b
  92. 92 }
  93. 93 // 拉普拉斯算子
  94. 94● let laplacian = 4.0 * gray(x, y) -
  95. 95● gray(x - 1, y) -
  96. 96● gray(x + 1, y) -
  97. 97● gray(x, y - 1) -
  98. 98● gray(x, y + 1)
  99. 99 laplacian * laplacian
  100. 100}
  101. 101
  102. 102///|
  103. 103/// 多焦点融合
  104. 104/// 基于清晰度的像素级融合,选择更清晰的像素
  105. 105pub fn fuse_multifocus(
  106. 106 img1 : @types.Image,
  107. 107 img2 : @types.Image,
  108. 108) -> @types.Image {
  109. 109● if img1.width != img2.width || img1.height != img2.height {
  110. 110○ return img1
  111. 111 }
  112. 112 let w = img1.width
  113. 113 let h = img1.height
  114. 114● let channels = img1.channels.min(img2.channels)
  115. 115● let out_channels = img1.channels.max(img2.channels)
  116. 116● let out_data : Array[Byte] = Array::make(w * h * out_channels, b'\x00')
  117. 117 // 计算清晰度图
  118. 118● let sharpness1 : Array[Double] = Array::make(w * h, 0.0)
  119. 119● let sharpness2 : Array[Double] = Array::make(w * h, 0.0)
  120. 120 for y in 0..<h {
  121. 121● for x in 0..<w {
  122. 122● sharpness1[y * w + x] = compute_sharpness(img1, x, y)
  123. 123● sharpness2[y * w + x] = compute_sharpness(img2, x, y)
  124. 124 }
  125. 125 }
  126. 126 // 像素级融合
  127. 127 for y in 0..<h {
  128. 128● for x in 0..<w {
  129. 129● let idx1 = (y * w + x) * img1.channels
  130. 130 let idx2 = (y * w + x) * img2.channels
  131. 131 let out_idx = (y * w + x) * out_channels
  132. 132 let use_img1 = sharpness1[y * w + x] >= sharpness2[y * w + x]
  133. 133 for c in 0..<channels {
  134. 134● if use_img1 {
  135. 135● out_data[out_idx + c] = img1.data[idx1 + c]
  136. 136 } else {
  137. 137● out_data[out_idx + c] = img2.data[idx2 + c]
  138. 138 }
  139. 139 }
  140. 140 // 复制额外通道
  141. 141 for c in channels..<out_channels {
  142. 142○ if c < img1.channels {
  143. 143○ out_data[out_idx + c] = img1.data[idx1 + c]
  144. 144○ } else if c < img2.channels {
  145. 145○ out_data[out_idx + c] = img2.data[idx2 + c]
  146. 146 }
  147. 147 }
  148. 148 }
  149. 149 }
  150. 150 @types.Image::{
  151. 151 width: w,
  152. 152 height: h,
  153. 153 channels: out_channels,
  154. 154● data: Bytes::from_array(out_data),
  155. 155 }
  156. 156}
  157. 157
  158. 158///|
  159. 159/// 曝光融合
  160. 160/// 基于曝光质量的加权融合
  161. 161pub fn fuse_exposure(images : Array[@types.Image]) -> @types.Image {
  162. 162● if images.length() == 0 {
  163. 163○ return @types.Image::{
  164. 164 width: 1,
  165. 165 height: 1,
  166. 166 channels: 1,
  167. 167○ data: Bytes::from_array([b'\x00']),
  168. 168 }
  169. 169 }
  170. 170● if images.length() == 1 {
  171. 171○ return images[0]
  172. 172 }
  173. 173 let w = images[0].width
  174. 174 let h = images[0].height
  175. 175 let channels = images[0].channels
  176. 176● let out_data : Array[Byte] = Array::make(w * h * channels, b'\x00')
  177. 177 // 计算每张图像的权重(基于对比度、饱和度、曝光度)
  178. 178 let weights : Array[Array[Double]] = []
  179. 179 for img in images {
  180. 180● let weight_map : Array[Double] = Array::make(w * h, 0.0)
  181. 181 for y in 0..<h {
  182. 182● for x in 0..<w {
  183. 183● let idx = (y * w + x) * img.channels
  184. 184● let r = img.data[idx].to_double()
  185. 185 let g = if img.channels >= 2 {
  186. 186○ img.data[idx + 1].to_double()
  187. 187 } else {
  188. 188● r
  189. 189 }
  190. 190 let b = if img.channels >= 3 {
  191. 191○ img.data[idx + 2].to_double()
  192. 192 } else {
  193. 193● r
  194. 194 }
  195. 195 let luminance = 0.299 * r + 0.587 * g + 0.114 * b
  196. 196 // 曝光度权重(高斯,中心在 128)
  197. 197● let exposure_weight = @math.exp(
  198. 198 -(luminance - 128.0) * (luminance - 128.0) / (2.0 * 64.0 * 64.0),
  199. 199 )
  200. 200 // 对比度权重(拉普拉斯)
  201. 201● let contrast_weight = compute_sharpness(img, x, y) + 1.0
  202. 202 weight_map[y * w + x] = exposure_weight * contrast_weight
  203. 203 }
  204. 204 }
  205. 205● weights.push(weight_map)
  206. 206 }
  207. 207 // 归一化权重并融合
  208. 208 for y in 0..<h {
  209. 209● for x in 0..<w {
  210. 210● let mut weight_sum = 0.0
  211. 211● for i in 0..<weights.length() {
  212. 212● weight_sum += weights[i][y * w + x]
  213. 213 }
  214. 214 let out_idx = (y * w + x) * channels
  215. 215 for c in 0..<channels {
  216. 216● if weight_sum > 0.0 {
  217. 217● let mut fused = 0.0
  218. 218● for i in 0..<images.length() {
  219. 219● let idx = (y * w + x) * images[i].channels
  220. 220 if c < images[i].channels {
  221. 221● fused += images[i].data[idx + c].to_double() *
  222. 222 weights[i][y * w + x] /
  223. 223 weight_sum
  224. 224 }
  225. 225 }
  226. 226● out_data[out_idx + c] = fused.min(255.0).max(0.0).to_int().to_byte()
  227. 227 } else {
  228. 228○ out_data[out_idx + c] = images[0].data[(y * w + x) *
  229. 229 images[0].channels +
  230. 230 c]
  231. 231 }
  232. 232 }
  233. 233 }
  234. 234 }
  235. 235 @types.Image::{
  236. 236 width: w,
  237. 237 height: h,
  238. 238 channels,
  239. 239● data: Bytes::from_array(out_data),
  240. 240 }
  241. 241}
  242. 242
  243. 243///|
  244. 244/// 通用图像融合接口
  245. 245pub fn fuse_images(
  246. 246 img1 : @types.Image,
  247. 247 img2 : @types.Image,
  248. 248 config? : FusionConfig,
  249. 249) -> @types.Image {
  250. 250● let cfg = match config {
  251. 251● Some(c) => c
  252. 252● None => fusion_default_config()
  253. 253 }
  254. 254● match cfg.method_type {
  255. 255● "alpha" => fuse_alpha(img1, img2, alpha=cfg.alpha)
  256. 256● "multifocus" => fuse_multifocus(img1, img2)
  257. 257○ _ => fuse_alpha(img1, img2, alpha=cfg.alpha)
  258. 258 }
  259. 259}