image.photography\style_transfer\style_transfer_enhancement.mbt
91.6% · 208 / 227 points · 816 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 风格迁移增强 style_transfer/style_transfer_enhancement - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// 图像风格迁移增强(v24.0.0) - 7
// 图像风格迁移、艺术风格化、颜色转移、纹理合成 - 8
- 9
///| - 10
/// 风格迁移算法类型 - 11
/// `#non_exhaustive` 表示未来可能添加新构造器 - 12
#derive(Eq, @debug.Debug) - 13
#non_exhaustive - 14
pub(all) enum StyleTransferAlgorithm { - 15
ColorTransfer - 16
TextureSynthesis - 17
NeuralStyle - 18
FastStyle - 19
CycleGAN - 20
AdaIN - 21
Cartoon - 22
OilPainting - 23
Watercolor - 24
Sketch - 25
Unknown - 26
} - 27
- 28
///| - 29
pub extend StyleTransferAlgorithm with @debug.Debug::{to_repr} - 30
- 31
///| - 32
pub extend StyleTransferAlgorithm with Eq::{not_equal, equal} - 33
- 34
///| - 35
/// 风格迁移结果 - 36
#derive(Eq, @debug.Debug) - 37
pub(all) struct StyleTransferResult { - 38
image : @types.Image - 39
content_image_path : String - 40
style_image_path : String - 41
algorithm : StyleTransferAlgorithm - 42
style_strength : Double - 43
content_strength : Double - 44
processing_time_ms : Double - 45
iterations : Int32 - 46
} - 47
- 48
///| - 49
pub extend StyleTransferResult with Eq::{not_equal, equal} - 50
- 51
///| - 52
pub extend StyleTransferResult with @debug.Debug::{to_repr} - 53
- 54
///| - 55
/// 风格迁移配置 - 56
#derive(Eq, @debug.Debug) - 57
pub(all) struct StyleTransferConfig { - 58
algorithm : StyleTransferAlgorithm - 59
style_strength : Double - 60
content_strength : Double - 61
iterations : Int32 - 62
learning_rate : Double - 63
use_color_preservation : Bool - 64
resolution : Int32 - 65
} - 66
- 67
///| - 68
pub extend StyleTransferConfig with Eq::{not_equal, equal} - 69
- 70
///| - 71
pub extend StyleTransferConfig with @debug.Debug::{to_repr} - 72
- 73
///| - 74
/// 颜色统计信息 - 75
#derive(Eq, @debug.Debug) - 76
pub(all) struct ColorStatistics { - 77
mean_r : Double - 78
mean_g : Double - 79
mean_b : Double - 80
std_r : Double - 81
std_g : Double - 82
std_b : Double - 83
min_r : Double - 84
min_g : Double - 85
min_b : Double - 86
max_r : Double - 87
max_g : Double - 88
max_b : Double - 89
} - 90
- 91
///| - 92
pub extend ColorStatistics with Eq::{not_equal, equal} - 93
- 94
///| - 95
pub extend ColorStatistics with @debug.Debug::{to_repr} - 96
- 97
///| - 98
/// 纹理特征 - 99
#derive(Eq, @debug.Debug) - 100
pub(all) struct StyleTransferTextureFeatures { - 101
contrast : Double - 102
homogeneity : Double - 103
energy : Double - 104
correlation : Double - 105
entropy : Double - 106
coarseness : Double - 107
directionality : Double - 108
regularity : Double - 109
} - 110
- 111
///| - 112
pub extend StyleTransferTextureFeatures with Eq::{not_equal, equal} - 113
- 114
///| - 115
pub extend StyleTransferTextureFeatures with @debug.Debug::{to_repr} - 116
- 117
///| - 118
/// 艺术风格类型 - 119
/// `#non_exhaustive` 表示未来可能添加新构造器 - 120
#derive(Eq, @debug.Debug) - 121
#non_exhaustive - 122
pub(all) enum ArtStyle { - 123
Impressionist - 124
Cubist - 125
Surrealist - 126
Abstract - 127
PopArt - 128
Minimalist - 129
Baroque - 130
Renaissance - 131
Modern - 132
Contemporary - 133
Unknown - 134
} - 135
- 136
///| - 137
pub extend ArtStyle with @debug.Debug::{to_repr} - 138
- 139
///| - 140
pub extend ArtStyle with Eq::{not_equal, equal} - 141
- 142
///| - 143
/// 创建默认风格迁移配置 - 144
pub fn create_default_style_transfer_config() -> StyleTransferConfig { - 145●
StyleTransferConfig::{ - 146
algorithm: StyleTransferAlgorithm::ColorTransfer, - 147
style_strength: 0.5, - 148
content_strength: 0.5, - 149
iterations: 100, - 150
learning_rate: 0.01, - 151
use_color_preservation: true, - 152
resolution: 512, - 153
} - 154
} - 155
- 156
///| - 157
/// 计算颜色统计信息 - 158
pub fn calculate_color_statistics( - 159
img : @types.Image, - 160
) -> ColorStatistics raise @types.LoadError { - 161
// 输入验证 - 162●
if img.width <= 0 || img.height <= 0 { - 163●
raise @types.LoadError::DecodeFailed( - 164
"calculate_color_statistics: 图像尺寸必须大于0", - 165
) - 166
} - 167
if img.channels < 1 || img.channels > 4 { - 168●
raise @types.LoadError::DecodeFailed( - 169
"calculate_color_statistics: 通道数必须在1-4之间", - 170
) - 171
} - 172
let expected_size = img.width * img.height * img.channels - 173●
if img.data.length() != expected_size { - 174●
raise @types.LoadError::DecodeFailed( - 175
"calculate_color_statistics: 数据大小不匹配", - 176
) - 177
} - 178
let w = img.width - 179
let h = img.height - 180
let total = w * h - 181
let mut sum_r = 0.0 - 182
let mut sum_g = 0.0 - 183
let mut sum_b = 0.0 - 184
let mut min_r = 255.0 - 185
let mut min_g = 255.0 - 186
let mut min_b = 255.0 - 187
let mut max_r = 0.0 - 188
let mut max_g = 0.0 - 189
let mut max_b = 0.0 - 190
- 191
for i in 0..<total { - 192●
let pixel = img.data[i].to_double() - 193
// 简化:单通道灰度 - 194
sum_r += pixel - 195
sum_g += pixel - 196
sum_b += pixel - 197
if pixel < min_r { - 198●
min_r = pixel - 199
} - 200
if pixel < min_g { - 201●
min_g = pixel - 202
} - 203
if pixel < min_b { - 204●
min_b = pixel - 205
} - 206
if pixel > max_r { - 207●
max_r = pixel - 208
} - 209
if pixel > max_g { - 210●
max_g = pixel - 211
} - 212
if pixel > max_b { - 213●
max_b = pixel - 214
} - 215
} - 216
- 217●
let mean_r = sum_r / total.to_double() - 218●
let mean_g = sum_g / total.to_double() - 219●
let mean_b = sum_b / total.to_double() - 220
- 221
let mut var_r = 0.0 - 222
let mut var_g = 0.0 - 223
let mut var_b = 0.0 - 224
for i in 0..<total { - 225●
let pixel = img.data[i].to_double() - 226
var_r += (pixel - mean_r) * (pixel - mean_r) - 227
var_g += (pixel - mean_g) * (pixel - mean_g) - 228
var_b += (pixel - mean_b) * (pixel - mean_b) - 229
} - 230●
var_r /= total.to_double() - 231●
var_g /= total.to_double() - 232●
var_b /= total.to_double() - 233
- 234
ColorStatistics::{ - 235
mean_r, - 236
mean_g, - 237
mean_b, - 238●
std_r: var_r.sqrt(), - 239●
std_g: var_g.sqrt(), - 240●
std_b: var_b.sqrt(), - 241
min_r, - 242
min_g, - 243
min_b, - 244
max_r, - 245
max_g, - 246
max_b, - 247
} - 248
} - 249
- 250
///| - 251
/// 颜色转移(Reinhard 方法) - 252
pub fn color_transfer( - 253
content : @types.Image, - 254
style : @types.Image, - 255
strength : Double, - 256
) -> @types.Image raise @types.LoadError { - 257
// 输入验证:content - 258●
if content.width <= 0 || content.height <= 0 { - 259●
raise @types.LoadError::DecodeFailed( - 260
"color_transfer: 图像尺寸必须大于0", - 261
) - 262
} - 263
if content.channels < 1 || content.channels > 4 { - 264●
raise @types.LoadError::DecodeFailed( - 265
"color_transfer: 通道数必须在1-4之间", - 266
) - 267
} - 268
let content_expected = content.width * content.height * content.channels - 269●
if content.data.length() != content_expected { - 270●
raise @types.LoadError::DecodeFailed( - 271
"color_transfer: 数据大小不匹配", - 272
) - 273
} - 274
// 输入验证:style - 275
if style.width <= 0 || style.height <= 0 { - 276●
raise @types.LoadError::DecodeFailed( - 277
"color_transfer: 图像尺寸必须大于0", - 278
) - 279
} - 280
if style.channels < 1 || style.channels > 4 { - 281●
raise @types.LoadError::DecodeFailed( - 282
"color_transfer: 通道数必须在1-4之间", - 283
) - 284
} - 285
let style_expected = style.width * style.height * style.channels - 286●
if style.data.length() != style_expected { - 287●
raise @types.LoadError::DecodeFailed( - 288
"color_transfer: 数据大小不匹配", - 289
) - 290
} - 291●
let content_stats = calculate_color_statistics(content) - 292●
let style_stats = calculate_color_statistics(style) - 293
- 294
let w = content.width - 295
let h = content.height - 296
let total = w * h - 297
let out : Array[Byte] = [] - 298
- 299
for i in 0..<total { - 300●
let pixel = content.data[i].to_double() - 301
// 标准化 - 302
let normalized = if content_stats.std_r > 0.0 { - 303●
(pixel - content_stats.mean_r) / content_stats.std_r - 304
} else { - 305○
0.0 - 306
} - 307
// 转移到风格分布 - 308
let transferred = normalized * style_stats.std_r + style_stats.mean_r - 309
// 混合 - 310
let blended = pixel * (1.0 - strength) + transferred * strength - 311
let clamped = if blended < 0.0 { - 312○
0.0 - 313●
} else if blended > 255.0 { - 314○
255.0 - 315
} else { - 316●
blended - 317
} - 318●
out.push(clamped.to_int().to_byte()) - 319
} - 320
- 321
@types.Image::{ - 322
width: w, - 323
height: h, - 324
channels: content.channels, - 325●
data: Bytes::from_array(out), - 326
} - 327
} - 328
- 329
///| - 330
/// 计算纹理特征(GLCM 简化版) - 331
pub fn calculate_texture_features( - 332
img : @types.Image, - 333
) -> StyleTransferTextureFeatures raise @types.LoadError { - 334
// 输入验证 - 335●
if img.width <= 0 || img.height <= 0 { - 336●
raise @types.LoadError::DecodeFailed( - 337
"calculate_texture_features: 图像尺寸必须大于0", - 338
) - 339
} - 340
if img.channels < 1 || img.channels > 4 { - 341●
raise @types.LoadError::DecodeFailed( - 342
"calculate_texture_features: 通道数必须在1-4之间", - 343
) - 344
} - 345
let expected_size = img.width * img.height * img.channels - 346●
if img.data.length() != expected_size { - 347●
raise @types.LoadError::DecodeFailed( - 348
"calculate_texture_features: 数据大小不匹配", - 349
) - 350
} - 351
let w = img.width - 352
let h = img.height - 353
let _ = w * h - 354
- 355
// 计算灰度共生矩阵(简化:水平方向) - 356
let glcm_size = 16 - 357
let glcm : Array[Double] = [] - 358
for _ in 0..<(glcm_size * glcm_size) { - 359●
glcm.push(0.0) - 360
} - 361
- 362
for y in 0..<h { - 363●
for x in 0..<(w - 1) { - 364●
let i = img.data[y * w + x].to_int() * glcm_size / 256 % glcm_size - 365●
let j = img.data[y * w + x + 1].to_int() * glcm_size / 256 % glcm_size - 366
glcm[i * glcm_size + j] += 1.0 - 367
} - 368
} - 369
- 370
// 归一化 - 371
let glcm_total = (w - 1) * h - 372
if glcm_total > 0 { - 373●
for i in 0..<(glcm_size * glcm_size) { - 374●
glcm[i] /= glcm_total.to_double() - 375
} - 376
} - 377
- 378
// 计算特征 - 379
let mut contrast = 0.0 - 380
let mut homogeneity = 0.0 - 381
let mut energy = 0.0 - 382
let mut correlation = 0.0 - 383
let mut entropy = 0.0 - 384
- 385
let mut mean_i = 0.0 - 386
let mut mean_j = 0.0 - 387
for i in 0..<glcm_size { - 388●
for j in 0..<glcm_size { - 389●
let p = glcm[i * glcm_size + j] - 390●
mean_i += i.to_double() * p - 391●
mean_j += j.to_double() * p - 392
} - 393
} - 394
- 395
for i in 0..<glcm_size { - 396●
for j in 0..<glcm_size { - 397●
let p = glcm[i * glcm_size + j] - 398●
let diff = (i - j).to_double() - 399
contrast += diff * diff * p - 400
homogeneity += p / (1.0 + diff * diff) - 401
energy += p * p - 402●
correlation += (i.to_double() - mean_i) * (j.to_double() - mean_j) * p - 403
if p > 0.0 { - 404
// 真香农熵(bit):-Σ p·log2(p)。旧的 -Σ p²·0.1 结果恒为负、与"熵"语义相反(S22 修复) - 405●
entropy -= p * @math.log2(p) - 406
} - 407
} - 408
} - 409
- 410
StyleTransferTextureFeatures::{ - 411
contrast, - 412
homogeneity, - 413
energy, - 414
correlation, - 415
entropy, - 416
coarseness: 1.0 / (1.0 + contrast), - 417
directionality: 0.5, - 418
regularity: homogeneity * energy, - 419
} - 420
} - 421
- 422
///| - 423
/// 卡通化效果 - 424
pub fn cartoonize( - 425
img : @types.Image, - 426
strength : Double, - 427
) -> @types.Image raise @types.LoadError { - 428
// 输入验证 - 429●
if img.width <= 0 || img.height <= 0 { - 430●
raise @types.LoadError::DecodeFailed( - 431
"cartoonize: 图像尺寸必须大于0", - 432
) - 433
} - 434
if img.channels < 1 || img.channels > 4 { - 435●
raise @types.LoadError::DecodeFailed( - 436
"cartoonize: 通道数必须在1-4之间", - 437
) - 438
} - 439
let expected_size = img.width * img.height * img.channels - 440●
if img.data.length() != expected_size { - 441●
raise @types.LoadError::DecodeFailed("cartoonize: 数据大小不匹配") - 442
} - 443
let w = img.width - 444
let h = img.height - 445
let _ = w * h - 446
let out : Array[Byte] = [] - 447
- 448
// 简化:边缘检测 + 色彩量化 - 449
for y in 0..<h { - 450●
for x in 0..<w { - 451●
let idx = y * w + x - 452●
let center = img.data[idx].to_double() - 453
- 454
// 边缘检测(Sobel 简化) - 455
let mut gx = 0.0 - 456
let mut gy = 0.0 - 457
if x > 0 && x < w - 1 && y > 0 && y < h - 1 { - 458●
gx = img.data[(y + 1) * w + (x - 1)].to_double() + - 459●
2.0 * img.data[y * w + (x - 1)].to_double() + - 460●
img.data[(y - 1) * w + (x - 1)].to_double() - - 461●
img.data[(y + 1) * w + (x + 1)].to_double() - - 462●
2.0 * img.data[y * w + (x + 1)].to_double() - - 463●
img.data[(y - 1) * w + (x + 1)].to_double() - 464●
gy = img.data[(y - 1) * w + (x + 1)].to_double() + - 465●
2.0 * img.data[(y - 1) * w + x].to_double() + - 466●
img.data[(y - 1) * w + (x - 1)].to_double() - - 467●
img.data[(y + 1) * w + (x + 1)].to_double() - - 468●
2.0 * img.data[(y + 1) * w + x].to_double() - - 469●
img.data[(y + 1) * w + (x - 1)].to_double() - 470
} - 471●
let edge = (gx * gx + gy * gy).sqrt() - 472
- 473
// 色彩量化(减少色阶) - 474
let levels = 8.0 - 475●
let quantized = (center / 255.0 * levels).to_int().to_double() / - 476
levels * - 477
255.0 - 478
- 479
// 混合:边缘变暗,量化色彩 - 480●
let edge_factor = if edge > 30.0 { strength } else { 0.0 } - 481
let result = quantized * (1.0 - edge_factor) + 0.0 * edge_factor - 482
let clamped = if result < 0.0 { - 483○
0.0 - 484●
} else if result > 255.0 { - 485○
255.0 - 486
} else { - 487●
result - 488
} - 489●
out.push(clamped.to_int().to_byte()) - 490
} - 491
} - 492
- 493
@types.Image::{ - 494
width: w, - 495
height: h, - 496
channels: img.channels, - 497●
data: Bytes::from_array(out), - 498
} - 499
} - 500
- 501
///| - 502
/// 油画效果 - 503
pub fn oil_painting( - 504
img : @types.Image, - 505
radius : Int32, - 506
intensity_levels : Int32, - 507
) -> @types.Image raise @types.LoadError { - 508
// 输入验证 - 509●
if img.width <= 0 || img.height <= 0 { - 510●
raise @types.LoadError::DecodeFailed( - 511
"oil_painting: 图像尺寸必须大于0", - 512
) - 513
} - 514
if img.channels < 1 || img.channels > 4 { - 515●
raise @types.LoadError::DecodeFailed( - 516
"oil_painting: 通道数必须在1-4之间", - 517
) - 518
} - 519
let expected_size = img.width * img.height * img.channels - 520●
if img.data.length() != expected_size { - 521●
raise @types.LoadError::DecodeFailed("oil_painting: 数据大小不匹配") - 522
} - 523
let w = img.width - 524
let h = img.height - 525
let _ = w * h - 526
let out : Array[Byte] = [] - 527
- 528
for y in 0..<h { - 529●
for x in 0..<w { - 530
// 统计邻域内的强度直方图 - 531●
let histogram : Array[Int32] = [] - 532
let sum_histogram : Array[Double] = [] - 533
for _ in 0..<intensity_levels { - 534●
histogram.push(0) - 535●
sum_histogram.push(0.0) - 536
} - 537
- 538
for dy in -radius..<(radius + 1) { - 539●
for dx in -radius..<(radius + 1) { - 540●
let nx = x + dx - 541
let ny = y + dy - 542
if nx >= 0 && nx < w && ny >= 0 && ny < h { - 543●
let pixel = img.data[ny * w + nx].to_double() - 544●
let level = (pixel / 255.0 * intensity_levels.to_double()).to_int() - 545
let clamped_level = if level < 0 { - 546○
0 - 547●
} else if level >= intensity_levels { - 548○
intensity_levels - 1 - 549
} else { - 550●
level - 551
} - 552
histogram[clamped_level] += 1 - 553
sum_histogram[clamped_level] += pixel - 554
} - 555
} - 556
} - 557
- 558
// 找到最频繁的强度级别 - 559
let mut max_count = 0 - 560
let mut max_level = 0 - 561
for i in 0..<intensity_levels { - 562●
if histogram[i] > max_count { - 563●
max_count = histogram[i] - 564
max_level = i - 565
} - 566
} - 567
- 568
// 输出该级别的平均值 - 569
let result = if max_count > 0 { - 570●
sum_histogram[max_level] / max_count.to_double() - 571
} else { - 572○
img.data[y * w + x].to_double() - 573
} - 574
let clamped = if result < 0.0 { - 575○
0.0 - 576●
} else if result > 255.0 { - 577○
255.0 - 578
} else { - 579●
result - 580
} - 581●
out.push(clamped.to_int().to_byte()) - 582
} - 583
} - 584
- 585
@types.Image::{ - 586
width: w, - 587
height: h, - 588
channels: img.channels, - 589●
data: Bytes::from_array(out), - 590
} - 591
} - 592
- 593
///| - 594
/// 素描效果 - 595
pub fn sketch_effect( - 596
img : @types.Image, - 597
strength : Double, - 598
) -> @types.Image raise @types.LoadError { - 599
// 输入验证 - 600●
if img.width <= 0 || img.height <= 0 { - 601●
raise @types.LoadError::DecodeFailed( - 602
"sketch_effect: 图像尺寸必须大于0", - 603
) - 604
} - 605
if img.channels < 1 || img.channels > 4 { - 606●
raise @types.LoadError::DecodeFailed( - 607
"sketch_effect: 通道数必须在1-4之间", - 608
) - 609
} - 610
let expected_size = img.width * img.height * img.channels - 611●
if img.data.length() != expected_size { - 612●
raise @types.LoadError::DecodeFailed("sketch_effect: 数据大小不匹配") - 613
} - 614
let w = img.width - 615
let h = img.height - 616
let _ = w * h - 617
let out : Array[Byte] = [] - 618
- 619
for y in 0..<h { - 620●
for x in 0..<w { - 621●
let idx = y * w + x - 622●
let _ = img.data[idx].to_double() - 623
- 624
// 边缘检测 - 625
let mut gx = 0.0 - 626
let mut gy = 0.0 - 627
if x > 0 && x < w - 1 && y > 0 && y < h - 1 { - 628●
gx = img.data[(y + 1) * w + (x - 1)].to_double() + - 629●
2.0 * img.data[y * w + (x - 1)].to_double() + - 630●
img.data[(y - 1) * w + (x - 1)].to_double() - - 631●
img.data[(y + 1) * w + (x + 1)].to_double() - - 632●
2.0 * img.data[y * w + (x + 1)].to_double() - - 633●
img.data[(y - 1) * w + (x + 1)].to_double() - 634●
gy = img.data[(y - 1) * w + (x + 1)].to_double() + - 635●
2.0 * img.data[(y - 1) * w + x].to_double() + - 636●
img.data[(y - 1) * w + (x - 1)].to_double() - - 637●
img.data[(y + 1) * w + (x + 1)].to_double() - - 638●
2.0 * img.data[(y + 1) * w + x].to_double() - - 639●
img.data[(y + 1) * w + (x - 1)].to_double() - 640
} - 641●
let edge = (gx * gx + gy * gy).sqrt() - 642
- 643
// 反色:边缘为黑色,背景为白色 - 644
let sketch_value = 255.0 - edge * strength - 645
let clamped = if sketch_value < 0.0 { - 646○
0.0 - 647●
} else if sketch_value > 255.0 { - 648○
255.0 - 649
} else { - 650●
sketch_value - 651
} - 652●
out.push(clamped.to_int().to_byte()) - 653
} - 654
} - 655
- 656
@types.Image::{ - 657
width: w, - 658
height: h, - 659
channels: img.channels, - 660●
data: Bytes::from_array(out), - 661
} - 662
} - 663
- 664
///| - 665
/// 风格迁移处理 - 666
pub fn style_transfer( - 667
content : @types.Image, - 668
style : @types.Image, - 669
config : StyleTransferConfig, - 670
) -> StyleTransferResult raise @types.LoadError { - 671
// 输入验证:content - 672●
if content.width <= 0 || content.height <= 0 { - 673●
raise @types.LoadError::DecodeFailed( - 674
"style_transfer: 图像尺寸必须大于0", - 675
) - 676
} - 677
if content.channels < 1 || content.channels > 4 { - 678●
raise @types.LoadError::DecodeFailed( - 679
"style_transfer: 通道数必须在1-4之间", - 680
) - 681
} - 682
let content_expected = content.width * content.height * content.channels - 683●
if content.data.length() != content_expected { - 684●
raise @types.LoadError::DecodeFailed( - 685
"style_transfer: 数据大小不匹配", - 686
) - 687
} - 688
// 输入验证:style - 689
if style.width <= 0 || style.height <= 0 { - 690●
raise @types.LoadError::DecodeFailed( - 691
"style_transfer: 图像尺寸必须大于0", - 692
) - 693
} - 694
if style.channels < 1 || style.channels > 4 { - 695●
raise @types.LoadError::DecodeFailed( - 696
"style_transfer: 通道数必须在1-4之间", - 697
) - 698
} - 699
let style_expected = style.width * style.height * style.channels - 700●
if style.data.length() != style_expected { - 701●
raise @types.LoadError::DecodeFailed( - 702
"style_transfer: 数据大小不匹配", - 703
) - 704
} - 705
let result = if config.algorithm == StyleTransferAlgorithm::ColorTransfer { - 706●
color_transfer(content, style, config.style_strength) - 707●
} else if config.algorithm == StyleTransferAlgorithm::Cartoon { - 708●
cartoonize(content, config.style_strength) - 709●
} else if config.algorithm == StyleTransferAlgorithm::OilPainting { - 710●
oil_painting(content, 3, 8) - 711●
} else if config.algorithm == StyleTransferAlgorithm::Sketch { - 712●
sketch_effect(content, config.style_strength) - 713
} else { - 714●
color_transfer(content, style, config.style_strength) - 715
} - 716
- 717
StyleTransferResult::{ - 718
image: result, - 719
content_image_path: "", - 720
style_image_path: "", - 721
algorithm: config.algorithm, - 722
style_strength: config.style_strength, - 723
content_strength: config.content_strength, - 724
processing_time_ms: 0.0, - 725
iterations: config.iterations, - 726
} - 727
} - 728
- 729
///| - 730
/// 生成风格迁移报告 - 731
pub fn style_transfer_report_to_string( - 732
result : StyleTransferResult, - 733
content_stats : ColorStatistics, - 734
style_stats : ColorStatistics, - 735
texture : StyleTransferTextureFeatures, - 736
) -> String { - 737●
let mut report = "## 图像风格迁移报告\n\n" - 738
report = report + "### 处理信息\n\n" - 739
report = report + "| 指标 | 值 |\n|------|------|\n" - 740
let algo_str = if result.algorithm == StyleTransferAlgorithm::ColorTransfer { - 741●
"颜色转移" - 742●
} else if result.algorithm == StyleTransferAlgorithm::TextureSynthesis { - 743○
"纹理合成" - 744●
} else if result.algorithm == StyleTransferAlgorithm::NeuralStyle { - 745●
"神经风格" - 746●
} else if result.algorithm == StyleTransferAlgorithm::FastStyle { - 747○
"快速风格" - 748●
} else if result.algorithm == StyleTransferAlgorithm::CycleGAN { - 749○
"CycleGAN" - 750●
} else if result.algorithm == StyleTransferAlgorithm::AdaIN { - 751○
"AdaIN" - 752●
} else if result.algorithm == StyleTransferAlgorithm::Cartoon { - 753●
"卡通化" - 754●
} else if result.algorithm == StyleTransferAlgorithm::OilPainting { - 755●
"油画" - 756●
} else if result.algorithm == StyleTransferAlgorithm::Watercolor { - 757○
"水彩" - 758●
} else if result.algorithm == StyleTransferAlgorithm::Sketch { - 759●
"素描" - 760
} else { - 761○
"未知" - 762
} - 763
report = report + "| 算法 | " + algo_str + " |\n" - 764
report = report + - 765
"| 风格强度 | " + - 766●
result.style_strength.to_string() + - 767
" |\n" - 768
report = report + - 769
"| 内容强度 | " + - 770●
result.content_strength.to_string() + - 771
" |\n" - 772●
report = report + "| 迭代次数 | " + result.iterations.to_string() + " |\n" - 773
report = report + - 774
"| 处理耗时 | " + - 775●
result.processing_time_ms.to_string() + - 776
"ms |\n\n" - 777
- 778
report = report + "### 颜色统计\n\n" - 779
report = report + - 780
"| 指标 | 内容图 | 风格图 |\n|------|--------|--------|\n" - 781
report = report + - 782
"| 均值 | " + - 783●
content_stats.mean_r.to_string() + - 784
" | " + - 785●
style_stats.mean_r.to_string() + - 786
" |\n" - 787
report = report + - 788
"| 标准差 | " + - 789●
content_stats.std_r.to_string() + - 790
" | " + - 791●
style_stats.std_r.to_string() + - 792
" |\n" - 793
report = report + - 794
"| 最小值 | " + - 795●
content_stats.min_r.to_string() + - 796
" | " + - 797●
style_stats.min_r.to_string() + - 798
" |\n" - 799
report = report + - 800
"| 最大值 | " + - 801●
content_stats.max_r.to_string() + - 802
" | " + - 803●
style_stats.max_r.to_string() + - 804
" |\n\n" - 805
- 806
report = report + "### 纹理特征\n\n" - 807
report = report + "| 指标 | 值 |\n|------|------|\n" - 808●
report = report + "| 对比度 | " + texture.contrast.to_string() + " |\n" - 809●
report = report + "| 同质性 | " + texture.homogeneity.to_string() + " |\n" - 810●
report = report + "| 能量 | " + texture.energy.to_string() + " |\n" - 811●
report = report + "| 相关性 | " + texture.correlation.to_string() + " |\n" - 812●
report = report + "| 熵 | " + texture.entropy.to_string() + " |\n" - 813●
report = report + "| 粗糙度 | " + texture.coarseness.to_string() + " |\n" - 814
- 815
report - 816
}