image.process\feature\image_quality.mbt

100% · 107 / 107 points · 340 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/// # 图像质量 image quality
  3. 3///
  4. 4/// 图像处理算法模块
  5. 5/// 图像处理算法模块 Image processing algorithm module
  6. 6// 图像质量评估(纯 MoonBit):mse / psnr / ssim
  7. 7
  8. 8///|
  9. 9/// 均方误差(MSE)。计算两幅图像对应像素的均方误差。
  10. 10/// 两幅图像必须具有相同的尺寸和通道数。
  11. 11/// 返回 Double 类型的 MSE 值。
  12. 12pub fn mse(
  13. 13 img1 : @types.Image,
  14. 14 img2 : @types.Image,
  15. 15) -> Double raise @types.LoadError {
  16. 16 // 输入验证
  17. 17● if img1.width <= 0 || img1.height <= 0 {
  18. 18● raise @types.LoadError::DecodeFailed("mse: 图像1尺寸必须大于0")
  19. 19 }
  20. 20 if img2.width <= 0 || img2.height <= 0 {
  21. 21● raise @types.LoadError::DecodeFailed("mse: 图像2尺寸必须大于0")
  22. 22 }
  23. 23 if img1.channels < 1 || img1.channels > 4 {
  24. 24● raise @types.LoadError::DecodeFailed(
  25. 25 "mse: 图像1通道数必须在1-4之间",
  26. 26 )
  27. 27 }
  28. 28 if img2.channels < 1 || img2.channels > 4 {
  29. 29● raise @types.LoadError::DecodeFailed(
  30. 30 "mse: 图像2通道数必须在1-4之间",
  31. 31 )
  32. 32 }
  33. 33 if img1.width != img2.width || img1.height != img2.height {
  34. 34● raise @types.LoadError::DecodeFailed("mse: 两幅图像尺寸必须相同")
  35. 35 }
  36. 36 if img1.channels != img2.channels {
  37. 37● raise @types.LoadError::DecodeFailed(
  38. 38 "mse: 两幅图像通道数必须相同",
  39. 39 )
  40. 40 }
  41. 41 let expected_size = img1.width * img1.height * img1.channels
  42. 42● if img1.data.length() != expected_size {
  43. 43● raise @types.LoadError::DecodeFailed(
  44. 44 "mse: 图像1数据大小不匹配,期望 " +
  45. 45● expected_size.to_string() +
  46. 46 ",实际 " +
  47. 47● img1.data.length().to_string(),
  48. 48 )
  49. 49 }
  50. 50● if img2.data.length() != expected_size {
  51. 51● raise @types.LoadError::DecodeFailed(
  52. 52 "mse: 图像2数据大小不匹配,期望 " +
  53. 53● expected_size.to_string() +
  54. 54 ",实际 " +
  55. 55● img2.data.length().to_string(),
  56. 56 )
  57. 57 }
  58. 58
  59. 59● let n = img1.data.length()
  60. 60 let mut sum : Int32 = 0
  61. 61 for i in 0..<n {
  62. 62● let d = img1.data[i].to_int() - img2.data[i].to_int()
  63. 63 sum += d * d
  64. 64 }
  65. 65● sum.to_double() / n.to_double()
  66. 66}
  67. 67
  68. 68///|
  69. 69/// 峰值信噪比(PSNR)。基于 MSE 计算 PSNR(单位 dB)。
  70. 70/// 若两幅图像完全相同(MSE=0),返回 Infinity。
  71. 71/// 两幅图像必须具有相同的尺寸和通道数。
  72. 72pub fn psnr(
  73. 73 img1 : @types.Image,
  74. 74 img2 : @types.Image,
  75. 75) -> Double raise @types.LoadError {
  76. 76 // 输入验证
  77. 77● if img1.width <= 0 || img1.height <= 0 {
  78. 78● raise @types.LoadError::DecodeFailed("psnr: 图像1尺寸必须大于0")
  79. 79 }
  80. 80 if img2.width <= 0 || img2.height <= 0 {
  81. 81● raise @types.LoadError::DecodeFailed("psnr: 图像2尺寸必须大于0")
  82. 82 }
  83. 83 if img1.channels < 1 || img1.channels > 4 {
  84. 84● raise @types.LoadError::DecodeFailed(
  85. 85 "psnr: 图像1通道数必须在1-4之间",
  86. 86 )
  87. 87 }
  88. 88 if img2.channels < 1 || img2.channels > 4 {
  89. 89● raise @types.LoadError::DecodeFailed(
  90. 90 "psnr: 图像2通道数必须在1-4之间",
  91. 91 )
  92. 92 }
  93. 93 if img1.width != img2.width || img1.height != img2.height {
  94. 94● raise @types.LoadError::DecodeFailed("psnr: 两幅图像尺寸必须相同")
  95. 95 }
  96. 96 if img1.channels != img2.channels {
  97. 97● raise @types.LoadError::DecodeFailed(
  98. 98 "psnr: 两幅图像通道数必须相同",
  99. 99 )
  100. 100 }
  101. 101 let expected_size = img1.width * img1.height * img1.channels
  102. 102● if img1.data.length() != expected_size {
  103. 103● raise @types.LoadError::DecodeFailed(
  104. 104 "psnr: 图像1数据大小不匹配,期望 " +
  105. 105● expected_size.to_string() +
  106. 106 ",实际 " +
  107. 107● img1.data.length().to_string(),
  108. 108 )
  109. 109 }
  110. 110● if img2.data.length() != expected_size {
  111. 111● raise @types.LoadError::DecodeFailed(
  112. 112 "psnr: 图像2数据大小不匹配,期望 " +
  113. 113● expected_size.to_string() +
  114. 114 ",实际 " +
  115. 115● img2.data.length().to_string(),
  116. 116 )
  117. 117 }
  118. 118
  119. 119● let m = mse(img1, img2)
  120. 120 if m == 0.0 {
  121. 121● return 1.0 / 0.0
  122. 122 }
  123. 123● 10.0 * @math.log10(255.0 * 255.0 / m)
  124. 124}
  125. 125
  126. 126///|
  127. 127/// 结构相似性指数(SSIM)。在 8x8 局部窗口上计算 SSIM 均值。
  128. 128/// 两幅图像必须具有相同的尺寸和通道数。
  129. 129/// 返回 Double 类型的 SSIM 值,范围.围 [-1, 1],越接近 1 越相似。
  130. 130pub fn ssim(
  131. 131 img1 : @types.Image,
  132. 132 img2 : @types.Image,
  133. 133) -> Double raise @types.LoadError {
  134. 134 // 输入验证
  135. 135● if img1.width <= 0 || img1.height <= 0 {
  136. 136● raise @types.LoadError::DecodeFailed("ssim: 图像1尺寸必须大于0")
  137. 137 }
  138. 138 if img2.width <= 0 || img2.height <= 0 {
  139. 139● raise @types.LoadError::DecodeFailed("ssim: 图像2尺寸必须大于0")
  140. 140 }
  141. 141 if img1.channels < 1 || img1.channels > 4 {
  142. 142● raise @types.LoadError::DecodeFailed(
  143. 143 "ssim: 图像1通道数必须在1-4之间",
  144. 144 )
  145. 145 }
  146. 146 if img2.channels < 1 || img2.channels > 4 {
  147. 147● raise @types.LoadError::DecodeFailed(
  148. 148 "ssim: 图像2通道数必须在1-4之间",
  149. 149 )
  150. 150 }
  151. 151 if img1.width != img2.width || img1.height != img2.height {
  152. 152● raise @types.LoadError::DecodeFailed("ssim: 两幅图像尺寸必须相同")
  153. 153 }
  154. 154 if img1.channels != img2.channels {
  155. 155● raise @types.LoadError::DecodeFailed(
  156. 156 "ssim: 两幅图像通道数必须相同",
  157. 157 )
  158. 158 }
  159. 159 let expected_size = img1.width * img1.height * img1.channels
  160. 160● if img1.data.length() != expected_size {
  161. 161● raise @types.LoadError::DecodeFailed(
  162. 162 "ssim: 图像1数据大小不匹配,期望 " +
  163. 163● expected_size.to_string() +
  164. 164 ",实际 " +
  165. 165● img1.data.length().to_string(),
  166. 166 )
  167. 167 }
  168. 168● if img2.data.length() != expected_size {
  169. 169● raise @types.LoadError::DecodeFailed(
  170. 170 "ssim: 图像2数据大小不匹配,期望 " +
  171. 171● expected_size.to_string() +
  172. 172 ",实际 " +
  173. 173● img2.data.length().to_string(),
  174. 174 )
  175. 175 }
  176. 176
  177. 177 let w = img1.width
  178. 178 let h = img1.height
  179. 179 let ch = img1.channels
  180. 180 let n = w * h
  181. 181 let c1 = 0.01 * 255.0 * (0.01 * 255.0)
  182. 182 let c2 = 0.03 * 255.0 * (0.03 * 255.0)
  183. 183 let win = 8
  184. 184 let half = win / 2
  185. 185 // 预计算通道求和/平方和/乘积和图像,用于构建积分图
  186. 186● let sum1 : Array[Double] = Array::make(n, 0.0)
  187. 187● let sum2 : Array[Double] = Array::make(n, 0.0)
  188. 188● let sq1 : Array[Double] = Array::make(n, 0.0)
  189. 189● let sq2 : Array[Double] = Array::make(n, 0.0)
  190. 190● let s12 : Array[Double] = Array::make(n, 0.0)
  191. 191 for i in 0..<n {
  192. 192● let mut a = 0.0
  193. 193 let mut b = 0.0
  194. 194 let mut aa = 0.0
  195. 195 let mut bb = 0.0
  196. 196 let mut ab = 0.0
  197. 197 for c in 0..<ch {
  198. 198● let v1 = img1.data[i * ch + c].to_int().to_double()
  199. 199● let v2 = img2.data[i * ch + c].to_int().to_double()
  200. 200 a += v1
  201. 201 b += v2
  202. 202 aa += v1 * v1
  203. 203 bb += v2 * v2
  204. 204 ab += v1 * v2
  205. 205 }
  206. 206 sum1[i] = a
  207. 207 sum2[i] = b
  208. 208 sq1[i] = aa
  209. 209 sq2[i] = bb
  210. 210 s12[i] = ab
  211. 211 }
  212. 212 // 构建积分图 (w+1)*(h+1),integral[y][x] = sum of img[0..y, 0..x]
  213. 213 let iw = w + 1
  214. 214 let ih = h + 1
  215. 215● let i_sum1 : Array[Double] = Array::make(iw * ih, 0.0)
  216. 216● let i_sum2 : Array[Double] = Array::make(iw * ih, 0.0)
  217. 217● let i_sq1 : Array[Double] = Array::make(iw * ih, 0.0)
  218. 218● let i_sq2 : Array[Double] = Array::make(iw * ih, 0.0)
  219. 219● let i_s12 : Array[Double] = Array::make(iw * ih, 0.0)
  220. 220 for y in 1..<=h {
  221. 221● let mut rs1 = 0.0
  222. 222 let mut rs2 = 0.0
  223. 223 let mut rq1 = 0.0
  224. 224 let mut rq2 = 0.0
  225. 225 let mut r12 = 0.0
  226. 226 for x in 1..<=w {
  227. 227● let idx = (y - 1) * w + (x - 1)
  228. 228 rs1 += sum1[idx]
  229. 229 rs2 += sum2[idx]
  230. 230 rq1 += sq1[idx]
  231. 231 rq2 += sq2[idx]
  232. 232 r12 += s12[idx]
  233. 233 let above = (y - 1) * iw + x
  234. 234 i_sum1[y * iw + x] = i_sum1[above] + rs1
  235. 235 i_sum2[y * iw + x] = i_sum2[above] + rs2
  236. 236 i_sq1[y * iw + x] = i_sq1[above] + rq1
  237. 237 i_sq2[y * iw + x] = i_sq2[above] + rq2
  238. 238 i_s12[y * iw + x] = i_s12[above] + r12
  239. 239 }
  240. 240 }
  241. 241 let mut total : Double = 0.0
  242. 242 let mut count = 0
  243. 243 let inner_n = win * win * ch
  244. 244● let inner_n_d = inner_n.to_double()
  245. 245 // 内部区域:用积分图 O(1) 计算窗口统计量
  246. 246 for y in half..<(h - half) {
  247. 247● for x in half..<(w - half) {
  248. 248● let x0 = x - half
  249. 249 let y0 = y - half
  250. 250 let x1 = x0 + win
  251. 251 let y1 = y0 + win
  252. 252 let s1 = i_sum1[y1 * iw + x1] -
  253. 253 i_sum1[y0 * iw + x1] -
  254. 254 i_sum1[y1 * iw + x0] +
  255. 255 i_sum1[y0 * iw + x0]
  256. 256 let s2 = i_sum2[y1 * iw + x1] -
  257. 257 i_sum2[y0 * iw + x1] -
  258. 258 i_sum2[y1 * iw + x0] +
  259. 259 i_sum2[y0 * iw + x0]
  260. 260 let sq1v = i_sq1[y1 * iw + x1] -
  261. 261 i_sq1[y0 * iw + x1] -
  262. 262 i_sq1[y1 * iw + x0] +
  263. 263 i_sq1[y0 * iw + x0]
  264. 264 let sq2v = i_sq2[y1 * iw + x1] -
  265. 265 i_sq2[y0 * iw + x1] -
  266. 266 i_sq2[y1 * iw + x0] +
  267. 267 i_sq2[y0 * iw + x0]
  268. 268 let s12v = i_s12[y1 * iw + x1] -
  269. 269 i_s12[y0 * iw + x1] -
  270. 270 i_s12[y1 * iw + x0] +
  271. 271 i_s12[y0 * iw + x0]
  272. 272 let mu1 = s1 / inner_n_d
  273. 273 let mu2 = s2 / inner_n_d
  274. 274 let var1 = sq1v / inner_n_d - mu1 * mu1
  275. 275 let var2 = sq2v / inner_n_d - mu2 * mu2
  276. 276 let cov = s12v / inner_n_d - mu1 * mu2
  277. 277 let s = (2.0 * mu1 * mu2 + c1) *
  278. 278 (2.0 * cov + c2) /
  279. 279 ((mu1 * mu1 + mu2 * mu2 + c1) * (var1 + var2 + c2))
  280. 280 total += s
  281. 281 count += 1
  282. 282 }
  283. 283 }
  284. 284 // 边界区域:预计算 Double 数组,用 O(win²) 方法
  285. 285● let src1 : Array[Double] = Array::make(n * ch, 0.0)
  286. 286● let src2 : Array[Double] = Array::make(n * ch, 0.0)
  287. 287 for i in 0..<(n * ch) {
  288. 288● src1[i] = img1.data[i].to_int().to_double()
  289. 289● src2[i] = img2.data[i].to_int().to_double()
  290. 290 }
  291. 291 for y in 0..<h {
  292. 292● let y_inner = y >= half && y < h - half
  293. 293 for x in 0..<w {
  294. 294● if y_inner && x >= half && x < w - half {
  295. 295● continue
  296. 296 }
  297. 297 let mut mu1 : Double = 0.0
  298. 298 let mut mu2 : Double = 0.0
  299. 299 let mut nn = 0
  300. 300 for dy in 0..<win {
  301. 301● for dx in 0..<win {
  302. 302● let sx = @types.clamp_int(x + dx - half, 0, w - 1)
  303. 303● let sy = @types.clamp_int(y + dy - half, 0, h - 1)
  304. 304 for c in 0..<ch {
  305. 305● mu1 += src1[(sy * w + sx) * ch + c]
  306. 306 mu2 += src2[(sy * w + sx) * ch + c]
  307. 307 nn += 1
  308. 308 }
  309. 309 }
  310. 310 }
  311. 311● mu1 /= nn.to_double()
  312. 312● mu2 /= nn.to_double()
  313. 313 let mut var1 : Double = 0.0
  314. 314 let mut var2 : Double = 0.0
  315. 315 let mut cov : Double = 0.0
  316. 316 for dy in 0..<win {
  317. 317● for dx in 0..<win {
  318. 318● let sx = @types.clamp_int(x + dx - half, 0, w - 1)
  319. 319● let sy = @types.clamp_int(y + dy - half, 0, h - 1)
  320. 320 for c in 0..<ch {
  321. 321● let v1 = src1[(sy * w + sx) * ch + c] - mu1
  322. 322 let v2 = src2[(sy * w + sx) * ch + c] - mu2
  323. 323 var1 += v1 * v1
  324. 324 var2 += v2 * v2
  325. 325 cov += v1 * v2
  326. 326 }
  327. 327 }
  328. 328 }
  329. 329● var1 /= nn.to_double()
  330. 330● var2 /= nn.to_double()
  331. 331● cov /= nn.to_double()
  332. 332 let s = (2.0 * mu1 * mu2 + c1) *
  333. 333 (2.0 * cov + c2) /
  334. 334 ((mu1 * mu1 + mu2 * mu2 + c1) * (var1 + var2 + c2))
  335. 335 total += s
  336. 336 count += 1
  337. 337 }
  338. 338 }
  339. 339● total / count.to_double()
  340. 340}