image.process\color\adaptive_threshold_16f.mbt

94.1% · 191 / 203 points · 605 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/// # 自适应阈值 16f adaptive threshold 16f
  3. 3///
  4. 4/// 图像处理算法模块
  5. 5/// 图像处理算法模块 Image processing algorithm module
  6. 6// 阈值处理 16-bit/float 变体
  7. 7// 支持 Image16 / ImageF 两种类型
  8. 8// 输出均为二值图像(16-bit: 0/65535,float: 0.0/1.0)
  9. 9
  10. 10// 复用 color_16f.mbt 中的 read_u16_le_c / write_u16_le_c / write_f32_le
  11. 11// 复用 tonemap.mbt 中的 read_float_le
  12. 12
  13. 13///|
  14. 14/// 16-bit 自适应阈值(均值法)。block_size 必须为 >=3 的奇数。
  15. 15/// 输出二值图像(0 或 65535),alpha 通道不变。
  16. 16/// @see adaptive_threshold_mean 8-bit 版本,本函数为 Image16 变体
  17. 17pub fn adaptive_threshold_mean_16(
  18. 18 img : @types.Image16,
  19. 19 block_size : Int32,
  20. 20 c : Int32,
  21. 21) -> @types.Image16 raise @types.LoadError {
  22. 22 // 输入验证
  23. 23● if img.width <= 0 || img.height <= 0 {
  24. 24● raise @types.LoadError::DecodeFailed(
  25. 25 "adaptive_threshold_mean_16: 图像尺寸必须大于0",
  26. 26 )
  27. 27 }
  28. 28 if block_size < 3 || block_size % 2 == 0 {
  29. 29● raise @types.LoadError::DecodeFailed("block_size 必须为 >=3 的奇数")
  30. 30 }
  31. 31 if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  32. 32● raise @types.LoadError::DecodeFailed(
  33. 33 "adaptive_threshold_mean_16 仅支持 channels=1/3/4",
  34. 34 )
  35. 35 }
  36. 36 let expected_size = img.width * img.height * img.channels * 2
  37. 37● if img.data.length() != expected_size {
  38. 38● raise @types.LoadError::DecodeFailed(
  39. 39 "adaptive_threshold_mean_16: 数据大小不匹配,期望 " +
  40. 40● expected_size.to_string() +
  41. 41 ",实际 " +
  42. 42● img.data.length().to_string(),
  43. 43 )
  44. 44 }
  45. 45 let ch = img.channels
  46. 46● let color_ch = if ch >= 3 { 3 } else { 1 }
  47. 47 let w = img.width
  48. 48 let h = img.height
  49. 49 let n = w * h
  50. 50 let bpp = ch * 2
  51. 51● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  52. 52 let half = block_size / 2
  53. 53● let src : Array[Int32] = Array::make(n * ch, 0)
  54. 54 for i in 0..<(n * ch) {
  55. 55● src[i] = read_u16_le_c(img.data, i * 2)
  56. 56 }
  57. 57 let inner_count = block_size * block_size
  58. 58 for c_idx in 0..<color_ch {
  59. 59● for y in half..<(h - half) {
  60. 60● for x in half..<(w - half) {
  61. 61● let mut sum = 0
  62. 62 for dy in -half..<=half {
  63. 63● let row = (y + dy) * w
  64. 64 for dx in -half..<=half {
  65. 65● sum += src[(row + x + dx) * ch + c_idx]
  66. 66 }
  67. 67 }
  68. 68 let mean = sum / inner_count
  69. 69 let threshold = mean - c
  70. 70 let v = src[(y * w + x) * ch + c_idx]
  71. 71 if v > threshold {
  72. 72● write_u16_le_c(out, (y * w + x) * bpp + c_idx * 2, 65535)
  73. 73 }
  74. 74 }
  75. 75 }
  76. 76 for y in 0..<h {
  77. 77● let y_inner = y >= half && y < h - half
  78. 78 for x in 0..<w {
  79. 79● if y_inner && x >= half && x < w - half {
  80. 80● continue
  81. 81 }
  82. 82 let mut sum = 0
  83. 83 let mut count = 0
  84. 84 for dy in -half..<=half {
  85. 85● for dx in -half..<=half {
  86. 86● let ny = y + dy
  87. 87 let nx = x + dx
  88. 88 if ny >= 0 && ny < h && nx >= 0 && nx < w {
  89. 89● sum += src[(ny * w + nx) * ch + c_idx]
  90. 90 count += 1
  91. 91 }
  92. 92 }
  93. 93 }
  94. 94◐ let mean = if count > 0 { sum / count } else { 0 }
  95. 95 let threshold = mean - c
  96. 96 let v = src[(y * w + x) * ch + c_idx]
  97. 97 if v > threshold {
  98. 98● write_u16_le_c(out, (y * w + x) * bpp + c_idx * 2, 65535)
  99. 99 }
  100. 100 }
  101. 101 }
  102. 102 }
  103. 103 if ch == 4 {
  104. 104● for i in 0..<n {
  105. 105● let alpha_off = i * bpp + 6
  106. 106 out[alpha_off] = img.data[alpha_off]
  107. 107 out[alpha_off + 1] = img.data[alpha_off + 1]
  108. 108 }
  109. 109 }
  110. 110 @types.Image16::{
  111. 111 width: w,
  112. 112 height: h,
  113. 113 channels: ch,
  114. 114● data: Bytes::from_array(out),
  115. 115 }
  116. 116}
  117. 117
  118. 118///|
  119. 119/// float 自适应阈值(均值法)。c 为浮点偏移量。
  120. 120/// 输出二值图像(0.0 或 1.0),alpha 通道不变。
  121. 121/// @see adaptive_threshold_mean 8-bit 版本,本函数为 ImageF 变体
  122. 122pub fn adaptive_threshold_mean_f(
  123. 123 img : @types.ImageF,
  124. 124 block_size : Int32,
  125. 125 c : Float,
  126. 126) -> @types.ImageF raise @types.LoadError {
  127. 127● if block_size < 3 || block_size % 2 == 0 {
  128. 128● raise @types.LoadError::DecodeFailed("block_size 必须为 >=3 的奇数")
  129. 129 }
  130. 130 if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  131. 131● raise @types.LoadError::DecodeFailed(
  132. 132 "adaptive_threshold_mean_f 仅支持 channels=1/3/4",
  133. 133 )
  134. 134 }
  135. 135 let ch = img.channels
  136. 136● let color_ch = if ch >= 3 { 3 } else { 1 }
  137. 137 let w = img.width
  138. 138 let h = img.height
  139. 139 let n = w * h
  140. 140 let bpp = ch * 4
  141. 141● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  142. 142 let half = block_size / 2
  143. 143● let src : Array[Float] = Array::make(n * ch, 0.0F)
  144. 144 for i in 0..<(n * ch) {
  145. 145● src[i] = read_float_le(img.data, i * 4)
  146. 146 }
  147. 147● let inner_count = Float::from_int(block_size * block_size)
  148. 148 for c_idx in 0..<color_ch {
  149. 149● for y in half..<(h - half) {
  150. 150● for x in half..<(w - half) {
  151. 151● let mut sum = 0.0F
  152. 152 for dy in -half..<=half {
  153. 153● let row = (y + dy) * w
  154. 154 for dx in -half..<=half {
  155. 155● sum += src[(row + x + dx) * ch + c_idx]
  156. 156 }
  157. 157 }
  158. 158 let mean = sum / inner_count
  159. 159 let threshold = mean - c
  160. 160 let v = src[(y * w + x) * ch + c_idx]
  161. 161 if v > threshold {
  162. 162● write_f32_le(out, (y * w + x) * bpp + c_idx * 4, 1.0F)
  163. 163 }
  164. 164 }
  165. 165 }
  166. 166 for y in 0..<h {
  167. 167● let y_inner = y >= half && y < h - half
  168. 168 for x in 0..<w {
  169. 169● if y_inner && x >= half && x < w - half {
  170. 170● continue
  171. 171 }
  172. 172 let mut sum = 0.0F
  173. 173 let mut count = 0.0F
  174. 174 for dy in -half..<=half {
  175. 175● for dx in -half..<=half {
  176. 176● let ny = y + dy
  177. 177 let nx = x + dx
  178. 178 if ny >= 0 && ny < h && nx >= 0 && nx < w {
  179. 179● sum += src[(ny * w + nx) * ch + c_idx]
  180. 180 count += 1.0F
  181. 181 }
  182. 182 }
  183. 183 }
  184. 184◐ let mean = if count > 0.0F { sum / count } else { 0.0F }
  185. 185 let threshold = mean - c
  186. 186 let v = src[(y * w + x) * ch + c_idx]
  187. 187 if v > threshold {
  188. 188● write_f32_le(out, (y * w + x) * bpp + c_idx * 4, 1.0F)
  189. 189 }
  190. 190 }
  191. 191 }
  192. 192 }
  193. 193 if ch == 4 {
  194. 194● for i in 0..<n {
  195. 195● let alpha_off = i * bpp + 12
  196. 196 for b in 0..<4 {
  197. 197● out[alpha_off + b] = img.data[alpha_off + b]
  198. 198 }
  199. 199 }
  200. 200 }
  201. 201 @types.ImageF::{
  202. 202 width: w,
  203. 203 height: h,
  204. 204 channels: ch,
  205. 205● data: Bytes::from_array(out),
  206. 206 }
  207. 207}
  208. 208
  209. 209///|
  210. 210/// 16-bit 自适应阈值(高斯加权法)。
  211. 211/// @see adaptive_threshold_gaussian 8-bit 版本,本函数为 Image16 变体
  212. 212pub fn adaptive_threshold_gaussian_16(
  213. 213 img : @types.Image16,
  214. 214 block_size : Int32,
  215. 215 c : Int32,
  216. 216) -> @types.Image16 raise @types.LoadError {
  217. 217 // 输入验证
  218. 218● if img.width <= 0 || img.height <= 0 {
  219. 219● raise @types.LoadError::DecodeFailed(
  220. 220 "adaptive_threshold_gaussian_16: 图像尺寸必须大于0",
  221. 221 )
  222. 222 }
  223. 223 if block_size < 3 || block_size % 2 == 0 {
  224. 224● raise @types.LoadError::DecodeFailed("block_size 必须为 >=3 的奇数")
  225. 225 }
  226. 226 let expected_size = img.width * img.height * img.channels * 2
  227. 227● if img.data.length() != expected_size {
  228. 228○ raise @types.LoadError::DecodeFailed(
  229. 229 "adaptive_threshold_gaussian_16: 数据大小不匹配,期望 " +
  230. 230○ expected_size.to_string() +
  231. 231 ",实际 " +
  232. 232○ img.data.length().to_string(),
  233. 233 )
  234. 234 }
  235. 235 if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  236. 236● raise @types.LoadError::DecodeFailed(
  237. 237 "adaptive_threshold_gaussian_16 仅支持 channels=1/3/4",
  238. 238 )
  239. 239 }
  240. 240 let ch = img.channels
  241. 241● let color_ch = if ch >= 3 { 3 } else { 1 }
  242. 242 let w = img.width
  243. 243 let h = img.height
  244. 244 let n = w * h
  245. 245 let bpp = ch * 2
  246. 246● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  247. 247 let half = block_size / 2
  248. 248● let sigma = Float::from_int(half) / 2.0F
  249. 249 let two_sigma_sq = 2.0F * sigma * sigma
  250. 250● let weights : Array[Float] = Array::make(block_size * block_size, 0.0F)
  251. 251 let mut total_weight = 0.0F
  252. 252 for dy in 0..<block_size {
  253. 253● for dx in 0..<block_size {
  254. 254● let fy = Float::from_int(dy - half)
  255. 255● let fx = Float::from_int(dx - half)
  256. 256● let wt = @math.expf(-(fx * fx + fy * fy) / two_sigma_sq)
  257. 257 weights[dy * block_size + dx] = wt
  258. 258 total_weight += wt
  259. 259 }
  260. 260 }
  261. 261● let src : Array[Float] = Array::make(n * ch, 0.0F)
  262. 262 for i in 0..<(n * ch) {
  263. 263● src[i] = Float::from_int(read_u16_le_c(img.data, i * 2))
  264. 264 }
  265. 265 for c_idx in 0..<color_ch {
  266. 266● for y in half..<(h - half) {
  267. 267● for x in half..<(w - half) {
  268. 268● let mut sum = 0.0F
  269. 269 for dy in 0..<block_size {
  270. 270● let row = (y + dy - half) * w
  271. 271 let wbase = dy * block_size
  272. 272 for dx in 0..<block_size {
  273. 273● sum += src[(row + x + dx - half) * ch + c_idx] * weights[wbase + dx]
  274. 274 }
  275. 275 }
  276. 276 let mean = sum / total_weight
  277. 277● let threshold = mean - Float::from_int(c)
  278. 278 let v = src[(y * w + x) * ch + c_idx]
  279. 279 if v > threshold {
  280. 280● write_u16_le_c(out, (y * w + x) * bpp + c_idx * 2, 65535)
  281. 281 }
  282. 282 }
  283. 283 }
  284. 284 for y in 0..<h {
  285. 285● let y_inner = y >= half && y < h - half
  286. 286 for x in 0..<w {
  287. 287● if y_inner && x >= half && x < w - half {
  288. 288● continue
  289. 289 }
  290. 290 let mut sum = 0.0F
  291. 291 let mut weight_sum = 0.0F
  292. 292 for dy in -half..<=half {
  293. 293● for dx in -half..<=half {
  294. 294● let ny = y + dy
  295. 295 let nx = x + dx
  296. 296 if ny >= 0 && ny < h && nx >= 0 && nx < w {
  297. 297● let weight = weights[(dy + half) * block_size + (dx + half)]
  298. 298 sum += src[(ny * w + nx) * ch + c_idx] * weight
  299. 299 weight_sum += weight
  300. 300 }
  301. 301 }
  302. 302 }
  303. 303◐ let mean = if weight_sum > 0.0F { sum / weight_sum } else { 0.0F }
  304. 304● let threshold = mean - Float::from_int(c)
  305. 305 let v = src[(y * w + x) * ch + c_idx]
  306. 306 if v > threshold {
  307. 307● write_u16_le_c(out, (y * w + x) * bpp + c_idx * 2, 65535)
  308. 308 }
  309. 309 }
  310. 310 }
  311. 311 }
  312. 312 if ch == 4 {
  313. 313● for i in 0..<n {
  314. 314● let alpha_off = i * bpp + 6
  315. 315 out[alpha_off] = img.data[alpha_off]
  316. 316 out[alpha_off + 1] = img.data[alpha_off + 1]
  317. 317 }
  318. 318 }
  319. 319 @types.Image16::{
  320. 320 width: w,
  321. 321 height: h,
  322. 322 channels: ch,
  323. 323● data: Bytes::from_array(out),
  324. 324 }
  325. 325}
  326. 326
  327. 327///|
  328. 328/// float 自适应阈值(高斯加权法)。
  329. 329/// @see adaptive_threshold_gaussian 8-bit 版本,本函数为 ImageF 变体
  330. 330pub fn adaptive_threshold_gaussian_f(
  331. 331 img : @types.ImageF,
  332. 332 block_size : Int32,
  333. 333 c : Float,
  334. 334) -> @types.ImageF raise @types.LoadError {
  335. 335● if block_size < 3 || block_size % 2 == 0 {
  336. 336● raise @types.LoadError::DecodeFailed("block_size 必须为 >=3 的奇数")
  337. 337 }
  338. 338 if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  339. 339● raise @types.LoadError::DecodeFailed(
  340. 340 "adaptive_threshold_gaussian_f 仅支持 channels=1/3/4",
  341. 341 )
  342. 342 }
  343. 343 let ch = img.channels
  344. 344● let color_ch = if ch >= 3 { 3 } else { 1 }
  345. 345 let w = img.width
  346. 346 let h = img.height
  347. 347 let n = w * h
  348. 348 let bpp = ch * 4
  349. 349● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  350. 350 let half = block_size / 2
  351. 351● let sigma = Float::from_int(half) / 2.0F
  352. 352 let two_sigma_sq = 2.0F * sigma * sigma
  353. 353● let weights : Array[Float] = Array::make(block_size * block_size, 0.0F)
  354. 354 let mut total_weight = 0.0F
  355. 355 for dy in 0..<block_size {
  356. 356● for dx in 0..<block_size {
  357. 357● let fy = Float::from_int(dy - half)
  358. 358● let fx = Float::from_int(dx - half)
  359. 359● let wt = @math.expf(-(fx * fx + fy * fy) / two_sigma_sq)
  360. 360 weights[dy * block_size + dx] = wt
  361. 361 total_weight += wt
  362. 362 }
  363. 363 }
  364. 364● let src : Array[Float] = Array::make(n * ch, 0.0F)
  365. 365 for i in 0..<(n * ch) {
  366. 366● src[i] = read_float_le(img.data, i * 4)
  367. 367 }
  368. 368 for c_idx in 0..<color_ch {
  369. 369● for y in half..<(h - half) {
  370. 370● for x in half..<(w - half) {
  371. 371● let mut sum = 0.0F
  372. 372 for dy in 0..<block_size {
  373. 373● let row = (y + dy - half) * w
  374. 374 let wbase = dy * block_size
  375. 375 for dx in 0..<block_size {
  376. 376● sum += src[(row + x + dx - half) * ch + c_idx] * weights[wbase + dx]
  377. 377 }
  378. 378 }
  379. 379 let mean = sum / total_weight
  380. 380 let threshold = mean - c
  381. 381 let v = src[(y * w + x) * ch + c_idx]
  382. 382 if v > threshold {
  383. 383● write_f32_le(out, (y * w + x) * bpp + c_idx * 4, 1.0F)
  384. 384 }
  385. 385 }
  386. 386 }
  387. 387 for y in 0..<h {
  388. 388● let y_inner = y >= half && y < h - half
  389. 389 for x in 0..<w {
  390. 390● if y_inner && x >= half && x < w - half {
  391. 391● continue
  392. 392 }
  393. 393 let mut sum = 0.0F
  394. 394 let mut weight_sum = 0.0F
  395. 395 for dy in -half..<=half {
  396. 396● for dx in -half..<=half {
  397. 397● let ny = y + dy
  398. 398 let nx = x + dx
  399. 399 if ny >= 0 && ny < h && nx >= 0 && nx < w {
  400. 400● let weight = weights[(dy + half) * block_size + (dx + half)]
  401. 401 sum += src[(ny * w + nx) * ch + c_idx] * weight
  402. 402 weight_sum += weight
  403. 403 }
  404. 404 }
  405. 405 }
  406. 406◐ let mean = if weight_sum > 0.0F { sum / weight_sum } else { 0.0F }
  407. 407 let threshold = mean - c
  408. 408 let v = src[(y * w + x) * ch + c_idx]
  409. 409 if v > threshold {
  410. 410● write_f32_le(out, (y * w + x) * bpp + c_idx * 4, 1.0F)
  411. 411 }
  412. 412 }
  413. 413 }
  414. 414 }
  415. 415 if ch == 4 {
  416. 416● for i in 0..<n {
  417. 417● let alpha_off = i * bpp + 12
  418. 418 for b in 0..<4 {
  419. 419● out[alpha_off + b] = img.data[alpha_off + b]
  420. 420 }
  421. 421 }
  422. 422 }
  423. 423 @types.ImageF::{
  424. 424 width: w,
  425. 425 height: h,
  426. 426 channels: ch,
  427. 427● data: Bytes::from_array(out),
  428. 428 }
  429. 429}
  430. 430
  431. 431///|
  432. 432/// 16-bit Otsu 大津法自动阈值。使用 65536-bin 直方图。
  433. 433/// 输出二值图像(0 或 65535)。
  434. 434/// @see threshold_otsu 8-bit 版本,本函数为 Image16 变体
  435. 435pub fn threshold_otsu_16(
  436. 436 img : @types.Image16,
  437. 437) -> @types.Image16 raise @types.LoadError {
  438. 438 // 输入验证
  439. 439● if img.width <= 0 || img.height <= 0 {
  440. 440● raise @types.LoadError::DecodeFailed(
  441. 441 "threshold_otsu_16: 图像尺寸必须大于0",
  442. 442 )
  443. 443 }
  444. 444 if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  445. 445○ raise @types.LoadError::DecodeFailed(
  446. 446 "threshold_otsu_16 仅支持 channels=1/3/4",
  447. 447 )
  448. 448 }
  449. 449 let expected_size = img.width * img.height * img.channels * 2
  450. 450● if img.data.length() != expected_size {
  451. 451● raise @types.LoadError::DecodeFailed(
  452. 452 "threshold_otsu_16: 数据大小不匹配,期望 " +
  453. 453● expected_size.to_string() +
  454. 454 ",实际 " +
  455. 455● img.data.length().to_string(),
  456. 456 )
  457. 457 }
  458. 458 let ch = img.channels
  459. 459● let color_ch = if ch >= 3 { 3 } else { 1 }
  460. 460 let w = img.width
  461. 461 let h = img.height
  462. 462 let n = w * h
  463. 463 let bpp = ch * 2
  464. 464● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  465. 465 for c_idx in 0..<color_ch {
  466. 466● let hist : Array[Int32] = Array::make(65536, 0)
  467. 467 for i in 0..<n {
  468. 468● hist[read_u16_le_c(img.data, i * bpp + c_idx * 2)] += 1
  469. 469 }
  470. 470 let mut total = 0
  471. 471 let mut sum = 0
  472. 472 for t in 0..<65536 {
  473. 473● total += hist[t]
  474. 474 sum += t * hist[t]
  475. 475 }
  476. 476 let mut best_t = 0
  477. 477 let mut best_var = -1.0F
  478. 478 let mut w0 = 0
  479. 479 let mut sum0 = 0
  480. 480 for t in 0..<65536 {
  481. 481● w0 += hist[t]
  482. 482 sum0 += t * hist[t]
  483. 483 if w0 == 0 || w0 == total {
  484. 484● continue
  485. 485 }
  486. 486 let w1 = total - w0
  487. 487 let sum1 = sum - sum0
  488. 488● let mean0 = Float::from_int(sum0) / Float::from_int(w0)
  489. 489● let mean1 = Float::from_int(sum1) / Float::from_int(w1)
  490. 490● let var_between = Float::from_int(w0) *
  491. 491● Float::from_int(w1) *
  492. 492 (mean0 - mean1) *
  493. 493 (mean0 - mean1)
  494. 494 if var_between > best_var {
  495. 495● best_var = var_between
  496. 496 best_t = t
  497. 497 }
  498. 498 }
  499. 499 for i in 0..<n {
  500. 500● let v = read_u16_le_c(img.data, i * bpp + c_idx * 2)
  501. 501 if v > best_t {
  502. 502● write_u16_le_c(out, i * bpp + c_idx * 2, 65535)
  503. 503 }
  504. 504 }
  505. 505 }
  506. 506 if ch == 4 {
  507. 507● for i in 0..<n {
  508. 508● let alpha_off = i * bpp + 6
  509. 509 out[alpha_off] = img.data[alpha_off]
  510. 510 out[alpha_off + 1] = img.data[alpha_off + 1]
  511. 511 }
  512. 512 }
  513. 513 @types.Image16::{
  514. 514 width: w,
  515. 515 height: h,
  516. 516 channels: ch,
  517. 517● data: Bytes::from_array(out),
  518. 518 }
  519. 519}
  520. 520
  521. 521///|
  522. 522/// float Otsu 大津法自动阈值。将值归一化到 0-255 后使用 256-bin 直方图。
  523. 523/// 输出二值图像(0.0 或 1.0)。
  524. 524/// @see threshold_otsu 8-bit 版本,本函数为 ImageF 变体
  525. 525pub fn threshold_otsu_f(
  526. 526 img : @types.ImageF,
  527. 527) -> @types.ImageF raise @types.LoadError {
  528. 528● if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  529. 529○ raise @types.LoadError::DecodeFailed(
  530. 530 "threshold_otsu_f 仅支持 channels=1/3/4",
  531. 531 )
  532. 532 }
  533. 533 let ch = img.channels
  534. 534● let color_ch = if ch >= 3 { 3 } else { 1 }
  535. 535 let w = img.width
  536. 536 let h = img.height
  537. 537 let n = w * h
  538. 538 let bpp = ch * 4
  539. 539● let out : Array[Byte] = Array::make(n * bpp, b'\x00')
  540. 540 for c_idx in 0..<color_ch {
  541. 541● let hist : Array[Int32] = Array::make(256, 0)
  542. 542● let src : Array[Float] = Array::make(n, 0.0F)
  543. 543 for i in 0..<n {
  544. 544● let v = read_float_le(img.data, i * bpp + c_idx * 4)
  545. 545 src[i] = v
  546. 546 let bin = if v < 0.0F {
  547. 547○ 0
  548. 548● } else if v >= 1.0F {
  549. 549○ 255
  550. 550 } else {
  551. 551● (v * 255.0F + 0.5F).to_int()
  552. 552 }
  553. 553 hist[bin] += 1
  554. 554 }
  555. 555 let mut total = 0
  556. 556 let mut sum = 0
  557. 557 for t in 0..<256 {
  558. 558● total += hist[t]
  559. 559 sum += t * hist[t]
  560. 560 }
  561. 561 let mut best_t = 0
  562. 562 let mut best_var = -1.0F
  563. 563 let mut w0 = 0
  564. 564 let mut sum0 = 0
  565. 565 for t in 0..<256 {
  566. 566● w0 += hist[t]
  567. 567 sum0 += t * hist[t]
  568. 568 if w0 == 0 || w0 == total {
  569. 569● continue
  570. 570 }
  571. 571 let w1 = total - w0
  572. 572 let sum1 = sum - sum0
  573. 573● let mean0 = Float::from_int(sum0) / Float::from_int(w0)
  574. 574● let mean1 = Float::from_int(sum1) / Float::from_int(w1)
  575. 575● let var_between = Float::from_int(w0) *
  576. 576● Float::from_int(w1) *
  577. 577 (mean0 - mean1) *
  578. 578 (mean0 - mean1)
  579. 579 if var_between > best_var {
  580. 580● best_var = var_between
  581. 581 best_t = t
  582. 582 }
  583. 583 }
  584. 584● let threshold_f = Float::from_int(best_t) / 255.0F
  585. 585 for i in 0..<n {
  586. 586● if src[i] > threshold_f {
  587. 587● write_f32_le(out, i * bpp + c_idx * 4, 1.0F)
  588. 588 }
  589. 589 }
  590. 590 }
  591. 591 if ch == 4 {
  592. 592● for i in 0..<n {
  593. 593● let alpha_off = i * bpp + 12
  594. 594 for b in 0..<4 {
  595. 595● out[alpha_off + b] = img.data[alpha_off + b]
  596. 596 }
  597. 597 }
  598. 598 }
  599. 599 @types.ImageF::{
  600. 600 width: w,
  601. 601 height: h,
  602. 602 channels: ch,
  603. 603● data: Bytes::from_array(out),
  604. 604 }
  605. 605}