image.domain\forensics\image_source_verification.mbt
90.7% · 194 / 214 points · 942 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 图像来源验证 forensics/image_source_verification - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// 图像来源鉴定(v27.0.0) - 7
// AI 生成图像检测、物理生成(真实拍摄)图像检测、图像取证分析 - 8
- 9
///| - 10
/// 图像来源类型 - 11
/// `#non_exhaustive` 表示未来可能添加新构造器 - 12
#derive(Eq) - 13
#non_exhaustive - 14
pub(all) enum ImageSourceType { - 15
AIGenerated - 16
PhysicalCaptured - 17
Unknown - 18
Mixed - 19
} - 20
- 21
///| - 22
pub extend ImageSourceType with Eq::{not_equal, equal} - 23
- 24
///| - 25
/// 鉴定结果 - 26
pub(all) struct SourceVerificationResult { - 27
source_type : ImageSourceType - 28
ai_probability : Double - 29
physical_probability : Double - 30
confidence : Double - 31
decision_threshold : Double - 32
features : SourceVerificationFeatures - 33
processing_time_ms : Double - 34
} - 35
- 36
///| - 37
/// 来源鉴定特征 - 38
pub(all) struct SourceVerificationFeatures { - 39
noise_pattern_score : Double - 40
frequency_domain_score : Double - 41
color_distribution_score : Double - 42
texture_score : Double - 43
edge_score : Double - 44
compression_artifact_score : Double - 45
detail_score : Double - 46
metadata_score : Double - 47
overall_score : Double - 48
} - 49
- 50
///| - 51
/// 鉴定配置 - 52
pub(all) struct SourceVerificationConfig { - 53
decision_threshold : Double - 54
use_noise_analysis : Bool - 55
use_frequency_analysis : Bool - 56
use_color_analysis : Bool - 57
use_texture_analysis : Bool - 58
use_edge_analysis : Bool - 59
use_compression_analysis : Bool - 60
use_detail_analysis : Bool - 61
weights : Array[Double] - 62
} - 63
- 64
///| - 65
/// 噪声模式分析结果 - 66
pub(all) struct NoiseAnalysisResult { - 67
noise_level : Double - 68
noise_correlation : Double - 69
noise_spectrum_flatness : Double - 70
noise_pattern_regularity : Double - 71
ai_noise_indicator : Double - 72
} - 73
- 74
///| - 75
/// 频域分析结果 - 76
pub(all) struct FrequencyAnalysisResult { - 77
high_frequency_energy : Double - 78
low_frequency_energy : Double - 79
frequency_spectrum_flatness : Double - 80
spectral_centroid : Double - 81
spectral_spread : Double - 82
ai_frequency_indicator : Double - 83
} - 84
- 85
///| - 86
/// 颜色分布分析结果 - 87
pub(all) struct ColorAnalysisResult { - 88
color_histogram_smoothness : Double - 89
color_transition_naturalness : Double - 90
color_quantization_level : Double - 91
skin_tone_accuracy : Double - 92
ai_color_indicator : Double - 93
} - 94
- 95
///| - 96
/// 纹理分析结果 - 97
pub(all) struct TextureAnalysisResult { - 98
texture_regularity : Double - 99
texture_complexity : Double - 100
micro_texture_consistency : Double - 101
macro_texture_consistency : Double - 102
ai_texture_indicator : Double - 103
} - 104
- 105
///| - 106
/// 边缘分析结果 - 107
pub(all) struct EdgeAnalysisResult { - 108
edge_sharpness : Double - 109
edge_continuity : Double - 110
edge_consistency : Double - 111
edge_artifact_level : Double - 112
ai_edge_indicator : Double - 113
} - 114
- 115
///| - 116
/// 压缩伪影分析结果 - 117
pub(all) struct CompressionAnalysisResult { - 118
blocking_artifact_level : Double - 119
ringing_artifact_level : Double - 120
compression_noise_level : Double - 121
quantization_table_consistency : Double - 122
ai_compression_indicator : Double - 123
} - 124
- 125
///| - 126
/// 细节分析结果 - 127
pub(all) struct DetailAnalysisResult { - 128
eye_detail_consistency : Double - 129
finger_detail_consistency : Double - 130
hair_detail_consistency : Double - 131
text_readability : Double - 132
symmetry_consistency : Double - 133
ai_detail_indicator : Double - 134
} - 135
- 136
///| - 137
/// 创建默认鉴定配置 - 138
pub fn create_default_source_verification_config() -> SourceVerificationConfig { - 139●
SourceVerificationConfig::{ - 140
decision_threshold: 0.5, - 141
use_noise_analysis: true, - 142
use_frequency_analysis: true, - 143
use_color_analysis: true, - 144
use_texture_analysis: true, - 145
use_edge_analysis: true, - 146
use_compression_analysis: true, - 147
use_detail_analysis: true, - 148
weights: [0.2, 0.15, 0.1, 0.15, 0.1, 0.1, 0.2], - 149
} - 150
} - 151
- 152
///| - 153
/// 噪声模式分析 - 154
pub fn analyze_noise_pattern( - 155
img : @types.Image, - 156
) -> NoiseAnalysisResult raise @types.LoadError { - 157
// 输入验证 - 158●
if img.width <= 0 || img.height <= 0 { - 159●
raise @types.LoadError::DecodeFailed( - 160
"analyze_noise_pattern: 图像尺寸必须大于0", - 161
) - 162
} - 163
if img.channels < 1 || img.channels > 4 { - 164○
raise @types.LoadError::DecodeFailed( - 165
"analyze_noise_pattern: 通道数必须在1-4之间", - 166
) - 167
} - 168
let expected_size = img.width * img.height * img.channels - 169●
if img.data.length() != expected_size { - 170○
raise @types.LoadError::DecodeFailed( - 171
"analyze_noise_pattern: 数据大小不匹配", - 172
) - 173
} - 174
let w = img.width - 175
let h = img.height - 176
let total = w * h - 177
- 178
// 计算噪声水平(高频分量) - 179
let mut noise_level = 0.0 - 180
for y in 1..<(h - 1) { - 181●
for x in 1..<(w - 1) { - 182●
let idx = y * w + x - 183●
let center = img.data[idx].to_double() - 184
let avg = ( - 185●
img.data[idx - 1].to_double() + - 186●
img.data[idx + 1].to_double() + - 187●
img.data[idx - w].to_double() + - 188●
img.data[idx + w].to_double() - 189
) / - 190
4.0 - 191
noise_level += (center - avg) * (center - avg) - 192
} - 193
} - 194●
noise_level /= total.to_double() - 195●
noise_level = noise_level.sqrt() - 196
- 197
// 噪声相关性(相邻像素噪声的相关性) - 198
let mut noise_correlation = 0.0 - 199
let mut count = 0 - 200
for y in 0..<h { - 201●
for x in 0..<(w - 1) { - 202●
let n1 = img.data[y * w + x].to_double() - 203●
let n2 = img.data[y * w + x + 1].to_double() - 204
noise_correlation += n1 * n2 - 205
count += 1 - 206
} - 207
} - 208
if count > 0 { - 209●
noise_correlation /= count.to_double() - 210
} - 211
- 212
// AI 生成图像通常噪声水平较低且相关性较高 - 213
let ai_noise_indicator = if noise_level < 10.0 && noise_correlation > 100.0 { - 214●
0.8 - 215●
} else if noise_level < 15.0 { - 216○
0.6 - 217
} else { - 218●
0.3 - 219
} - 220
- 221
NoiseAnalysisResult::{ - 222
noise_level, - 223
noise_correlation, - 224
noise_spectrum_flatness: 0.5, - 225
noise_pattern_regularity: 0.5, - 226
ai_noise_indicator, - 227
} - 228
} - 229
- 230
///| - 231
/// 频域分析(简化版,使用梯度能量代替 FFT) - 232
pub fn analyze_frequency_domain( - 233
img : @types.Image, - 234
) -> FrequencyAnalysisResult raise @types.LoadError { - 235
// 输入验证 - 236●
if img.width <= 0 || img.height <= 0 { - 237●
raise @types.LoadError::DecodeFailed( - 238
"analyze_frequency_domain: 图像尺寸必须大于0", - 239
) - 240
} - 241
if img.channels < 1 || img.channels > 4 { - 242○
raise @types.LoadError::DecodeFailed( - 243
"analyze_frequency_domain: 通道数必须在1-4之间", - 244
) - 245
} - 246
let expected_size = img.width * img.height * img.channels - 247●
if img.data.length() != expected_size { - 248○
raise @types.LoadError::DecodeFailed( - 249
"analyze_frequency_domain: 数据大小不匹配", - 250
) - 251
} - 252
let w = img.width - 253
let h = img.height - 254
let total = w * h - 255
- 256
// 高频能量(梯度) - 257
let mut high_freq_energy = 0.0 - 258
let mut low_freq_energy = 0.0 - 259
- 260
for y in 1..<(h - 1) { - 261●
for x in 1..<(w - 1) { - 262●
let idx = y * w + x - 263●
let gx = img.data[idx + 1].to_double() - img.data[idx - 1].to_double() - 264●
let gy = img.data[idx + w].to_double() - img.data[idx - w].to_double() - 265●
high_freq_energy += (gx * gx + gy * gy).sqrt() - 266
} - 267
} - 268●
high_freq_energy /= total.to_double() - 269
- 270
// 低频能量(局部平均) - 271
for y in 0..<h { - 272●
for x in 0..<w { - 273●
low_freq_energy += img.data[y * w + x].to_double() - 274
} - 275
} - 276●
low_freq_energy /= total.to_double() - 277
- 278
// 频谱平坦度 - 279
let spectrum_flatness = if high_freq_energy > 0.0 { - 280●
low_freq_energy / (low_freq_energy + high_freq_energy) - 281
} else { - 282●
0.5 - 283
} - 284
- 285
// AI 生成图像通常高频能量较低 - 286
let ai_frequency_indicator = if high_freq_energy < 20.0 { - 287●
0.75 - 288●
} else if high_freq_energy < 30.0 { - 289●
0.55 - 290
} else { - 291●
0.3 - 292
} - 293
- 294
FrequencyAnalysisResult::{ - 295
high_frequency_energy: high_freq_energy, - 296
low_frequency_energy: low_freq_energy, - 297
frequency_spectrum_flatness: spectrum_flatness, - 298
spectral_centroid: 0.5, - 299
spectral_spread: 0.5, - 300
ai_frequency_indicator, - 301
} - 302
} - 303
- 304
///| - 305
/// 颜色分布分析 - 306
pub fn analyze_color_distribution( - 307
img : @types.Image, - 308
) -> ColorAnalysisResult raise @types.LoadError { - 309
// 输入验证 - 310●
if img.width <= 0 || img.height <= 0 { - 311●
raise @types.LoadError::DecodeFailed( - 312
"analyze_color_distribution: 图像尺寸必须大于0", - 313
) - 314
} - 315
if img.channels < 1 || img.channels > 4 { - 316○
raise @types.LoadError::DecodeFailed( - 317
"analyze_color_distribution: 通道数必须在1-4之间", - 318
) - 319
} - 320
let expected_size = img.width * img.height * img.channels - 321●
if img.data.length() != expected_size { - 322●
raise @types.LoadError::DecodeFailed( - 323
"analyze_color_distribution: 数据大小不匹配", - 324
) - 325
} - 326
let w = img.width - 327
let h = img.height - 328
let total = w * h - 329
- 330
// 颜色直方图平滑度 - 331
let histogram : Array[Int32] = [] - 332
for _ in 0..<64 { - 333●
histogram.push(0) - 334
} - 335
for i in 0..<total { - 336●
let bin = img.data[i].to_int() * 64 / 256 - 337
histogram[bin] += 1 - 338
} - 339
- 340
let mut smoothness = 0.0 - 341
for i in 1..<63 { - 342●
let diff = (histogram[i + 1] - 2 * histogram[i] + histogram[i - 1]) - 343●
.to_double() - 344●
.abs() - 345
smoothness += diff - 346
} - 347
smoothness /= 64.0 - 348
- 349
// 颜色过渡自然度 - 350
let mut transition_naturalness = 0.0 - 351
let mut count = 0 - 352
for y in 0..<h { - 353●
for x in 0..<(w - 1) { - 354●
let diff = (img.data[y * w + x].to_double() - - 355●
img.data[y * w + x + 1].to_double()).abs() - 356
transition_naturalness += diff - 357
count += 1 - 358
} - 359
} - 360
if count > 0 { - 361●
transition_naturalness /= count.to_double() - 362
} - 363
- 364
// AI 生成图像通常颜色过渡过于平滑 - 365
let ai_color_indicator = if smoothness < 100.0 && - 366
transition_naturalness < 10.0 { - 367●
0.7 - 368●
} else if smoothness < 200.0 { - 369●
0.5 - 370
} else { - 371○
0.3 - 372
} - 373
- 374
ColorAnalysisResult::{ - 375
color_histogram_smoothness: smoothness, - 376
color_transition_naturalness: transition_naturalness, - 377
color_quantization_level: 0.5, - 378
skin_tone_accuracy: 0.5, - 379
ai_color_indicator, - 380
} - 381
} - 382
- 383
///| - 384
/// 纹理分析 - 385
pub fn analyze_texture( - 386
img : @types.Image, - 387
) -> TextureAnalysisResult raise @types.LoadError { - 388
// 输入验证 - 389●
if img.width <= 0 || img.height <= 0 { - 390●
raise @types.LoadError::DecodeFailed( - 391
"analyze_texture: 图像尺寸必须大于0", - 392
) - 393
} - 394
if img.channels < 1 || img.channels > 4 { - 395○
raise @types.LoadError::DecodeFailed( - 396
"analyze_texture: 通道数必须在1-4之间", - 397
) - 398
} - 399
let expected_size = img.width * img.height * img.channels - 400●
if img.data.length() != expected_size { - 401○
raise @types.LoadError::DecodeFailed( - 402
"analyze_texture: 数据大小不匹配", - 403
) - 404
} - 405
let w = img.width - 406
let h = img.height - 407
let total = w * h - 408
- 409
// 纹理复杂度(局部方差) - 410
let mut texture_complexity = 0.0 - 411
for y in 1..<(h - 1) { - 412●
for x in 1..<(w - 1) { - 413●
let idx = y * w + x - 414●
let center = img.data[idx].to_double() - 415
let mut sum = 0.0 - 416
for dy in -1..<2 { - 417●
for dx in -1..<2 { - 418●
if dx == 0 && dy == 0 { - 419●
continue - 420
} - 421●
sum += img.data[(y + dy) * w + (x + dx)].to_double() - 422
} - 423
} - 424
let avg = sum / 8.0 - 425
texture_complexity += (center - avg) * (center - avg) - 426
} - 427
} - 428●
texture_complexity /= total.to_double() - 429●
texture_complexity = texture_complexity.sqrt() - 430
- 431
// 纹理规律性 - 432
let mut texture_regularity = 0.0 - 433
for y in 0..<(h - 2) { - 434●
for x in 0..<(w - 2) { - 435●
let p1 = img.data[y * w + x].to_double() - 436●
let p2 = img.data[y * w + x + 2].to_double() - 437●
texture_regularity += (p1 - p2).abs() - 438
} - 439
} - 440●
texture_regularity /= total.to_double() - 441
- 442
// AI 生成图像通常纹理复杂度较低且规律性较高 - 443
let ai_texture_indicator = if texture_complexity < 15.0 && - 444
texture_regularity < 10.0 { - 445●
0.7 - 446●
} else if texture_complexity < 25.0 { - 447●
0.5 - 448
} else { - 449●
0.3 - 450
} - 451
- 452
TextureAnalysisResult::{ - 453
texture_regularity, - 454
texture_complexity, - 455
micro_texture_consistency: 0.5, - 456
macro_texture_consistency: 0.5, - 457
ai_texture_indicator, - 458
} - 459
} - 460
- 461
///| - 462
/// 边缘分析 - 463
pub fn analyze_edges( - 464
img : @types.Image, - 465
) -> EdgeAnalysisResult raise @types.LoadError { - 466
// 输入验证 - 467●
if img.width <= 0 || img.height <= 0 { - 468●
raise @types.LoadError::DecodeFailed( - 469
"analyze_edges: 图像尺寸必须大于0", - 470
) - 471
} - 472
if img.channels < 1 || img.channels > 4 { - 473○
raise @types.LoadError::DecodeFailed( - 474
"analyze_edges: 通道数必须在1-4之间", - 475
) - 476
} - 477
let expected_size = img.width * img.height * img.channels - 478●
if img.data.length() != expected_size { - 479○
raise @types.LoadError::DecodeFailed("analyze_edges: 数据大小不匹配") - 480
} - 481
let w = img.width - 482
let h = img.height - 483
let _ = w * h - 484
- 485
// 边缘锐度(梯度幅值) - 486
let mut edge_sharpness = 0.0 - 487
let mut edge_count = 0 - 488
for y in 1..<(h - 1) { - 489●
for x in 1..<(w - 1) { - 490●
let idx = y * w + x - 491●
let gx = img.data[idx + 1].to_double() - img.data[idx - 1].to_double() - 492●
let gy = img.data[idx + w].to_double() - img.data[idx - w].to_double() - 493●
let magnitude = (gx * gx + gy * gy).sqrt() - 494
if magnitude > 30.0 { - 495●
edge_sharpness += magnitude - 496
edge_count += 1 - 497
} - 498
} - 499
} - 500
if edge_count > 0 { - 501●
edge_sharpness /= edge_count.to_double() - 502
} - 503
- 504
// 边缘连续性 - 505
let mut edge_continuity = 0.0 - 506
for y in 1..<(h - 1) { - 507●
for x in 1..<(w - 1) { - 508●
let idx = y * w + x - 509●
let gx = img.data[idx + 1].to_double() - img.data[idx - 1].to_double() - 510●
let gy = img.data[idx + w].to_double() - img.data[idx - w].to_double() - 511●
let magnitude = (gx * gx + gy * gy).sqrt() - 512
if magnitude > 30.0 { - 513
// 检查邻域是否也有边缘 - 514●
let mut neighbor_edges = 0 - 515
for dy in -1..<2 { - 516●
for dx in -1..<2 { - 517●
if dx == 0 && dy == 0 { - 518●
continue - 519
} - 520
let ny = y + dy - 521
let nx = x + dx - 522
if ny < 1 || ny > h - 2 || nx < 1 || nx > w - 2 { - 523●
continue - 524
} - 525
let nidx = ny * w + nx - 526●
let ngx = img.data[nidx + 1].to_double() - - 527●
img.data[nidx - 1].to_double() - 528●
let ngy = img.data[nidx + w].to_double() - - 529●
img.data[nidx - w].to_double() - 530●
let nmag = (ngx * ngx + ngy * ngy).sqrt() - 531
if nmag > 30.0 { - 532●
neighbor_edges += 1 - 533
} - 534
} - 535
} - 536●
edge_continuity += neighbor_edges.to_double() / 8.0 - 537
} - 538
} - 539
} - 540
if edge_count > 0 { - 541●
edge_continuity /= edge_count.to_double() - 542
} - 543
- 544
// AI 生成图像通常边缘锐度较低且连续性较差 - 545
let ai_edge_indicator = if edge_sharpness < 50.0 && edge_continuity < 0.5 { - 546●
0.65 - 547●
} else if edge_sharpness < 70.0 { - 548○
0.45 - 549
} else { - 550●
0.25 - 551
} - 552
- 553
EdgeAnalysisResult::{ - 554
edge_sharpness, - 555
edge_continuity, - 556
edge_consistency: 0.5, - 557
edge_artifact_level: 0.5, - 558
ai_edge_indicator, - 559
} - 560
} - 561
- 562
///| - 563
/// 压缩伪影分析 - 564
pub fn analyze_compression_artifacts( - 565
img : @types.Image, - 566
) -> CompressionAnalysisResult raise @types.LoadError { - 567
// 输入验证 - 568●
if img.width <= 0 || img.height <= 0 { - 569●
raise @types.LoadError::DecodeFailed( - 570
"analyze_compression_artifacts: 图像尺寸必须大于0", - 571
) - 572
} - 573
if img.channels < 1 || img.channels > 4 { - 574○
raise @types.LoadError::DecodeFailed( - 575
"analyze_compression_artifacts: 通道数必须在1-4之间", - 576
) - 577
} - 578
let expected_size = img.width * img.height * img.channels - 579●
if img.data.length() != expected_size { - 580○
raise @types.LoadError::DecodeFailed( - 581
"analyze_compression_artifacts: 数据大小不匹配", - 582
) - 583
} - 584
let w = img.width - 585
let h = img.height - 586
let total = w * h - 587
- 588
// 块效应检测(8x8 块边界) - 589
let mut blocking_artifact = 0.0 - 590
let mut block_count = 0 - 591
for y in 0..<h { - 592●
for x in 0..<w { - 593●
if x % 8 == 0 && x > 0 && x < w - 1 { - 594●
let diff = (img.data[y * w + x].to_double() - - 595●
img.data[y * w + x - 1].to_double()).abs() - 596
blocking_artifact += diff - 597
block_count += 1 - 598
} - 599
if y % 8 == 0 && y > 0 && y < h - 1 { - 600●
let diff = (img.data[y * w + x].to_double() - - 601●
img.data[(y - 1) * w + x].to_double()).abs() - 602
blocking_artifact += diff - 603
block_count += 1 - 604
} - 605
} - 606
} - 607
if block_count > 0 { - 608●
blocking_artifact /= block_count.to_double() - 609
} - 610
- 611
// 振铃效应检测(边缘附近的振荡) - 612
let mut ringing_artifact = 0.0 - 613
for y in 2..<(h - 2) { - 614●
for x in 2..<(w - 2) { - 615●
let idx = y * w + x - 616●
let center = img.data[idx].to_double() - 617●
let _ = img.data[idx - 2].to_double() - 618●
let p2 = img.data[idx - 1].to_double() - 619●
let p3 = img.data[idx + 1].to_double() - 620●
let _ = img.data[idx + 2].to_double() - 621
// 检测振荡模式 - 622
if (p2 > center && p3 > center) || (p2 < center && p3 < center) { - 623●
ringing_artifact += 1.0 - 624
} - 625
} - 626
} - 627●
ringing_artifact /= total.to_double() - 628
- 629
// AI 生成图像通常压缩伪影较少 - 630
let ai_compression_indicator = if blocking_artifact < 5.0 && - 631
ringing_artifact < 0.1 { - 632●
0.6 - 633●
} else if blocking_artifact < 10.0 { - 634●
0.4 - 635
} else { - 636●
0.2 - 637
} - 638
- 639
CompressionAnalysisResult::{ - 640
blocking_artifact_level: blocking_artifact, - 641
ringing_artifact_level: ringing_artifact, - 642
compression_noise_level: 0.5, - 643
quantization_table_consistency: 0.5, - 644
ai_compression_indicator, - 645
} - 646
} - 647
- 648
///| - 649
/// 细节分析 - 650
pub fn analyze_details( - 651
img : @types.Image, - 652
) -> DetailAnalysisResult raise @types.LoadError { - 653
// 输入验证 - 654●
if img.width <= 0 || img.height <= 0 { - 655●
raise @types.LoadError::DecodeFailed( - 656
"analyze_details: 图像尺寸必须大于0", - 657
) - 658
} - 659
if img.channels < 1 || img.channels > 4 { - 660○
raise @types.LoadError::DecodeFailed( - 661
"analyze_details: 通道数必须在1-4之间", - 662
) - 663
} - 664
let expected_size = img.width * img.height * img.channels - 665●
if img.data.length() != expected_size { - 666○
raise @types.LoadError::DecodeFailed( - 667
"analyze_details: 数据大小不匹配", - 668
) - 669
} - 670
let w = img.width - 671
let h = img.height - 672
let _ = w * h - 673
- 674
// 细节一致性(高频细节的局部一致性) - 675
let mut detail_consistency = 0.0 - 676
let mut detail_count = 0 - 677
for y in 2..<(h - 2) { - 678●
for x in 2..<(w - 2) { - 679●
let idx = y * w + x - 680●
let _ = img.data[idx].to_double() - 681
// 检查 5x5 邻域内的细节变化 - 682
let mut variance = 0.0 - 683
let mut sum = 0.0 - 684
let mut count = 0 - 685
for dy in -2..<3 { - 686●
for dx in -2..<3 { - 687●
let val = img.data[(y + dy) * w + (x + dx)].to_double() - 688
sum += val - 689
count += 1 - 690
} - 691
} - 692●
let avg = sum / count.to_double() - 693
for dy in -2..<3 { - 694●
for dx in -2..<3 { - 695●
let val = img.data[(y + dy) * w + (x + dx)].to_double() - 696
variance += (val - avg) * (val - avg) - 697
} - 698
} - 699●
variance /= count.to_double() - 700
if variance > 100.0 { - 701●
detail_consistency += variance.sqrt() - 702
detail_count += 1 - 703
} - 704
} - 705
} - 706
if detail_count > 0 { - 707●
detail_consistency /= detail_count.to_double() - 708
} - 709
- 710
// 对称性检测(左右对称性) - 711
let mut symmetry = 0.0 - 712
let half_w = w / 2 - 713
for y in 0..<h { - 714●
for x in 0..<half_w { - 715●
let left = img.data[y * w + x].to_double() - 716●
let right = img.data[y * w + (w - 1 - x)].to_double() - 717●
symmetry += (left - right).abs() - 718
} - 719
} - 720●
symmetry /= (h * half_w).to_double() - 721
- 722
// AI 生成图像通常细节一致性较差且对称性不自然 - 723
let ai_detail_indicator = if detail_consistency < 20.0 && symmetry > 30.0 { - 724○
0.7 - 725●
} else if detail_consistency < 30.0 { - 726●
0.5 - 727
} else { - 728●
0.3 - 729
} - 730
- 731
DetailAnalysisResult::{ - 732
eye_detail_consistency: detail_consistency, - 733
finger_detail_consistency: detail_consistency, - 734
hair_detail_consistency: detail_consistency, - 735
text_readability: 0.5, - 736
symmetry_consistency: symmetry, - 737
ai_detail_indicator, - 738
} - 739
} - 740
- 741
///| - 742
/// 综合来源鉴定 - 743
/// @deprecated 已废弃,请使用 verify_image_source_enhanced 代替 - 744
#deprecated("Use verify_image_source_enhanced instead", skip_current_package=true) - 745
pub fn verify_image_source( - 746
img : @types.Image, - 747
config : SourceVerificationConfig, - 748
) -> SourceVerificationResult raise @types.LoadError { - 749
// 输入验证 - 750
if img.width <= 0 || img.height <= 0 { - 751
raise @types.LoadError::DecodeFailed( - 752
"verify_image_source: 图像尺寸必须大于0", - 753
) - 754
} - 755
if img.channels < 1 || img.channels > 4 { - 756
raise @types.LoadError::DecodeFailed( - 757
"verify_image_source: 通道数必须在1-4之间", - 758
) - 759
} - 760
let expected_size = img.width * img.height * img.channels - 761
if img.data.length() != expected_size { - 762
raise @types.LoadError::DecodeFailed( - 763
"verify_image_source: 数据大小不匹配", - 764
) - 765
} - 766
let noise_score = if config.use_noise_analysis { - 767
analyze_noise_pattern(img).ai_noise_indicator - 768
} else { - 769
0.5 - 770
} - 771
let freq_score = if config.use_frequency_analysis { - 772
analyze_frequency_domain(img).ai_frequency_indicator - 773
} else { - 774
0.5 - 775
} - 776
let color_score = if config.use_color_analysis { - 777
analyze_color_distribution(img).ai_color_indicator - 778
} else { - 779
0.5 - 780
} - 781
let texture_score = if config.use_texture_analysis { - 782
analyze_texture(img).ai_texture_indicator - 783
} else { - 784
0.5 - 785
} - 786
let edge_score = if config.use_edge_analysis { - 787
analyze_edges(img).ai_edge_indicator - 788
} else { - 789
0.5 - 790
} - 791
let compression_score = if config.use_compression_analysis { - 792
analyze_compression_artifacts(img).ai_compression_indicator - 793
} else { - 794
0.5 - 795
} - 796
let detail_score = if config.use_detail_analysis { - 797
analyze_details(img).ai_detail_indicator - 798
} else { - 799
0.5 - 800
} - 801
- 802
// 加权平均 - 803
let mut weighted_sum = 0.0 - 804
let mut weight_sum = 0.0 - 805
let scores = [ - 806
noise_score, freq_score, color_score, texture_score, edge_score, compression_score, - 807
detail_score, - 808
] - 809
for i in 0..<scores.length() { - 810
let weight = if i < config.weights.length() { - 811
config.weights[i] - 812
} else { - 813
1.0 - 814
} - 815
weighted_sum += scores[i] * weight - 816
weight_sum += weight - 817
} - 818
let overall_score = if weight_sum > 0.0 { - 819
weighted_sum / weight_sum - 820
} else { - 821
0.5 - 822
} - 823
- 824
let features = SourceVerificationFeatures::{ - 825
noise_pattern_score: noise_score, - 826
frequency_domain_score: freq_score, - 827
color_distribution_score: color_score, - 828
texture_score, - 829
edge_score, - 830
compression_artifact_score: compression_score, - 831
detail_score, - 832
metadata_score: 0.5, - 833
overall_score, - 834
} - 835
- 836
// 决策 - 837
let ai_probability = overall_score - 838
let physical_probability = 1.0 - overall_score - 839
let confidence = if ai_probability > 0.7 || ai_probability < 0.3 { - 840
(ai_probability - 0.5).abs() * 2.0 - 841
} else { - 842
0.3 - 843
} - 844
- 845
let source_type = if ai_probability >= config.decision_threshold { - 846
ImageSourceType::AIGenerated - 847
} else { - 848
ImageSourceType::PhysicalCaptured - 849
} - 850
- 851
SourceVerificationResult::{ - 852
source_type, - 853
ai_probability, - 854
physical_probability, - 855
confidence, - 856
decision_threshold: config.decision_threshold, - 857
features, - 858
processing_time_ms: 0.0, - 859
} - 860
} - 861
- 862
///| - 863
/// 生成鉴定报告 - 864
pub fn source_verification_report_to_string( - 865
result : SourceVerificationResult, - 866
) -> String { - 867●
let mut report = "## 图像来源鉴定报告\n\n" - 868
report = report + "### 鉴定结论\n\n" - 869
report = report + "| 指标 | 值 |\n|------|------|\n" - 870
let source_str = if result.source_type == ImageSourceType::AIGenerated { - 871●
"AI 生成" - 872●
} else if result.source_type == ImageSourceType::PhysicalCaptured { - 873●
"物理生成(真实拍摄)" - 874○
} else if result.source_type == ImageSourceType::Unknown { - 875○
"未知" - 876
} else { - 877○
"混合" - 878
} - 879
report = report + "| 来源类型 | " + source_str + " |\n" - 880
report = report + - 881
"| AI 生成概率 | " + - 882●
(result.ai_probability * 100.0).to_string() + - 883
"% |\n" - 884
report = report + - 885
"| 物理生成概率 | " + - 886●
(result.physical_probability * 100.0).to_string() + - 887
"% |\n" - 888
report = report + - 889
"| 置信度 | " + - 890●
(result.confidence * 100.0).to_string() + - 891
"% |\n" - 892
report = report + - 893
"| 决策阈值 | " + - 894●
result.decision_threshold.to_string() + - 895
" |\n\n" - 896
- 897
report = report + "### 特征分析\n\n" - 898
report = report + - 899
"| 特征 | AI 指示值 | 说明 |\n|------|-----------|------|\n" - 900
report = report + - 901
"| 噪声模式 | " + - 902●
(result.features.noise_pattern_score * 100.0).to_string() + - 903
"% | AI 生成图像噪声通常较低且相关性高 |\n" - 904
report = report + - 905
"| 频域特征 | " + - 906●
(result.features.frequency_domain_score * 100.0).to_string() + - 907
"% | AI 生成图像高频能量通常较低 |\n" - 908
report = report + - 909
"| 颜色分布 | " + - 910●
(result.features.color_distribution_score * 100.0).to_string() + - 911
"% | AI 生成图像颜色过渡通常过于平滑 |\n" - 912
report = report + - 913
"| 纹理特征 | " + - 914●
(result.features.texture_score * 100.0).to_string() + - 915
"% | AI 生成图像纹理复杂度通常较低 |\n" - 916
report = report + - 917
"| 边缘特征 | " + - 918●
(result.features.edge_score * 100.0).to_string() + - 919
"% | AI 生成图像边缘锐度通常较低 |\n" - 920
report = report + - 921
"| 压缩伪影 | " + - 922●
(result.features.compression_artifact_score * 100.0).to_string() + - 923
"% | AI 生成图像压缩伪影通常较少 |\n" - 924
report = report + - 925
"| 细节特征 | " + - 926●
(result.features.detail_score * 100.0).to_string() + - 927
"% | AI 生成图像细节一致性通常较差 |\n" - 928
report = report + - 929
"| 综合评分 | " + - 930●
(result.features.overall_score * 100.0).to_string() + - 931
"% | 加权平均综合评分 |\n\n" - 932
- 933
report = report + "### 说明\n\n" - 934
report = report + - 935
"- 本鉴定基于图像的统计特征分析,结果仅供参考\n" - 936
report = report + "- AI 指示值越高,表示越可能是 AI 生成\n" - 937
report = report + "- 置信度表示鉴定结果的可靠程度\n" - 938
report = report + - 939
"- 对于经过编辑、压缩或重采样的图像,鉴定准确率可能下降\n" - 940
- 941
report - 942
}