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