image.process\segment\slic.mbt

97.7% · 85 / 87 points · 170 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/// # SLIC 超像素 slic
  3. 3///
  4. 4/// 图像处理算法模块
  5. 5/// 图像处理算法模块 Image processing algorithm module
  6. 6// SLIC 超像素分割(Simple Linear Iterative Clustering)
  7. 7// 纯 MoonBit 实现,基于 Achanta et al. 2012
  8. 8
  9. 9///|
  10. 10/// SLIC 超像素分割结果
  11. 11#derive(Eq, @debug.Debug)
  12. 12pub(all) struct SuperpixelResult {
  13. 13 labels : Array[Int32]
  14. 14 centers : Array[(Int32, Int32)]
  15. 15 num_labels : Int32
  16. 16}
  17. 17
  18. 18///|
  19. 19pub extend SuperpixelResult with @debug.Debug::{to_repr}
  20. 20
  21. 21///|
  22. 22pub extend SuperpixelResult with Eq::{not_equal, equal}
  23. 23
  24. 24///|
  25. 25fn sqrt_d(x : Double) -> Double {
  26. 26● if x <= 0.0 {
  27. 27○ return 0.0
  28. 28 }
  29. 29 let mut guess = x
  30. 30 for _ in 0..<20 {
  31. 31● guess = (guess + x / guess) / 2.0
  32. 32 }
  33. 33 guess
  34. 34}
  35. 35
  36. 36///|
  37. 37/// SLIC 超像素分割
  38. 38/// k: 期望超像素数量, m: 紧凑度参数(通常10-40), max_iters: 迭代次数
  39. 39pub fn slic(
  40. 40 img : @types.Image,
  41. 41 k : Int32,
  42. 42 m : Int32,
  43. 43 max_iters : Int32,
  44. 44) -> SuperpixelResult raise @types.LoadError {
  45. 45● let w = img.width
  46. 46 let h = img.height
  47. 47 let ch = img.channels
  48. 48 if k <= 0 || w <= 0 || h <= 0 {
  49. 49● raise @types.LoadError::DecodeFailed(
  50. 50 "SLIC 参数无效:k/w/h 必须为正数",
  51. 51 )
  52. 52 }
  53. 53 if max_iters <= 0 {
  54. 54● raise @types.LoadError::DecodeFailed(
  55. 55 "SLIC 参数无效:max_iters 必须为正数",
  56. 56 )
  57. 57 }
  58. 58 let n = w * h
  59. 59● let s = sqrt_d(n.to_double() / k.to_double())
  60. 60◐ let s_int = if s.to_int() < 1 { 1 } else { s.to_int() }
  61. 61 let s2 = s * s
  62. 62● let m_d = m.to_double()
  63. 63 let m2 = m_d * m_d
  64. 64 let centers_x : Array[Int32] = []
  65. 65 let centers_y : Array[Int32] = []
  66. 66 let centers_l : Array[Double] = []
  67. 67 let centers_a : Array[Double] = []
  68. 68 let centers_b : Array[Double] = []
  69. 69 let half_s = s_int / 2
  70. 70 let mut cy = half_s
  71. 71 while cy < h {
  72. 72● let mut cx = half_s
  73. 73 while cx < w {
  74. 74● let idx = (cy * w + cx) * ch
  75. 75● let r = img.data[idx].to_int()
  76. 76● let g = if ch > 1 { img.data[idx + 1].to_int() } else { r }
  77. 77● let b = if ch > 2 { img.data[idx + 2].to_int() } else { r }
  78. 78● let (lab_l, lab_a, lab_b) = @color.rgb_to_lab(r, g, b)
  79. 79● centers_x.push(cx)
  80. 80● centers_y.push(cy)
  81. 81● centers_l.push(lab_l.to_double())
  82. 82● centers_a.push(lab_a.to_double())
  83. 83● centers_b.push(lab_b.to_double())
  84. 84 cx += s_int
  85. 85 }
  86. 86 cy += s_int
  87. 87 }
  88. 88● let num_centers = centers_x.length()
  89. 89● let labels = Array::make(n, -1)
  90. 90● let dists = Array::make(n, 1.0e20)
  91. 91 for _ in 0..<max_iters {
  92. 92● for i in 0..<n {
  93. 93● dists[i] = 1.0e20
  94. 94 }
  95. 95 for k_idx in 0..<num_centers {
  96. 96● let cx = centers_x[k_idx]
  97. 97 let cy = centers_y[k_idx]
  98. 98 let cl = centers_l[k_idx]
  99. 99 let ca = centers_a[k_idx]
  100. 100 let cb = centers_b[k_idx]
  101. 101● let y_start = if cy - s_int < 0 { 0 } else { cy - s_int }
  102. 102● let y_end = if cy + s_int >= h { h - 1 } else { cy + s_int }
  103. 103● let x_start = if cx - s_int < 0 { 0 } else { cx - s_int }
  104. 104● let x_end = if cx + s_int >= w { w - 1 } else { cx + s_int }
  105. 105 for y in y_start..<=y_end {
  106. 106● for x in x_start..<=x_end {
  107. 107● let pidx = y * w + x
  108. 108 let data_idx = pidx * ch
  109. 109● let r = img.data[data_idx].to_int()
  110. 110● let g = if ch > 1 { img.data[data_idx + 1].to_int() } else { r }
  111. 111● let b = if ch > 2 { img.data[data_idx + 2].to_int() } else { r }
  112. 112● let (pl, pa, pb) = @color.rgb_to_lab(r, g, b)
  113. 113● let dl = pl.to_double() - cl
  114. 114● let da = pa.to_double() - ca
  115. 115● let db = pb.to_double() - cb
  116. 116 let d_lab2 = dl * dl + da * da + db * db
  117. 117● let dx = (x - cx).to_double()
  118. 118● let dy = (y - cy).to_double()
  119. 119 let d_xy2 = dx * dx + dy * dy
  120. 120 let d2 = d_lab2 + d_xy2 / s2 * m2
  121. 121 if d2 < dists[pidx] {
  122. 122● dists[pidx] = d2
  123. 123 labels[pidx] = k_idx
  124. 124 }
  125. 125 }
  126. 126 }
  127. 127 }
  128. 128● let sum_x = Array::make(num_centers, 0)
  129. 129● let sum_y = Array::make(num_centers, 0)
  130. 130● let sum_l = Array::make(num_centers, 0.0)
  131. 131● let sum_a = Array::make(num_centers, 0.0)
  132. 132● let sum_b = Array::make(num_centers, 0.0)
  133. 133● let count = Array::make(num_centers, 0)
  134. 134 for y in 0..<h {
  135. 135● for x in 0..<w {
  136. 136● let pidx = y * w + x
  137. 137 let l = labels[pidx]
  138. 138 if l >= 0 {
  139. 139● sum_x[l] += x
  140. 140 sum_y[l] += y
  141. 141 count[l] += 1
  142. 142 let data_idx = pidx * ch
  143. 143● let r = img.data[data_idx].to_int()
  144. 144● let g = if ch > 1 { img.data[data_idx + 1].to_int() } else { r }
  145. 145● let b = if ch > 2 { img.data[data_idx + 2].to_int() } else { r }
  146. 146● let (pl, pa, pb) = @color.rgb_to_lab(r, g, b)
  147. 147● sum_l[l] += pl.to_double()
  148. 148● sum_a[l] += pa.to_double()
  149. 149● sum_b[l] += pb.to_double()
  150. 150 }
  151. 151 }
  152. 152 }
  153. 153 for k_idx in 0..<num_centers {
  154. 154● let cnt = count[k_idx]
  155. 155 if cnt > 0 {
  156. 156● centers_x[k_idx] = sum_x[k_idx] / cnt
  157. 157 centers_y[k_idx] = sum_y[k_idx] / cnt
  158. 158● let cnt_d = cnt.to_double()
  159. 159 centers_l[k_idx] = sum_l[k_idx] / cnt_d
  160. 160 centers_a[k_idx] = sum_a[k_idx] / cnt_d
  161. 161 centers_b[k_idx] = sum_b[k_idx] / cnt_d
  162. 162 }
  163. 163 }
  164. 164 }
  165. 165 let centers : Array[(Int32, Int32)] = []
  166. 166 for k_idx in 0..<num_centers {
  167. 167● centers.push((centers_x[k_idx], centers_y[k_idx]))
  168. 168 }
  169. 169 SuperpixelResult::{ labels, centers, num_labels: num_centers, }
  170. 170}