image.process\color\clahe.mbt

90.8% · 69 / 76 points · 181 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/// # CLAHE 自适应直方图均衡 clahe
  3. 3///
  4. 4/// 图像处理算法模块
  5. 5/// 图像处理算法模块 Image processing algorithm module
  6. 6// CLAHE: 对比度受限自适应直方图均衡(纯 MoonBit)
  7. 7
  8. 8///|
  9. 9/// CLAHE(Contrast Limited Adaptive Histogram Equalization)。
  10. 10/// 将图像分成 tile_size × tile_size 的块,每块做直方图均衡,用双线性插值平滑拼接。
  11. 11/// clip_limit 控制对比度增强强度,值越大增强越强(典型值 2.0~4.0)。
  12. 12/// 仅处理灰度图(channels=1)或 RGB 图(channels=3/4),alpha 通道不变。
  13. 13pub fn clahe(
  14. 14 img : @types.Image,
  15. 15 tile_size : Int32,
  16. 16 clip_limit : Float,
  17. 17) -> @types.Image raise @types.LoadError {
  18. 18 // 输入验证
  19. 19● if img.width <= 0 || img.height <= 0 {
  20. 20● raise @types.LoadError::DecodeFailed("clahe: 图像尺寸必须大于0")
  21. 21 }
  22. 22 if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  23. 23● raise @types.LoadError::DecodeFailed("clahe 仅支持 channels=1/3/4")
  24. 24 }
  25. 25 let expected_size = img.width * img.height * img.channels
  26. 26● if img.data.length() != expected_size {
  27. 27○ raise @types.LoadError::DecodeFailed(
  28. 28 "clahe: 数据大小不匹配,期望 " +
  29. 29○ expected_size.to_string() +
  30. 30 ",实际 " +
  31. 31○ img.data.length().to_string(),
  32. 32 )
  33. 33 }
  34. 34 if tile_size < 2 {
  35. 35● raise @types.LoadError::DecodeFailed("tile_size 必须 >= 2")
  36. 36 }
  37. 37 if clip_limit <= 0.0 {
  38. 38● raise @types.LoadError::DecodeFailed("clip_limit 必须大于0")
  39. 39 }
  40. 40 let ch = img.channels
  41. 41● let color_ch = if ch >= 3 { 3 } else { 1 }
  42. 42 let n = img.width * img.height
  43. 43● let out : Array[Byte] = Array::make(n * ch, b'\x00')
  44. 44 for c in 0..<color_ch {
  45. 45● clahe_channel(img, out, c, ch, tile_size, clip_limit)
  46. 46 }
  47. 47 if ch == 4 {
  48. 48● for i in 0..<n {
  49. 49● out[i * 4 + 3] = img.data[i * 4 + 3]
  50. 50 }
  51. 51 }
  52. 52 @types.Image::{
  53. 53 width: img.width,
  54. 54 height: img.height,
  55. 55 channels: ch,
  56. 56● data: Bytes::from_array(out),
  57. 57 }
  58. 58}
  59. 59
  60. 60///|
  61. 61fn clahe_channel(
  62. 62 img : @types.Image,
  63. 63 out : Array[Byte],
  64. 64 channel : Int32,
  65. 65 ch : Int32,
  66. 66 tile_size : Int32,
  67. 67 clip_limit : Float,
  68. 68) -> Unit {
  69. 69● let w = img.width
  70. 70 let h = img.height
  71. 71 let n = w * h
  72. 72 // 预计算 to_int 数组
  73. 73● let data_int : Array[Int32] = Array::make(n * ch, 0)
  74. 74 for i in 0..<(n * ch) {
  75. 75● data_int[i] = img.data[i].to_int()
  76. 76 }
  77. 77 // 块数
  78. 78 let ntx = (w + tile_size - 1) / tile_size
  79. 79 let nty = (h + tile_size - 1) / tile_size
  80. 80 let n_tiles = ntx * nty
  81. 81● let luts : Array[Array[Int32]] = Array::make(n_tiles, Array::make(256, 0))
  82. 82 // 计算每块的直方图和 LUT
  83. 83 for ty in 0..<nty {
  84. 84● for tx in 0..<ntx {
  85. 85● let hist : Array[Int32] = Array::make(256, 0)
  86. 86 let x0 = tx * tile_size
  87. 87 let y0 = ty * tile_size
  88. 88● let x1 = if x0 + tile_size < w { x0 + tile_size } else { w }
  89. 89● let y1 = if y0 + tile_size < h { y0 + tile_size } else { h }
  90. 90 let tile_pixels = (x1 - x0) * (y1 - y0)
  91. 91 for y in y0..<y1 {
  92. 92● let row_base = y * w
  93. 93 for x in x0..<x1 {
  94. 94● let v = data_int[(row_base + x) * ch + channel]
  95. 95 hist[v] += 1
  96. 96 }
  97. 97 }
  98. 98 // 裁剪直方图
  99. 99● let clip_count = (Float::from_int(tile_pixels) * clip_limit / 256.0).to_int()
  100. 100 let mut excess = 0
  101. 101 for i in 0..<256 {
  102. 102● if hist[i] > clip_count {
  103. 103● excess += hist[i] - clip_count
  104. 104 hist[i] = clip_count
  105. 105 }
  106. 106 }
  107. 107 // 均匀分配多余的像素
  108. 108 let redistribute = excess / 256
  109. 109 let mut remainder = excess - redistribute * 256
  110. 110 for i in 0..<256 {
  111. 111● hist[i] += redistribute
  112. 112 if remainder > 0 {
  113. 113● hist[i] += 1
  114. 114 remainder -= 1
  115. 115 }
  116. 116 }
  117. 117 // 计算 CDF 并归一化到 0-255
  118. 118 let lut = luts[ty * ntx + tx]
  119. 119 let mut cdf = 0
  120. 120 for i in 0..<256 {
  121. 121● cdf += hist[i]
  122. 122 lut[i] = if tile_pixels > 0 {
  123. 123● @types.clamp_int(cdf * 255 / tile_pixels, 0, 255)
  124. 124 } else {
  125. 125○ i
  126. 126 }
  127. 127 }
  128. 128 }
  129. 129 }
  130. 130 // 预计算 tile 坐标和权重
  131. 131● let inv_tile_size = 1.0F / Float::from_int(tile_size)
  132. 132● let tx_arr : Array[Int32] = Array::make(w, 0)
  133. 133● let wx_arr : Array[Float] = Array::make(w, 0.0F)
  134. 134 for x in 0..<w {
  135. 135● let fx = Float::from_int(x) * inv_tile_size - 0.5F
  136. 136● let t = fx.to_int()
  137. 137 tx_arr[x] = t
  138. 138● wx_arr[x] = fx - Float::from_int(t)
  139. 139 }
  140. 140● let ty_arr : Array[Int32] = Array::make(h, 0)
  141. 141● let wy_arr : Array[Float] = Array::make(h, 0.0F)
  142. 142 for y in 0..<h {
  143. 143● let fy = Float::from_int(y) * inv_tile_size - 0.5F
  144. 144● let t = fy.to_int()
  145. 145 ty_arr[y] = t
  146. 146● wy_arr[y] = fy - Float::from_int(t)
  147. 147 }
  148. 148 // 双线性插值应用 LUT
  149. 149 for y in 0..<h {
  150. 150● let ty = ty_arr[y]
  151. 151 let wy = wy_arr[y]
  152. 152 let row_base = y * w
  153. 153 let wy_inv = 1.0F - wy
  154. 154 for x in 0..<w {
  155. 155● let tx = tx_arr[x]
  156. 156 let wx = wx_arr[x]
  157. 157 let v = data_int[(row_base + x) * ch + channel]
  158. 158 // 4 个相邻块
  159. 159● let t00 = tile_clamp(tx, ty, ntx, nty)
  160. 160● let t01 = tile_clamp(tx + 1, ty, ntx, nty)
  161. 161● let t10 = tile_clamp(tx, ty + 1, ntx, nty)
  162. 162● let t11 = tile_clamp(tx + 1, ty + 1, ntx, nty)
  163. 163● let v00 = Float::from_int(luts[t00][v])
  164. 164● let v01 = Float::from_int(luts[t01][v])
  165. 165● let v10 = Float::from_int(luts[t10][v])
  166. 166● let v11 = Float::from_int(luts[t11][v])
  167. 167 let wx_inv = 1.0F - wx
  168. 168 let top = v00 * wx_inv + v01 * wx
  169. 169 let bot = v10 * wx_inv + v11 * wx
  170. 170● let result = @types.clamp_int((top * wy_inv + bot * wy).to_int(), 0, 255)
  171. 171● out[(row_base + x) * ch + channel] = result.to_byte()
  172. 172 }
  173. 173 }
  174. 174}
  175. 175
  176. 176///|
  177. 177fn tile_clamp(tx : Int32, ty : Int32, ntx : Int32, nty : Int32) -> Int32 {
  178. 178◐ let cx = if tx < 0 { 0 } else if tx >= ntx { ntx - 1 } else { tx }
  179. 179◐ let cy = if ty < 0 { 0 } else if ty >= nty { nty - 1 } else { ty }
  180. 180 cy * ntx + cx
  181. 181}