1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
// 图像风格迁移增强(v24.0.0)
// 图像风格迁移、艺术风格化、颜色转移、纹理合成
///|
/// 风格迁移算法类型
pub(all) enum StyleTransferAlgorithm {
ColorTransfer
TextureSynthesis
NeuralStyle
FastStyle
CycleGAN
AdaIN
Cartoon
OilPainting
Watercolor
Sketch
Unknown
} derive(Eq)
///|
/// 风格迁移结果
pub(all) struct StyleTransferResult {
image : @types.Image
content_image_path : String
style_image_path : String
algorithm : StyleTransferAlgorithm
style_strength : Double
content_strength : Double
processing_time_ms : Double
iterations : Int
}
///|
/// 风格迁移配置
pub(all) struct StyleTransferConfig {
algorithm : StyleTransferAlgorithm
style_strength : Double
content_strength : Double
iterations : Int
learning_rate : Double
use_color_preservation : Bool
resolution : Int
}
///|
/// 颜色统计信息
pub(all) struct ColorStatistics {
mean_r : Double
mean_g : Double
mean_b : Double
std_r : Double
std_g : Double
std_b : Double
min_r : Double
min_g : Double
min_b : Double
max_r : Double
max_g : Double
max_b : Double
}
///|
/// 纹理特征
pub(all) struct StyleTransferTextureFeatures {
contrast : Double
homogeneity : Double
energy : Double
correlation : Double
entropy : Double
coarseness : Double
directionality : Double
regularity : Double
}
///|
/// 艺术风格类型
pub(all) enum ArtStyle {
Impressionist
Cubist
Surrealist
Abstract
PopArt
Minimalist
Baroque
Renaissance
Modern
Contemporary
Unknown
} derive(Eq)
///|
/// 创建默认风格迁移配置
pub fn create_default_style_transfer_config() -> StyleTransferConfig {
StyleTransferConfig::{
algorithm: StyleTransferAlgorithm::ColorTransfer,
style_strength: 0.5,
content_strength: 0.5,
iterations: 100,
learning_rate: 0.01,
use_color_preservation: true,
resolution: 512,
}
}
///|
/// 计算颜色统计信息
pub fn calculate_color_statistics(img : @types.Image) -> ColorStatistics {
let w = img.width
let h = img.height
let total = w * h
let mut sum_r = 0.0
let mut sum_g = 0.0
let mut sum_b = 0.0
let mut min_r = 255.0
let mut min_g = 255.0
let mut min_b = 255.0
let mut max_r = 0.0
let mut max_g = 0.0
let mut max_b = 0.0
for i in 0..<total {
let pixel = img.data[i].to_double()
// 简化:单通道灰度
sum_r += pixel
sum_g += pixel
sum_b += pixel
if pixel < min_r {
min_r = pixel
}
if pixel < min_g {
min_g = pixel
}
if pixel < min_b {
min_b = pixel
}
if pixel > max_r {
max_r = pixel
}
if pixel > max_g {
max_g = pixel
}
if pixel > max_b {
max_b = pixel
}
}
let mean_r = sum_r / total.to_double()
let mean_g = sum_g / total.to_double()
let mean_b = sum_b / total.to_double()
let mut var_r = 0.0
let mut var_g = 0.0
let mut var_b = 0.0
for i in 0..<total {
let pixel = img.data[i].to_double()
var_r += (pixel - mean_r) * (pixel - mean_r)
var_g += (pixel - mean_g) * (pixel - mean_g)
var_b += (pixel - mean_b) * (pixel - mean_b)
}
var_r /= total.to_double()
var_g /= total.to_double()
var_b /= total.to_double()
ColorStatistics::{
mean_r,
mean_g,
mean_b,
std_r: var_r.sqrt(),
std_g: var_g.sqrt(),
std_b: var_b.sqrt(),
min_r,
min_g,
min_b,
max_r,
max_g,
max_b,
}
}
///|
/// 颜色转移(Reinhard 方法)
pub fn color_transfer(
content : @types.Image,
style : @types.Image,
strength : Double,
) -> @types.Image {
let content_stats = calculate_color_statistics(content)
let style_stats = calculate_color_statistics(style)
let w = content.width
let h = content.height
let total = w * h
let out : Array[Byte] = []
for i in 0..<total {
let pixel = content.data[i].to_double()
// 标准化
let normalized = if content_stats.std_r > 0.0 {
(pixel - content_stats.mean_r) / content_stats.std_r
} else {
0.0
}
// 转移到风格分布
let transferred = normalized * style_stats.std_r + style_stats.mean_r
// 混合
let blended = pixel * (1.0 - strength) + transferred * strength
let clamped = if blended < 0.0 {
0.0
} else if blended > 255.0 {
255.0
} else {
blended
}
out.push(clamped.to_int().to_byte())
}
@types.Image::{
width: w,
height: h,
channels: content.channels,
data: Bytes::from_array(out),
}
}
///|
/// 计算纹理特征(GLCM 简化版)
pub fn calculate_texture_features(
img : @types.Image,
) -> StyleTransferTextureFeatures {
let w = img.width
let h = img.height
let _ = w * h
// 计算灰度共生矩阵(简化:水平方向)
let glcm_size = 16
let glcm : Array[Double] = []
for _ in 0..<(glcm_size * glcm_size) {
glcm.push(0.0)
}
for y in 0..<h {
for x in 0..<(w - 1) {
let i = img.data[y * w + x].to_int() * glcm_size / 256 % glcm_size
let j = img.data[y * w + x + 1].to_int() * glcm_size / 256 % glcm_size
glcm[i * glcm_size + j] += 1.0
}
}
// 归一化
let glcm_total = (w - 1) * h
if glcm_total > 0 {
for i in 0..<(glcm_size * glcm_size) {
glcm[i] /= glcm_total.to_double()
}
}
// 计算特征
let mut contrast = 0.0
let mut homogeneity = 0.0
let mut energy = 0.0
let mut correlation = 0.0
let mut entropy = 0.0
let mut mean_i = 0.0
let mut mean_j = 0.0
for i in 0..<glcm_size {
for j in 0..<glcm_size {
let p = glcm[i * glcm_size + j]
mean_i += i.to_double() * p
mean_j += j.to_double() * p
}
}
for i in 0..<glcm_size {
for j in 0..<glcm_size {
let p = glcm[i * glcm_size + j]
let diff = (i - j).to_double()
contrast += diff * diff * p
homogeneity += p / (1.0 + diff * diff)
energy += p * p
correlation += (i.to_double() - mean_i) * (j.to_double() - mean_j) * p
if p > 0.0 {
entropy -= p * p * 0.1
}
}
}
StyleTransferTextureFeatures::{
contrast,
homogeneity,
energy,
correlation,
entropy,
coarseness: 1.0 / (1.0 + contrast),
directionality: 0.5,
regularity: homogeneity * energy,
}
}
///|
/// 卡通化效果
pub fn cartoonize(img : @types.Image, strength : Double) -> @types.Image {
let w = img.width
let h = img.height
let _ = w * h
let out : Array[Byte] = []
// 简化:边缘检测 + 色彩量化
for y in 0..<h {
for x in 0..<w {
let idx = y * w + x
let center = img.data[idx].to_double()
// 边缘检测(Sobel 简化)
let mut gx = 0.0
let mut gy = 0.0
if x > 0 && x < w - 1 && y > 0 && y < h - 1 {
gx = img.data[(y + 1) * w + (x - 1)].to_double() +
2.0 * img.data[y * w + (x - 1)].to_double() +
img.data[(y - 1) * w + (x - 1)].to_double() -
img.data[(y + 1) * w + (x + 1)].to_double() -
2.0 * img.data[y * w + (x + 1)].to_double() -
img.data[(y - 1) * w + (x + 1)].to_double()
gy = img.data[(y - 1) * w + (x + 1)].to_double() +
2.0 * img.data[(y - 1) * w + x].to_double() +
img.data[(y - 1) * w + (x - 1)].to_double() -
img.data[(y + 1) * w + (x + 1)].to_double() -
2.0 * img.data[(y + 1) * w + x].to_double() -
img.data[(y + 1) * w + (x - 1)].to_double()
}
let edge = (gx * gx + gy * gy).sqrt()
// 色彩量化(减少色阶)
let levels = 8.0
let quantized = (center / 255.0 * levels).to_int().to_double() /
levels *
255.0
// 混合:边缘变暗,量化色彩
let edge_factor = if edge > 30.0 { strength } else { 0.0 }
let result = quantized * (1.0 - edge_factor) + 0.0 * edge_factor
let clamped = if result < 0.0 {
0.0
} else if result > 255.0 {
255.0
} else {
result
}
out.push(clamped.to_int().to_byte())
}
}
@types.Image::{
width: w,
height: h,
channels: img.channels,
data: Bytes::from_array(out),
}
}
///|
/// 油画效果
pub fn oil_painting(
img : @types.Image,
radius : Int,
intensity_levels : Int,
) -> @types.Image {
let w = img.width
let h = img.height
let _ = w * h
let out : Array[Byte] = []
for y in 0..<h {
for x in 0..<w {
// 统计邻域内的强度直方图
let histogram : Array[Int] = []
let sum_histogram : Array[Double] = []
for _ in 0..<intensity_levels {
histogram.push(0)
sum_histogram.push(0.0)
}
for dy in -radius..<(radius + 1) {
for dx in -radius..<(radius + 1) {
let nx = x + dx
let ny = y + dy
if nx >= 0 && nx < w && ny >= 0 && ny < h {
let pixel = img.data[ny * w + nx].to_double()
let level = (pixel / 255.0 * intensity_levels.to_double()).to_int()
let clamped_level = if level < 0 {
0
} else if level >= intensity_levels {
intensity_levels - 1
} else {
level
}
histogram[clamped_level] += 1
sum_histogram[clamped_level] += pixel
}
}
}
// 找到最频繁的强度级别
let mut max_count = 0
let mut max_level = 0
for i in 0..<intensity_levels {
if histogram[i] > max_count {
max_count = histogram[i]
max_level = i
}
}
// 输出该级别的平均值
let result = if max_count > 0 {
sum_histogram[max_level] / max_count.to_double()
} else {
img.data[y * w + x].to_double()
}
let clamped = if result < 0.0 {
0.0
} else if result > 255.0 {
255.0
} else {
result
}
out.push(clamped.to_int().to_byte())
}
}
@types.Image::{
width: w,
height: h,
channels: img.channels,
data: Bytes::from_array(out),
}
}
///|
/// 素描效果
pub fn sketch_effect(img : @types.Image, strength : Double) -> @types.Image {
let w = img.width
let h = img.height
let _ = w * h
let out : Array[Byte] = []
for y in 0..<h {
for x in 0..<w {
let idx = y * w + x
let _ = img.data[idx].to_double()
// 边缘检测
let mut gx = 0.0
let mut gy = 0.0
if x > 0 && x < w - 1 && y > 0 && y < h - 1 {
gx = img.data[(y + 1) * w + (x - 1)].to_double() +
2.0 * img.data[y * w + (x - 1)].to_double() +
img.data[(y - 1) * w + (x - 1)].to_double() -
img.data[(y + 1) * w + (x + 1)].to_double() -
2.0 * img.data[y * w + (x + 1)].to_double() -
img.data[(y - 1) * w + (x + 1)].to_double()
gy = img.data[(y - 1) * w + (x + 1)].to_double() +
2.0 * img.data[(y - 1) * w + x].to_double() +
img.data[(y - 1) * w + (x - 1)].to_double() -
img.data[(y + 1) * w + (x + 1)].to_double() -
2.0 * img.data[(y + 1) * w + x].to_double() -
img.data[(y + 1) * w + (x - 1)].to_double()
}
let edge = (gx * gx + gy * gy).sqrt()
// 反色:边缘为黑色,背景为白色
let sketch_value = 255.0 - edge * strength
let clamped = if sketch_value < 0.0 {
0.0
} else if sketch_value > 255.0 {
255.0
} else {
sketch_value
}
out.push(clamped.to_int().to_byte())
}
}
@types.Image::{
width: w,
height: h,
channels: img.channels,
data: Bytes::from_array(out),
}
}
///|
/// 风格迁移处理
pub fn style_transfer(
content : @types.Image,
style : @types.Image,
config : StyleTransferConfig,
) -> StyleTransferResult {
let result = if config.algorithm == StyleTransferAlgorithm::ColorTransfer {
color_transfer(content, style, config.style_strength)
} else if config.algorithm == StyleTransferAlgorithm::Cartoon {
cartoonize(content, config.style_strength)
} else if config.algorithm == StyleTransferAlgorithm::OilPainting {
oil_painting(content, 3, 8)
} else if config.algorithm == StyleTransferAlgorithm::Sketch {
sketch_effect(content, config.style_strength)
} else {
color_transfer(content, style, config.style_strength)
}
StyleTransferResult::{
image: result,
content_image_path: "",
style_image_path: "",
algorithm: config.algorithm,
style_strength: config.style_strength,
content_strength: config.content_strength,
processing_time_ms: 0.0,
iterations: config.iterations,
}
}
///|
/// 生成风格迁移报告
pub fn style_transfer_report_to_string(
result : StyleTransferResult,
content_stats : ColorStatistics,
style_stats : ColorStatistics,
texture : StyleTransferTextureFeatures,
) -> String {
let mut report = "## 图像风格迁移报告\n\n"
report = report + "### 处理信息\n\n"
report = report + "| 指标 | 值 |\n|------|------|\n"
let algo_str = if result.algorithm == StyleTransferAlgorithm::ColorTransfer {
"颜色转移"
} else if result.algorithm == StyleTransferAlgorithm::TextureSynthesis {
"纹理合成"
} else if result.algorithm == StyleTransferAlgorithm::NeuralStyle {
"神经风格"
} else if result.algorithm == StyleTransferAlgorithm::FastStyle {
"快速风格"
} else if result.algorithm == StyleTransferAlgorithm::CycleGAN {
"CycleGAN"
} else if result.algorithm == StyleTransferAlgorithm::AdaIN {
"AdaIN"
} else if result.algorithm == StyleTransferAlgorithm::Cartoon {
"卡通化"
} else if result.algorithm == StyleTransferAlgorithm::OilPainting {
"油画"
} else if result.algorithm == StyleTransferAlgorithm::Watercolor {
"水彩"
} else if result.algorithm == StyleTransferAlgorithm::Sketch {
"素描"
} else {
"未知"
}
report = report + "| 算法 | " + algo_str + " |\n"
report = report +
"| 风格强度 | " +
result.style_strength.to_string() +
" |\n"
report = report +
"| 内容强度 | " +
result.content_strength.to_string() +
" |\n"
report = report + "| 迭代次数 | " + result.iterations.to_string() + " |\n"
report = report +
"| 处理耗时 | " +
result.processing_time_ms.to_string() +
"ms |\n\n"
report = report + "### 颜色统计\n\n"
report = report +
"| 指标 | 内容图 | 风格图 |\n|------|--------|--------|\n"
report = report +
"| 均值 | " +
content_stats.mean_r.to_string() +
" | " +
style_stats.mean_r.to_string() +
" |\n"
report = report +
"| 标准差 | " +
content_stats.std_r.to_string() +
" | " +
style_stats.std_r.to_string() +
" |\n"
report = report +
"| 最小值 | " +
content_stats.min_r.to_string() +
" | " +
style_stats.min_r.to_string() +
" |\n"
report = report +
"| 最大值 | " +
content_stats.max_r.to_string() +
" | " +
style_stats.max_r.to_string() +
" |\n\n"
report = report + "### 纹理特征\n\n"
report = report + "| 指标 | 值 |\n|------|------|\n"
report = report + "| 对比度 | " + texture.contrast.to_string() + " |\n"
report = report + "| 同质性 | " + texture.homogeneity.to_string() + " |\n"
report = report + "| 能量 | " + texture.energy.to_string() + " |\n"
report = report + "| 相关性 | " + texture.correlation.to_string() + " |\n"
report = report + "| 熵 | " + texture.entropy.to_string() + " |\n"
report = report + "| 粗糙度 | " + texture.coarseness.to_string() + " |\n"
report
}