image.vision\ocr\projection_analysis.mbt

95.2% · 178 / 187 points · 645 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/// # 投影分析 ocr/projection_analysis
  3. 3///
  4. 4/// 图像处理库子模块
  5. 5/// 图像处理库子模块 Image processing library submodule
  6. 6// 投影分析与文本行分割
  7. 7// 水平/垂直投影、文本行检测、字符分割
  8. 8
  9. 9///|
  10. 10/// 投影结果结构体
  11. 11#derive(Eq, @debug.Debug)
  12. 12pub(all) struct ProjectionResult {
  13. 13 projection : Array[Int32]
  14. 14 peaks : Array[Int32]
  15. 15 valleys : Array[Int32]
  16. 16 mean : Double
  17. 17 stddev : Double
  18. 18}
  19. 19
  20. 20///|
  21. 21pub extend ProjectionResult with Eq::{not_equal, equal}
  22. 22
  23. 23///|
  24. 24pub extend ProjectionResult with @debug.Debug::{to_repr}
  25. 25
  26. 26///|
  27. 27/// 水平投影(按行统计前景像素数)
  28. 28pub fn horizontal_projection(
  29. 29 img : @types.Image,
  30. 30) -> ProjectionResult raise @types.LoadError {
  31. 31 // 输入验证
  32. 32● if img.width <= 0 || img.height <= 0 {
  33. 33● raise @types.LoadError::DecodeFailed(
  34. 34 "horizontal_projection: 图像尺寸必须大于0",
  35. 35 )
  36. 36 }
  37. 37 if img.channels != 1 {
  38. 38● raise @types.LoadError::DecodeFailed(
  39. 39 "horizontal_projection: 仅支持单通道二值图",
  40. 40 )
  41. 41 }
  42. 42 let expected_size = img.width * img.height
  43. 43● if img.data.length() != expected_size {
  44. 44● raise @types.LoadError::DecodeFailed(
  45. 45 "horizontal_projection: 数据大小不匹配,期望 " +
  46. 46● expected_size.to_string() +
  47. 47 ",实际 " +
  48. 48● img.data.length().to_string(),
  49. 49 )
  50. 50 }
  51. 51
  52. 52 let w = img.width
  53. 53 let h = img.height
  54. 54 let data = img.data
  55. 55● let projection : Array[Int32] = Array::make(h, 0)
  56. 56 for y in 0..<h {
  57. 57● for x in 0..<w {
  58. 58● if data[y * w + x].to_int() > 128 {
  59. 59● projection[y] += 1
  60. 60 }
  61. 61 }
  62. 62 }
  63. 63● analyze_projection(projection)
  64. 64}
  65. 65
  66. 66///|
  67. 67/// 垂直投影(按列统计前景像素数)
  68. 68pub fn vertical_projection(
  69. 69 img : @types.Image,
  70. 70) -> ProjectionResult raise @types.LoadError {
  71. 71 // 输入验证
  72. 72● if img.width <= 0 || img.height <= 0 {
  73. 73● raise @types.LoadError::DecodeFailed(
  74. 74 "vertical_projection: 图像尺寸必须大于0",
  75. 75 )
  76. 76 }
  77. 77 if img.channels != 1 {
  78. 78● raise @types.LoadError::DecodeFailed(
  79. 79 "vertical_projection: 仅支持单通道二值图",
  80. 80 )
  81. 81 }
  82. 82 let expected_size = img.width * img.height
  83. 83● if img.data.length() != expected_size {
  84. 84● raise @types.LoadError::DecodeFailed(
  85. 85 "vertical_projection: 数据大小不匹配,期望 " +
  86. 86● expected_size.to_string() +
  87. 87 ",实际 " +
  88. 88● img.data.length().to_string(),
  89. 89 )
  90. 90 }
  91. 91
  92. 92 let w = img.width
  93. 93 let h = img.height
  94. 94 let data = img.data
  95. 95● let projection : Array[Int32] = Array::make(w, 0)
  96. 96 for y in 0..<h {
  97. 97● for x in 0..<w {
  98. 98● if data[y * w + x].to_int() > 128 {
  99. 99● projection[x] += 1
  100. 100 }
  101. 101 }
  102. 102 }
  103. 103● analyze_projection(projection)
  104. 104}
  105. 105
  106. 106///|
  107. 107/// 分析投影(找峰值、谷值、均值、标准差)
  108. 108fn analyze_projection(projection : Array[Int32]) -> ProjectionResult {
  109. 109● let n = projection.length()
  110. 110 // 计算均值和标准差
  111. 111 let mut sum = 0
  112. 112 let mut sum_sq = 0
  113. 113 for i in 0..<n {
  114. 114● sum += projection[i]
  115. 115 sum_sq += projection[i] * projection[i]
  116. 116 }
  117. 117◐ let mean = if n > 0 { sum.to_double() / n.to_double() } else { 0.0 }
  118. 118 let variance = if n > 0 {
  119. 119● sum_sq.to_double() / n.to_double() - mean * mean
  120. 120 } else {
  121. 121○ 0.0
  122. 122 }
  123. 123● let stddev = if variance > 0.0 { variance.sqrt() } else { 0.0 }
  124. 124 // 找峰值(局部最大值)
  125. 125 let peaks : Array[Int32] = []
  126. 126 for i in 1..<(n - 1) {
  127. 127● if projection[i] > projection[i - 1] &&
  128. 128 projection[i] >= projection[i + 1] &&
  129. 129● projection[i].to_double() > mean {
  130. 130● peaks.push(i)
  131. 131 }
  132. 132 }
  133. 133 // 找谷值(局部最小值)
  134. 134 let valleys : Array[Int32] = []
  135. 135 for i in 1..<(n - 1) {
  136. 136● if projection[i] < projection[i - 1] &&
  137. 137 projection[i] <= projection[i + 1] &&
  138. 138● projection[i].to_double() < mean {
  139. 139● valleys.push(i)
  140. 140 }
  141. 141 }
  142. 142 ProjectionResult::{ projection, peaks, valleys, mean, stddev, }
  143. 143}
  144. 144
  145. 145///|
  146. 146/// 文本行结构体
  147. 147#derive(Eq, @debug.Debug)
  148. 148pub(all) struct TextLineInfo {
  149. 149 y_start : Int32
  150. 150 y_end : Int32
  151. 151 height : Int32
  152. 152 x_start : Int32
  153. 153 x_end : Int32
  154. 154 width : Int32
  155. 155 pixel_count : Int32
  156. 156 baseline : Int32
  157. 157}
  158. 158
  159. 159///|
  160. 160pub extend TextLineInfo with Eq::{not_equal, equal}
  161. 161
  162. 162///|
  163. 163pub extend TextLineInfo with @debug.Debug::{to_repr}
  164. 164
  165. 165///|
  166. 166/// 检测文本行(基于水平投影)
  167. 167pub fn detect_text_lines_projection(
  168. 168 img : @types.Image,
  169. 169 min_height~ : Int32 = 5,
  170. 170 max_gap~ : Int32 = 3,
  171. 171) -> Array[TextLineInfo] raise @types.LoadError {
  172. 172 // 输入验证
  173. 173● if img.width <= 0 || img.height <= 0 {
  174. 174● raise @types.LoadError::DecodeFailed(
  175. 175 "detect_text_lines_projection: 图像尺寸必须大于0",
  176. 176 )
  177. 177 }
  178. 178 if img.channels != 1 {
  179. 179● raise @types.LoadError::DecodeFailed(
  180. 180 "detect_text_lines_projection: 仅支持单通道二值图",
  181. 181 )
  182. 182 }
  183. 183 let expected_size = img.width * img.height
  184. 184● if img.data.length() != expected_size {
  185. 185● raise @types.LoadError::DecodeFailed(
  186. 186 "detect_text_lines_projection: 数据大小不匹配,期望 " +
  187. 187● expected_size.to_string() +
  188. 188 ",实际 " +
  189. 189● img.data.length().to_string(),
  190. 190 )
  191. 191 }
  192. 192 if min_height <= 0 {
  193. 193● raise @types.LoadError::DecodeFailed(
  194. 194 "detect_text_lines_projection: min_height必须大于0",
  195. 195 )
  196. 196 }
  197. 197 if max_gap < 0 {
  198. 198● raise @types.LoadError::DecodeFailed(
  199. 199 "detect_text_lines_projection: max_gap不能为负数",
  200. 200 )
  201. 201 }
  202. 202
  203. 203 let w = img.width
  204. 204 let h = img.height
  205. 205 let data = img.data
  206. 206 // 水平投影
  207. 207● let projection : Array[Int32] = Array::make(h, 0)
  208. 208 for y in 0..<h {
  209. 209● for x in 0..<w {
  210. 210● if data[y * w + x].to_int() > 128 {
  211. 211● projection[y] += 1
  212. 212 }
  213. 213 }
  214. 214 }
  215. 215 // 找文本行(连续的非零行)
  216. 216 let lines : Array[TextLineInfo] = []
  217. 217 let mut in_line = false
  218. 218 let mut start_y = 0
  219. 219 let mut gap_count = 0
  220. 220 for y in 0..<h {
  221. 221● if projection[y] > 0 {
  222. 222● if !in_line {
  223. 223● in_line = true
  224. 224 start_y = y
  225. 225 }
  226. 226 gap_count = 0
  227. 227● } else if in_line {
  228. 228● gap_count += 1
  229. 229 if gap_count > max_gap {
  230. 230 // 结束当前行
  231. 231● let end_y = y - gap_count
  232. 232 let line_height = end_y - start_y + 1
  233. 233 if line_height >= min_height {
  234. 234 // 计算这一行的水平范围和像素数
  235. 235● let mut min_x = w
  236. 236 let mut max_x = 0
  237. 237 let mut pixel_count = 0
  238. 238 let mut baseline_y = start_y
  239. 239 let mut max_proj = 0
  240. 240 for yy in start_y..<(end_y + 1) {
  241. 241● if projection[yy] > max_proj {
  242. 242● max_proj = projection[yy]
  243. 243 baseline_y = yy
  244. 244 }
  245. 245 for x in 0..<w {
  246. 246● if data[yy * w + x].to_int() > 128 {
  247. 247● pixel_count += 1
  248. 248 if x < min_x {
  249. 249● min_x = x
  250. 250 }
  251. 251 if x > max_x {
  252. 252● max_x = x
  253. 253 }
  254. 254 }
  255. 255 }
  256. 256 }
  257. 257 if max_x > min_x {
  258. 258● lines.push(TextLineInfo::{
  259. 259 y_start: start_y,
  260. 260 y_end: end_y,
  261. 261 height: line_height,
  262. 262 x_start: min_x,
  263. 263 x_end: max_x,
  264. 264 width: max_x - min_x + 1,
  265. 265 pixel_count,
  266. 266 baseline: baseline_y,
  267. 267 })
  268. 268 }
  269. 269 }
  270. 270 in_line = false
  271. 271 gap_count = 0
  272. 272 }
  273. 273 }
  274. 274 }
  275. 275 // 处理最后一行
  276. 276 if in_line {
  277. 277● let end_y = h - 1
  278. 278 let line_height = end_y - start_y + 1
  279. 279 if line_height >= min_height {
  280. 280● let mut min_x = w
  281. 281 let mut max_x = 0
  282. 282 let mut pixel_count = 0
  283. 283 for yy in start_y..<(end_y + 1) {
  284. 284● for x in 0..<w {
  285. 285● if data[yy * w + x].to_int() > 128 {
  286. 286● pixel_count += 1
  287. 287 if x < min_x {
  288. 288● min_x = x
  289. 289 }
  290. 290 if x > max_x {
  291. 291● max_x = x
  292. 292 }
  293. 293 }
  294. 294 }
  295. 295 }
  296. 296 if max_x > min_x {
  297. 297● lines.push(TextLineInfo::{
  298. 298 y_start: start_y,
  299. 299 y_end: end_y,
  300. 300 height: line_height,
  301. 301 x_start: min_x,
  302. 302 x_end: max_x,
  303. 303 width: max_x - min_x + 1,
  304. 304 pixel_count,
  305. 305 baseline: start_y + line_height / 2,
  306. 306 })
  307. 307 }
  308. 308 }
  309. 309 }
  310. 310 lines
  311. 311}
  312. 312
  313. 313///|
  314. 314/// 字符分割结构体
  315. 315#derive(Eq, @debug.Debug)
  316. 316pub(all) struct CharSegment {
  317. 317 x_start : Int32
  318. 318 x_end : Int32
  319. 319 width : Int32
  320. 320 y_start : Int32
  321. 321 y_end : Int32
  322. 322 height : Int32
  323. 323 pixel_count : Int32
  324. 324}
  325. 325
  326. 326///|
  327. 327pub extend CharSegment with Eq::{not_equal, equal}
  328. 328
  329. 329///|
  330. 330pub extend CharSegment with @debug.Debug::{to_repr}
  331. 331
  332. 332///|
  333. 333/// 文本行字符分割(基于垂直投影)
  334. 334pub fn segment_chars_projection(
  335. 335 img : @types.Image,
  336. 336 line : TextLineInfo,
  337. 337 min_width~ : Int32 = 2,
  338. 338 max_gap~ : Int32 = 2,
  339. 339) -> Array[CharSegment] raise @types.LoadError {
  340. 340 // 输入验证
  341. 341● if img.width <= 0 || img.height <= 0 {
  342. 342● raise @types.LoadError::DecodeFailed(
  343. 343 "segment_chars_projection: 图像尺寸必须大于0",
  344. 344 )
  345. 345 }
  346. 346 if img.channels != 1 {
  347. 347● raise @types.LoadError::DecodeFailed(
  348. 348 "segment_chars_projection: 仅支持单通道二值图",
  349. 349 )
  350. 350 }
  351. 351 let expected_size = img.width * img.height
  352. 352● if img.data.length() != expected_size {
  353. 353● raise @types.LoadError::DecodeFailed(
  354. 354 "segment_chars_projection: 数据大小不匹配,期望 " +
  355. 355● expected_size.to_string() +
  356. 356 ",实际 " +
  357. 357● img.data.length().to_string(),
  358. 358 )
  359. 359 }
  360. 360 if line.width <= 0 || line.height <= 0 {
  361. 361● raise @types.LoadError::DecodeFailed(
  362. 362 "segment_chars_projection: 文本行尺寸必须大于0",
  363. 363 )
  364. 364 }
  365. 365 if min_width <= 0 {
  366. 366● raise @types.LoadError::DecodeFailed(
  367. 367 "segment_chars_projection: min_width必须大于0",
  368. 368 )
  369. 369 }
  370. 370 if max_gap < 0 {
  371. 371● raise @types.LoadError::DecodeFailed(
  372. 372 "segment_chars_projection: max_gap不能为负数",
  373. 373 )
  374. 374 }
  375. 375
  376. 376 let w = img.width
  377. 377 let data = img.data
  378. 378 let y_start = line.y_start
  379. 379 let y_end = line.y_end
  380. 380 let x_start = line.x_start
  381. 381 let x_end = line.x_end
  382. 382 // 垂直投影(只在文本行范围内)
  383. 383● let projection : Array[Int32] = Array::make(w, 0)
  384. 384 for x in x_start..<(x_end + 1) {
  385. 385● for y in y_start..<(y_end + 1) {
  386. 386● if data[y * w + x].to_int() > 128 {
  387. 387● projection[x] += 1
  388. 388 }
  389. 389 }
  390. 390 }
  391. 391 // 找字符(连续的非零列)
  392. 392 let chars : Array[CharSegment] = []
  393. 393 let mut in_char = false
  394. 394 let mut start_x = 0
  395. 395 let mut gap_count = 0
  396. 396 for x in x_start..<(x_end + 1) {
  397. 397● if projection[x] > 0 {
  398. 398● if !in_char {
  399. 399● in_char = true
  400. 400 start_x = x
  401. 401 }
  402. 402 gap_count = 0
  403. 403● } else if in_char {
  404. 404● gap_count += 1
  405. 405 if gap_count > max_gap {
  406. 406● let end_x = x - gap_count
  407. 407 let char_width = end_x - start_x + 1
  408. 408 if char_width >= min_width {
  409. 409 // 计算字符的垂直范围和像素数
  410. 410● let mut min_y = y_end
  411. 411 let mut max_y = y_start
  412. 412 let mut pixel_count = 0
  413. 413 for xx in start_x..<(end_x + 1) {
  414. 414● for yy in y_start..<(y_end + 1) {
  415. 415● if data[yy * w + xx].to_int() > 128 {
  416. 416● pixel_count += 1
  417. 417 if yy < min_y {
  418. 418● min_y = yy
  419. 419 }
  420. 420 if yy > max_y {
  421. 421● max_y = yy
  422. 422 }
  423. 423 }
  424. 424 }
  425. 425 }
  426. 426● chars.push(CharSegment::{
  427. 427 x_start: start_x,
  428. 428 x_end: end_x,
  429. 429 width: char_width,
  430. 430 y_start: min_y,
  431. 431 y_end: max_y,
  432. 432 height: max_y - min_y + 1,
  433. 433 pixel_count,
  434. 434 })
  435. 435 }
  436. 436 in_char = false
  437. 437 gap_count = 0
  438. 438 }
  439. 439 }
  440. 440 }
  441. 441 // 处理最后一个字符
  442. 442 if in_char {
  443. 443● let end_x = x_end
  444. 444 let char_width = end_x - start_x + 1
  445. 445 if char_width >= min_width {
  446. 446● let mut min_y = y_end
  447. 447 let mut max_y = y_start
  448. 448 let mut pixel_count = 0
  449. 449 for xx in start_x..<(end_x + 1) {
  450. 450● for yy in y_start..<(y_end + 1) {
  451. 451● if data[yy * w + xx].to_int() > 128 {
  452. 452● pixel_count += 1
  453. 453 if yy < min_y {
  454. 454● min_y = yy
  455. 455 }
  456. 456 if yy > max_y {
  457. 457● max_y = yy
  458. 458 }
  459. 459 }
  460. 460 }
  461. 461 }
  462. 462● chars.push(CharSegment::{
  463. 463 x_start: start_x,
  464. 464 x_end: end_x,
  465. 465 width: char_width,
  466. 466 y_start: min_y,
  467. 467 y_end: max_y,
  468. 468 height: max_y - min_y + 1,
  469. 469 pixel_count,
  470. 470 })
  471. 471 }
  472. 472 }
  473. 473 chars
  474. 474}
  475. 475
  476. 476///|
  477. 477/// 提取文本行图像
  478. 478pub fn extract_text_line_image(
  479. 479 img : @types.Image,
  480. 480 line : TextLineInfo,
  481. 481 padding~ : Int32 = 2,
  482. 482) -> @types.Image raise @types.LoadError {
  483. 483 // 输入验证
  484. 484● if img.width <= 0 || img.height <= 0 {
  485. 485● raise @types.LoadError::DecodeFailed(
  486. 486 "extract_text_line_image: 图像尺寸必须大于0",
  487. 487 )
  488. 488 }
  489. 489 if img.channels < 1 || img.channels > 4 {
  490. 490● raise @types.LoadError::DecodeFailed(
  491. 491 "extract_text_line_image: 通道数必须在1-4之间",
  492. 492 )
  493. 493 }
  494. 494 let expected_size = img.width * img.height * img.channels
  495. 495● if img.data.length() != expected_size {
  496. 496● raise @types.LoadError::DecodeFailed(
  497. 497 "extract_text_line_image: 数据大小不匹配,期望 " +
  498. 498● expected_size.to_string() +
  499. 499 ",实际 " +
  500. 500● img.data.length().to_string(),
  501. 501 )
  502. 502 }
  503. 503 if line.width <= 0 || line.height <= 0 {
  504. 504● raise @types.LoadError::DecodeFailed(
  505. 505 "extract_text_line_image: 文本行尺寸必须大于0",
  506. 506 )
  507. 507 }
  508. 508 if padding < 0 {
  509. 509● raise @types.LoadError::DecodeFailed(
  510. 510 "extract_text_line_image: padding不能为负数",
  511. 511 )
  512. 512 }
  513. 513
  514. 514 let w = img.width
  515. 515 let h = img.height
  516. 516 let ch = img.channels
  517. 517● let x_start = max(0, line.x_start - padding)
  518. 518● let x_end = min(w - 1, line.x_end + padding)
  519. 519● let y_start = max(0, line.y_start - padding)
  520. 520● let y_end = min(h - 1, line.y_end + padding)
  521. 521 let new_w = x_end - x_start + 1
  522. 522 let new_h = y_end - y_start + 1
  523. 523● let out : Array[Byte] = Array::make(new_w * new_h * ch, b'\x00')
  524. 524 let data = img.data
  525. 525 for y in 0..<new_h {
  526. 526● for x in 0..<new_w {
  527. 527● for c in 0..<ch {
  528. 528● out[(y * new_w + x) * ch + c] = data[((y_start + y) * w + (x_start + x)) *
  529. 529 ch +
  530. 530 c]
  531. 531 }
  532. 532 }
  533. 533 }
  534. 534 @types.Image::{
  535. 535 width: new_w,
  536. 536 height: new_h,
  537. 537 channels: ch,
  538. 538● data: Bytes::from_array(out),
  539. 539 }
  540. 540}
  541. 541
  542. 542///|
  543. 543/// 提取字符图像
  544. 544pub fn extract_char_image(
  545. 545 img : @types.Image,
  546. 546 char : CharSegment,
  547. 547 target_size~ : Int32 = 32,
  548. 548 padding~ : Int32 = 2,
  549. 549) -> @types.Image raise @types.LoadError {
  550. 550 // 输入验证
  551. 551● if img.width <= 0 || img.height <= 0 {
  552. 552● raise @types.LoadError::DecodeFailed(
  553. 553 "extract_char_image: 图像尺寸必须大于0",
  554. 554 )
  555. 555 }
  556. 556 if img.channels < 1 || img.channels > 4 {
  557. 557● raise @types.LoadError::DecodeFailed(
  558. 558 "extract_char_image: 通道数必须在1-4之间",
  559. 559 )
  560. 560 }
  561. 561 let expected_size = img.width * img.height * img.channels
  562. 562● if img.data.length() != expected_size {
  563. 563● raise @types.LoadError::DecodeFailed(
  564. 564 "extract_char_image: 数据大小不匹配,期望 " +
  565. 565● expected_size.to_string() +
  566. 566 ",实际 " +
  567. 567● img.data.length().to_string(),
  568. 568 )
  569. 569 }
  570. 570 if char.width <= 0 || char.height <= 0 {
  571. 571● raise @types.LoadError::DecodeFailed(
  572. 572 "extract_char_image: 字符尺寸必须大于0",
  573. 573 )
  574. 574 }
  575. 575 if target_size <= 0 {
  576. 576● raise @types.LoadError::DecodeFailed(
  577. 577 "extract_char_image: target_size必须大于0",
  578. 578 )
  579. 579 }
  580. 580 if padding < 0 {
  581. 581● raise @types.LoadError::DecodeFailed(
  582. 582 "extract_char_image: padding不能为负数",
  583. 583 )
  584. 584 }
  585. 585
  586. 586 let w = img.width
  587. 587 let h = img.height
  588. 588 let ch = img.channels
  589. 589● let x_start = max(0, char.x_start - padding)
  590. 590● let x_end = min(w - 1, char.x_end + padding)
  591. 591● let y_start = max(0, char.y_start - padding)
  592. 592● let y_end = min(h - 1, char.y_end + padding)
  593. 593 let src_w = x_end - x_start + 1
  594. 594 let src_h = y_end - y_start + 1
  595. 595 // 缩放到目标尺寸(保持宽高比,居中)
  596. 596● let scale_w = target_size.to_double() / src_w.to_double()
  597. 597● let scale_h = target_size.to_double() / src_h.to_double()
  598. 598◐ let scale = if scale_w < scale_h { scale_w } else { scale_h }
  599. 599● let new_w = max(1, (src_w.to_double() * scale).to_int())
  600. 600● let new_h = max(1, (src_h.to_double() * scale).to_int())
  601. 601 let offset_x = (target_size - new_w) / 2
  602. 602 let offset_y = (target_size - new_h) / 2
  603. 603● let out : Array[Byte] = Array::make(target_size * target_size * ch, b'\x00')
  604. 604 let data = img.data
  605. 605 // 最近邻缩放
  606. 606 for y in 0..<new_h {
  607. 607● for x in 0..<new_w {
  608. 608● let src_x = x_start + x * src_w / new_w
  609. 609 let src_y = y_start + y * src_h / new_h
  610. 610 for c in 0..<ch {
  611. 611● out[((offset_y + y) * target_size + (offset_x + x)) * ch + c] = data[(
  612. 612 src_y * w + src_x
  613. 613 ) *
  614. 614 ch +
  615. 615 c]
  616. 616 }
  617. 617 }
  618. 618 }
  619. 619 @types.Image::{
  620. 620 width: target_size,
  621. 621 height: target_size,
  622. 622 channels: ch,
  623. 623● data: Bytes::from_array(out),
  624. 624 }
  625. 625}
  626. 626
  627. 627///|
  628. 628/// 辅助函数:最小值
  629. 629fn _min_proj(a : Int32, b : Int32) -> Int32 {
  630. 630○ if a < b {
  631. 631○ a
  632. 632 } else {
  633. 633○ b
  634. 634 }
  635. 635}
  636. 636
  637. 637///|
  638. 638/// 辅助函数:最大值
  639. 639fn _max_proj(a : Int32, b : Int32) -> Int32 {
  640. 640○ if a > b {
  641. 641○ a
  642. 642 } else {
  643. 643○ b
  644. 644 }
  645. 645}