image.vision\ocr\char_segmentation.mbt

87.8% · 172 / 196 points · 632 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/char_segmentation
  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 SplitPoint {
  13. 13 x : Int32
  14. 14 y_top : Int32
  15. 15 y_bottom : Int32
  16. 16 confidence : Double
  17. 17}
  18. 18
  19. 19///|
  20. 20pub extend SplitPoint with Eq::{not_equal, equal}
  21. 21
  22. 22///|
  23. 23pub extend SplitPoint with @debug.Debug::{to_repr}
  24. 24
  25. 25///|
  26. 26/// 滴水算法分割结果
  27. 27#derive(Eq, @debug.Debug)
  28. 28pub(all) struct DropFallResult {
  29. 29 split_points : Array[SplitPoint]
  30. 30 char_regions : Array[(Int32, Int32)]
  31. 31 success : Bool
  32. 32}
  33. 33
  34. 34///|
  35. 35pub extend DropFallResult with Eq::{not_equal, equal}
  36. 36
  37. 37///|
  38. 38pub extend DropFallResult with @debug.Debug::{to_repr}
  39. 39
  40. 40///|
  41. 41/// 滴水算法(Drop Fall Algorithm)
  42. 42/// 用于分割粘连字符,模拟水滴从顶部落下的路径
  43. 43pub fn drop_fall_segmentation(
  44. 44 img : @types.Image,
  45. 45 start_x~ : Int32 = -1,
  46. 46) -> DropFallResult raise @types.LoadError {
  47. 47 // 输入验证
  48. 48● if img.width <= 0 || img.height <= 0 {
  49. 49● raise @types.LoadError::DecodeFailed(
  50. 50 "drop_fall_segmentation: 图像尺寸必须大于0",
  51. 51 )
  52. 52 }
  53. 53 if img.channels != 1 {
  54. 54● raise @types.LoadError::DecodeFailed(
  55. 55 "drop_fall_segmentation: 仅支持单通道二值图",
  56. 56 )
  57. 57 }
  58. 58 let expected_size = img.width * img.height
  59. 59● if img.data.length() != expected_size {
  60. 60● raise @types.LoadError::DecodeFailed(
  61. 61 "drop_fall_segmentation: 数据大小不匹配,期望 " +
  62. 62● expected_size.to_string() +
  63. 63 ",实际 " +
  64. 64● img.data.length().to_string(),
  65. 65 )
  66. 66 }
  67. 67
  68. 68 let w = img.width
  69. 69 let h = img.height
  70. 70 let data = img.data
  71. 71 // 如果未指定起始x,使用中间位置
  72. 72● let sx = if start_x < 0 { w / 2 } else { start_x }
  73. 73 // 滴水路径
  74. 74 let path : Array[(Int32, Int32)] = []
  75. 75 let mut x = sx
  76. 76 let mut y = 0
  77. 77 let stuck = false
  78. 78 let mut iterations = 0
  79. 79 let max_iterations = w * h
  80. 80 while y < h - 1 && !stuck && iterations < max_iterations {
  81. 81● iterations += 1
  82. 82● path.push((x, y))
  83. 83 // 检查下方三个方向:左下、正下、右下
  84. 84◐ let left = if x > 0 { data[(y + 1) * w + (x - 1)].to_int() } else { 255 }
  85. 85● let center = data[(y + 1) * w + x].to_int()
  86. 86 let right = if x < w - 1 {
  87. 87● data[(y + 1) * w + (x + 1)].to_int()
  88. 88 } else {
  89. 89○ 255
  90. 90 }
  91. 91 // 水滴优先向黑色像素(前景)移动
  92. 92 if center < 128 {
  93. 93 // 正下是黑色,继续向下
  94. 94● y += 1
  95. 95● } else if left < 128 && right < 128 {
  96. 96 // 左右都是黑色,随机选择(这里选择左)
  97. 97○ x -= 1
  98. 98 y += 1
  99. 99● } else if left < 128 {
  100. 100 // 左下是黑色
  101. 101○ x -= 1
  102. 102 y += 1
  103. 103● } else if right < 128 {
  104. 104 // 右下是黑色
  105. 105● x += 1
  106. 106 y += 1
  107. 107 } else {
  108. 108 // 下方都是白色,水滴在白色区域
  109. 109 // 检查是否到达底部或卡住
  110. 110● if y >= h - 1 {
  111. 111○ break
  112. 112 }
  113. 113 // 在白色区域,继续向下(模拟水滴穿透白色背景)
  114. 114 y += 1
  115. 115 }
  116. 116 // 边界检查
  117. 117 if x < 0 {
  118. 118○ x = 0
  119. 119 }
  120. 120 if x >= w {
  121. 121○ x = w - 1
  122. 122 }
  123. 123 }
  124. 124● path.push((x, y))
  125. 125 // 从路径中提取分割点
  126. 126 let split_points : Array[SplitPoint] = []
  127. 127● if path.length() > 0 {
  128. 128● let (final_x, _) = path[path.length() - 1]
  129. 129● split_points.push(SplitPoint::{
  130. 130 x: final_x,
  131. 131 y_top: 0,
  132. 132 y_bottom: h - 1,
  133. 133 confidence: 0.8,
  134. 134 })
  135. 135 }
  136. 136 // 计算字符区域
  137. 137 let char_regions : Array[(Int32, Int32)] = []
  138. 138● if split_points.length() > 0 {
  139. 139● let mut prev_x = 0
  140. 140 for sp in split_points {
  141. 141● char_regions.push((prev_x, sp.x))
  142. 142 prev_x = sp.x
  143. 143 }
  144. 144● char_regions.push((prev_x, w - 1))
  145. 145 } else {
  146. 146○ char_regions.push((0, w - 1))
  147. 147 }
  148. 148 DropFallResult::{
  149. 149 split_points,
  150. 150 char_regions,
  151. 151● success: split_points.length() > 0,
  152. 152 }
  153. 153}
  154. 154
  155. 155///|
  156. 156/// 检测粘连字符(基于垂直投影的局部最小值分析)
  157. 157/// 返回粘连位置列表
  158. 158pub fn detect_touching_chars(
  159. 159 img : @types.Image,
  160. 160 min_char_width~ : Int32 = 8,
  161. 161 max_char_width~ : Int32 = 50,
  162. 162) -> Array[Int32] raise @types.LoadError {
  163. 163 // 输入验证
  164. 164● if img.width <= 0 || img.height <= 0 {
  165. 165● raise @types.LoadError::DecodeFailed(
  166. 166 "detect_touching_chars: 图像尺寸必须大于0",
  167. 167 )
  168. 168 }
  169. 169 if img.channels != 1 {
  170. 170● raise @types.LoadError::DecodeFailed(
  171. 171 "detect_touching_chars: 仅支持单通道二值图",
  172. 172 )
  173. 173 }
  174. 174 let expected_size = img.width * img.height
  175. 175● if img.data.length() != expected_size {
  176. 176● raise @types.LoadError::DecodeFailed(
  177. 177 "detect_touching_chars: 数据大小不匹配,期望 " +
  178. 178● expected_size.to_string() +
  179. 179 ",实际 " +
  180. 180● img.data.length().to_string(),
  181. 181 )
  182. 182 }
  183. 183 if min_char_width <= 0 {
  184. 184○ raise @types.LoadError::DecodeFailed(
  185. 185 "detect_touching_chars: min_char_width必须大于0",
  186. 186 )
  187. 187 }
  188. 188 if max_char_width < min_char_width {
  189. 189○ raise @types.LoadError::DecodeFailed(
  190. 190 "detect_touching_chars: max_char_width不能小于min_char_width",
  191. 191 )
  192. 192 }
  193. 193
  194. 194 let w = img.width
  195. 195 let h = img.height
  196. 196 let data = img.data
  197. 197 // 垂直投影
  198. 198● let projection : Array[Int32] = Array::make(w, 0)
  199. 199 for y in 0..<h {
  200. 200● for x in 0..<w {
  201. 201● if data[y * w + x].to_int() > 128 {
  202. 202● projection[x] += 1
  203. 203 }
  204. 204 }
  205. 205 }
  206. 206 // 找局部最小值(可能的分割点)
  207. 207 let minima : Array[Int32] = []
  208. 208 for x in 2..<(w - 2) {
  209. 209● if projection[x] < projection[x - 1] &&
  210. 210 projection[x] <= projection[x + 1] &&
  211. 211 projection[x] < projection[x - 2] &&
  212. 212 projection[x] <= projection[x + 2] {
  213. 213● minima.push(x)
  214. 214 }
  215. 215 }
  216. 216 // 过滤:只保留在合理字符宽度范围内的分割点
  217. 217 let touching_points : Array[Int32] = []
  218. 218 let mut last_split = 0
  219. 219 for x in minima {
  220. 220● let width = x - last_split
  221. 221 if width >= min_char_width && width <= max_char_width * 2 {
  222. 222 // 检查这个位置是否真的是粘连点(投影值较低但不为0)
  223. 223● if projection[x] > 0 && projection[x] < h / 4 {
  224. 224○ touching_points.push(x)
  225. 225 last_split = x
  226. 226 }
  227. 227 }
  228. 228 }
  229. 229 touching_points
  230. 230}
  231. 231
  232. 232///|
  233. 233/// 基于连通域的字符分割
  234. 234/// 返回字符区域列表
  235. 235pub fn segment_by_connected_components(
  236. 236 img : @types.Image,
  237. 237 min_area~ : Int32 = 5,
  238. 238 max_area~ : Int32 = 10000,
  239. 239) -> Array[(Int32, Int32, Int32, Int32)] raise @types.LoadError {
  240. 240 // 输入验证
  241. 241● if img.width <= 0 || img.height <= 0 {
  242. 242● raise @types.LoadError::DecodeFailed(
  243. 243 "segment_by_connected_components: 图像尺寸必须大于0",
  244. 244 )
  245. 245 }
  246. 246 if img.channels != 1 {
  247. 247● raise @types.LoadError::DecodeFailed(
  248. 248 "segment_by_connected_components: 仅支持单通道二值图",
  249. 249 )
  250. 250 }
  251. 251 let expected_size = img.width * img.height
  252. 252● if img.data.length() != expected_size {
  253. 253● raise @types.LoadError::DecodeFailed(
  254. 254 "segment_by_connected_components: 数据大小不匹配,期望 " +
  255. 255● expected_size.to_string() +
  256. 256 ",实际 " +
  257. 257● img.data.length().to_string(),
  258. 258 )
  259. 259 }
  260. 260 if min_area < 0 {
  261. 261● raise @types.LoadError::DecodeFailed(
  262. 262 "segment_by_connected_components: min_area不能为负数",
  263. 263 )
  264. 264 }
  265. 265 if max_area < min_area {
  266. 266● raise @types.LoadError::DecodeFailed(
  267. 267 "segment_by_connected_components: max_area不能小于min_area",
  268. 268 )
  269. 269 }
  270. 270
  271. 271 let w = img.width
  272. 272 let h = img.height
  273. 273 let data = img.data
  274. 274 // 连通域标记 + 并查集(S30 修复)
  275. 275 //
  276. 276 // 旧版只做「取左邻或上邻的标签」,**不做等价标签合并**:当一个像素同时有左邻与上邻、
  277. 277 // 且两者标签不同时,它只认左边,右边那一片从此独立——同一个连通的字形会被切成好几段。
  278. 278 // 实测 5×7 字形里带斜笔画的 `2`/`Z`/`N` 被切成 6 段,块状字母 `E`/`F`/`L` 却没事,
  279. 279 // 这就是"英文识别只有块状字母能过"的真正原因。
  280. 280● let parent : Array[Int32] = Array::make(w * h + 1, 0)
  281. 281 let find = fn(start : Int32) -> Int32 {
  282. 282● let mut r = start
  283. 283 while parent[r] != 0 && parent[r] != r {
  284. 284● r = parent[r]
  285. 285 }
  286. 286 r
  287. 287 }
  288. 288 let union = fn(a1 : Int32, b1 : Int32) -> Unit {
  289. 289● let ra = find(a1)
  290. 290● let rb = find(b1)
  291. 291 if ra != rb {
  292. 292● parent[rb] = ra
  293. 293 }
  294. 294 }
  295. 295● let labels : Array[Int32] = Array::make(w * h, 0)
  296. 296 let mut next_label = 1
  297. 297 // 第一遍:扫前景(>128 = 墨),左/上双亲时 union
  298. 298 for y in 0..<h {
  299. 299● for x in 0..<w {
  300. 300● if data[y * w + x].to_int() > 128 {
  301. 301● let mut l = 0
  302. 302 if x > 0 {
  303. 303● l = labels[y * w + x - 1]
  304. 304 }
  305. 305 let mut u = 0
  306. 306 if y > 0 {
  307. 307● u = labels[(y - 1) * w + x]
  308. 308 }
  309. 309 let label = if l > 0 && u > 0 {
  310. 310● union(l, u)
  311. 311 l
  312. 312● } else if l > 0 {
  313. 313● l
  314. 314● } else if u > 0 {
  315. 315● u
  316. 316 } else {
  317. 317● let fresh = next_label
  318. 318 next_label += 1
  319. 319 parent[fresh] = fresh
  320. 320 fresh
  321. 321 }
  322. 322 labels[y * w + x] = label
  323. 323 }
  324. 324 }
  325. 325 }
  326. 326 // 第二遍:按并查集根统计每个连通域的包围盒与像素面积
  327. 327 // 计算每个标签的边界框
  328. 328● let min_x : Array[Int32] = Array::make(next_label, w)
  329. 329● let max_x : Array[Int32] = Array::make(next_label, 0)
  330. 330● let min_y : Array[Int32] = Array::make(next_label, h)
  331. 331● let max_y : Array[Int32] = Array::make(next_label, 0)
  332. 332● let area : Array[Int32] = Array::make(next_label, 0)
  333. 333 for y in 0..<h {
  334. 334● for x in 0..<w {
  335. 335● let label = find(labels[y * w + x])
  336. 336 if label > 0 {
  337. 337● area[label] += 1
  338. 338 if x < min_x[label] {
  339. 339● min_x[label] = x
  340. 340 }
  341. 341 if x > max_x[label] {
  342. 342● max_x[label] = x
  343. 343 }
  344. 344 if y < min_y[label] {
  345. 345● min_y[label] = y
  346. 346 }
  347. 347 if y > max_y[label] {
  348. 348● max_y[label] = y
  349. 349 }
  350. 350 }
  351. 351 }
  352. 352 }
  353. 353 // 过滤并返回
  354. 354 let regions : Array[(Int32, Int32, Int32, Int32)] = []
  355. 355 for label in 1..<next_label {
  356. 356● if area[label] >= min_area && area[label] <= max_area {
  357. 357● let rw = max_x[label] - min_x[label] + 1
  358. 358 let rh = max_y[label] - min_y[label] + 1
  359. 359 if rw > 0 && rh > 0 {
  360. 360● regions.push((min_x[label], min_y[label], rw, rh))
  361. 361 }
  362. 362 }
  363. 363 }
  364. 364 regions
  365. 365}
  366. 366
  367. 367///|
  368. 368/// 分割后处理:合并过小的分割区域
  369. 369pub fn merge_small_segments(
  370. 370 segments : Array[(Int32, Int32, Int32, Int32)],
  371. 371 min_width : Int32,
  372. 372 min_height : Int32,
  373. 373) -> Array[(Int32, Int32, Int32, Int32)] raise @types.LoadError {
  374. 374 // 输入验证
  375. 375● if segments.length() == 0 {
  376. 376● raise @types.LoadError::DecodeFailed(
  377. 377 "merge_small_segments: 分割区域列表不能为空",
  378. 378 )
  379. 379 }
  380. 380 if min_width <= 0 {
  381. 381● raise @types.LoadError::DecodeFailed(
  382. 382 "merge_small_segments: min_width必须大于0",
  383. 383 )
  384. 384 }
  385. 385 if min_height <= 0 {
  386. 386● raise @types.LoadError::DecodeFailed(
  387. 387 "merge_small_segments: min_height必须大于0",
  388. 388 )
  389. 389 }
  390. 390
  391. 391 let merged : Array[(Int32, Int32, Int32, Int32)] = []
  392. 392 let mut current = segments[0]
  393. 393● for i in 1..<segments.length() {
  394. 394● let next = segments[i]
  395. 395 let (cx, cy, cw, ch) = current
  396. 396 let (nx, ny, nw, nh) = next
  397. 397 // 检查当前区域是否过小
  398. 398 if cw < min_width || ch < min_height {
  399. 399 // 合并到下一个区域
  400. 400● let new_x = min(cx, nx)
  401. 401● let new_y = min(cy, ny)
  402. 402● let new_max_x = max(cx + cw, nx + nw)
  403. 403● let new_max_y = max(cy + ch, ny + nh)
  404. 404 current = (new_x, new_y, new_max_x - new_x, new_max_y - new_y)
  405. 405 } else {
  406. 406● merged.push(current)
  407. 407 current = next
  408. 408 }
  409. 409 }
  410. 410 // 处理最后一个区域
  411. 411 let (cx, cy, cw, ch) = current
  412. 412 if cw >= min_width && ch >= min_height {
  413. 413● merged.push(current)
  414. 414● } else if merged.length() > 0 {
  415. 415 // 合并到前一个区域
  416. 416● let (px, py, pw, ph) = merged[merged.length() - 1]
  417. 417● let new_x = min(px, cx)
  418. 418● let new_y = min(py, cy)
  419. 419● let new_max_x = max(px + pw, cx + cw)
  420. 420● let new_max_y = max(py + ph, cy + ch)
  421. 421● merged[merged.length() - 1] = (
  422. 422 new_x,
  423. 423 new_y,
  424. 424 new_max_x - new_x,
  425. 425 new_max_y - new_y,
  426. 426 )
  427. 427 }
  428. 428 merged
  429. 429}
  430. 430
  431. 431///|
  432. 432/// 分割后处理:过滤噪声区域
  433. 433pub fn filter_noise_segments(
  434. 434 segments : Array[(Int32, Int32, Int32, Int32)],
  435. 435 img_width : Int32,
  436. 436 img_height : Int32,
  437. 437 min_area_ratio~ : Double = 0.001,
  438. 438 max_area_ratio~ : Double = 0.5,
  439. 439) -> Array[(Int32, Int32, Int32, Int32)] raise @types.LoadError {
  440. 440 // 输入验证
  441. 441● if segments.length() == 0 {
  442. 442● raise @types.LoadError::DecodeFailed(
  443. 443 "filter_noise_segments: 分割区域列表不能为空",
  444. 444 )
  445. 445 }
  446. 446 if img_width <= 0 || img_height <= 0 {
  447. 447● raise @types.LoadError::DecodeFailed(
  448. 448 "filter_noise_segments: 图像尺寸必须大于0",
  449. 449 )
  450. 450 }
  451. 451 if min_area_ratio < 0.0 || min_area_ratio > 1.0 {
  452. 452● raise @types.LoadError::DecodeFailed(
  453. 453 "filter_noise_segments: min_area_ratio必须在0-1之间",
  454. 454 )
  455. 455 }
  456. 456 if max_area_ratio < 0.0 || max_area_ratio > 1.0 {
  457. 457● raise @types.LoadError::DecodeFailed(
  458. 458 "filter_noise_segments: max_area_ratio必须在0-1之间",
  459. 459 )
  460. 460 }
  461. 461 if max_area_ratio < min_area_ratio {
  462. 462● raise @types.LoadError::DecodeFailed(
  463. 463 "filter_noise_segments: max_area_ratio不能小于min_area_ratio",
  464. 464 )
  465. 465 }
  466. 466
  467. 467 let img_area = img_width * img_height
  468. 468● let min_area = (img_area.to_double() * min_area_ratio).to_int()
  469. 469● let max_area = (img_area.to_double() * max_area_ratio).to_int()
  470. 470 let filtered : Array[(Int32, Int32, Int32, Int32)] = []
  471. 471 for _tup0 in segments {
  472. 472● let (x, y, w, h) = _tup0
  473. 473 let area = w * h
  474. 474 if area >= min_area && area <= max_area {
  475. 475● filtered.push((x, y, w, h))
  476. 476 }
  477. 477 }
  478. 478 filtered
  479. 479}
  480. 480
  481. 481///|
  482. 482/// 完整字符分割流水线
  483. 483/// 1. 连通域初步分割
  484. 484/// 2. 粘连字符检测
  485. 485/// 3. 滴水算法分割粘连字符
  486. 486/// 4. 后处理(合并过小、过滤噪声)
  487. 487pub fn complete_char_segmentation(
  488. 488 img : @types.Image,
  489. 489 min_char_width~ : Int32 = 5,
  490. 490 max_char_width~ : Int32 = 60,
  491. 491) -> Array[(Int32, Int32, Int32, Int32)] raise @types.LoadError {
  492. 492 // 输入验证
  493. 493● if img.width <= 0 || img.height <= 0 {
  494. 494● raise @types.LoadError::DecodeFailed(
  495. 495 "complete_char_segmentation: 图像尺寸必须大于0",
  496. 496 )
  497. 497 }
  498. 498 if img.channels != 1 {
  499. 499● raise @types.LoadError::DecodeFailed(
  500. 500 "complete_char_segmentation: 仅支持单通道二值图",
  501. 501 )
  502. 502 }
  503. 503 let expected_size = img.width * img.height
  504. 504● if img.data.length() != expected_size {
  505. 505● raise @types.LoadError::DecodeFailed(
  506. 506 "complete_char_segmentation: 数据大小不匹配,期望 " +
  507. 507● expected_size.to_string() +
  508. 508 ",实际 " +
  509. 509● img.data.length().to_string(),
  510. 510 )
  511. 511 }
  512. 512 if min_char_width <= 0 {
  513. 513● raise @types.LoadError::DecodeFailed(
  514. 514 "complete_char_segmentation: min_char_width必须大于0",
  515. 515 )
  516. 516 }
  517. 517 if max_char_width < min_char_width {
  518. 518● raise @types.LoadError::DecodeFailed(
  519. 519 "complete_char_segmentation: max_char_width不能小于min_char_width",
  520. 520 )
  521. 521 }
  522. 522
  523. 523 let w = img.width
  524. 524 let h = img.height
  525. 525 // 1. 连通域初步分割
  526. 526● let segments = segment_by_connected_components(
  527. 527 img,
  528. 528 min_area=5,
  529. 529 max_area=100000,
  530. 530 )
  531. 531 // 2. 检测粘连字符(宽度过大的区域)
  532. 532 let final_segments : Array[(Int32, Int32, Int32, Int32)] = []
  533. 533 for _tup1 in segments {
  534. 534● let (x, y, sw, sh) = _tup1
  535. 535 if sw > max_char_width && sh > min_char_width {
  536. 536 // 可能是粘连字符,尝试滴水算法分割
  537. 537 // 裁剪这个区域
  538. 538● let sub_img = crop_sub_image(img, x, y, sw, sh)
  539. 539● match sub_img {
  540. 540● Some(si) => {
  541. 541● let drop_result = drop_fall_segmentation(si)
  542. 542● if drop_result.success && drop_result.char_regions.length() > 1 {
  543. 543 // 分割成功,添加子区域
  544. 544● for _tup2 in drop_result.char_regions {
  545. 545● let (rx1, rx2) = _tup2
  546. 546 let rw = rx2 - rx1 + 1
  547. 547 if rw >= min_char_width {
  548. 548● final_segments.push((x + rx1, y, rw, sh))
  549. 549 }
  550. 550 }
  551. 551 } else {
  552. 552 // 分割失败,保留原区域
  553. 553○ final_segments.push((x, y, sw, sh))
  554. 554 }
  555. 555 }
  556. 556○ None => final_segments.push((x, y, sw, sh))
  557. 557 }
  558. 558 } else {
  559. 559● final_segments.push((x, y, sw, sh))
  560. 560 }
  561. 561 }
  562. 562 // 空图/纯背景:没有候选就不该进后处理链(旧版会让 merge_small_segments 抛错)
  563. 563● if final_segments.length() == 0 {
  564. 564● return []
  565. 565 }
  566. 566 // 3. 后处理:合并过小的区域
  567. 567● let merged = merge_small_segments(
  568. 568 final_segments, min_char_width, min_char_width,
  569. 569 )
  570. 570 // 4. 过滤噪声
  571. 571● let filtered = filter_noise_segments(
  572. 572 merged,
  573. 573 w,
  574. 574 h,
  575. 575 min_area_ratio=0.0005,
  576. 576 max_area_ratio=0.8,
  577. 577 )
  578. 578 filtered
  579. 579}
  580. 580
  581. 581///|
  582. 582/// 裁剪子图像
  583. 583fn crop_sub_image(
  584. 584 img : @types.Image,
  585. 585 x : Int32,
  586. 586 y : Int32,
  587. 587 width : Int32,
  588. 588 height : Int32,
  589. 589) -> @types.Image? {
  590. 590● let w = img.width
  591. 591 let h = img.height
  592. 592 let ch = img.channels
  593. 593 if x < 0 || y < 0 || x + width > w || y + height > h {
  594. 594○ return None
  595. 595 }
  596. 596● let out : Array[Byte] = Array::make(width * height * ch, b'\x00')
  597. 597 let data = img.data
  598. 598 for yy in 0..<height {
  599. 599● for xx in 0..<width {
  600. 600● for c in 0..<ch {
  601. 601● out[(yy * width + xx) * ch + c] = data[((y + yy) * w + (x + xx)) * ch +
  602. 602 c]
  603. 603 }
  604. 604 }
  605. 605 }
  606. 606 Some(@types.Image::{
  607. 607 width,
  608. 608 height,
  609. 609 channels: ch,
  610. 610● data: Bytes::from_array(out),
  611. 611 })
  612. 612}
  613. 613
  614. 614///|
  615. 615/// 辅助函数:最小值
  616. 616fn _min_seg(a : Int32, b : Int32) -> Int32 {
  617. 617○ if a < b {
  618. 618○ a
  619. 619 } else {
  620. 620○ b
  621. 621 }
  622. 622}
  623. 623
  624. 624///|
  625. 625/// 辅助函数:最大值
  626. 626fn _max_seg(a : Int32, b : Int32) -> Int32 {
  627. 627○ if a > b {
  628. 628○ a
  629. 629 } else {
  630. 630○ b
  631. 631 }
  632. 632}