image.photography\style_transfer\style_transfer_enhancement.mbt

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