image.process\feature\glcm.mbt

81.6% · 62 / 76 points · 239 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/// # 灰度共生矩阵 glcm
  3. 3///
  4. 4/// 图像处理算法模块
  5. 5/// 图像处理算法模块 Image processing algorithm module
  6. 6// GLCM 灰度共生矩阵纹理分析(纯 MoonBit)
  7. 7
  8. 8///|
  9. 9/// GLCM 纹理特征
  10. 10pub struct GlcmFeatures {
  11. 11 contrast : Float // 对比度
  12. 12 correlation : Float // 相关性
  13. 13 energy : Float // 能量(ASM 的平方根)
  14. 14 homogeneity : Float // 同质性
  15. 15 entropy : Float // 熵
  16. 16 asm_value : Float // 角二阶矩
  17. 17 dissimilarity : Float // 不相似性
  18. 18}
  19. 19
  20. 20///|
  21. 21/// 计算灰度共生矩阵(GLCM)。
  22. 22/// 适用于灰度图(channels=1)或 RGB/RGBA 图(channels=3/4,自动转灰度)。
  23. 23/// dx, dy 为偏移量(如 (1,0) 为水平 0°,(0,1) 为垂直 90°)。
  24. 24/// levels 为量化级别数(默认 256,可降采样以加速)。
  25. 25pub fn compute_glcm(
  26. 26 img : @types.Image,
  27. 27 dx : Int32,
  28. 28 dy : Int32,
  29. 29 levels~ : Int32 = 256,
  30. 30) -> Array[Array[Int32]] raise @types.LoadError {
  31. 31 // 输入验证
  32. 32● if img.width <= 0 || img.height <= 0 {
  33. 33● raise @types.LoadError::DecodeFailed(
  34. 34 "compute_glcm: 图像尺寸必须大于0",
  35. 35 )
  36. 36 }
  37. 37 if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  38. 38○ raise @types.LoadError::DecodeFailed(
  39. 39 "compute_glcm: 仅支持 channels=1/3/4",
  40. 40 )
  41. 41 }
  42. 42 let expected_size = img.width * img.height * img.channels
  43. 43● if img.data.length() != expected_size {
  44. 44○ raise @types.LoadError::DecodeFailed(
  45. 45 "compute_glcm: 数据大小不匹配,期望 " +
  46. 46○ expected_size.to_string() +
  47. 47 ",实际 " +
  48. 48○ img.data.length().to_string(),
  49. 49 )
  50. 50 }
  51. 51 if dx == 0 && dy == 0 {
  52. 52● raise @types.LoadError::DecodeFailed(
  53. 53 "compute_glcm: dx和dy不能同时为0",
  54. 54 )
  55. 55 }
  56. 56 if levels < 2 || levels > 256 {
  57. 57● raise @types.LoadError::DecodeFailed("levels 必须在 2..256 之间")
  58. 58 }
  59. 59 let w = img.width
  60. 60 let h = img.height
  61. 61 let n = w * h
  62. 62 if n == 0 {
  63. 63○ return []
  64. 64 }
  65. 65 // 转灰度并量化
  66. 66● let gray : Array[Int32] = Array::make(n, 0)
  67. 67 let ch = img.channels
  68. 68● let data_int : Array[Int32] = Array::make(n * ch, 0)
  69. 69 for i in 0..<(n * ch) {
  70. 70● data_int[i] = img.data[i].to_int()
  71. 71 }
  72. 72● let scale = if levels < 256 { 256 / levels } else { 1 }
  73. 73 for i in 0..<n {
  74. 74● let v = if ch == 1 {
  75. 75● data_int[i]
  76. 76 } else {
  77. 77● let r = data_int[i * ch]
  78. 78 let g = data_int[i * ch + 1]
  79. 79 let b = data_int[i * ch + 2]
  80. 80 (r * 299 + g * 587 + b * 114) / 1000
  81. 81 }
  82. 82● gray[i] = if scale > 1 { v / scale } else { v }
  83. 83 if gray[i] >= levels {
  84. 84○ gray[i] = levels - 1
  85. 85 }
  86. 86 }
  87. 87 // 构建 GLCM
  88. 88● let glcm : Array[Array[Int32]] = Array::make(levels, [])
  89. 89 for i in 0..<levels {
  90. 90● glcm[i] = Array::make(levels, 0)
  91. 91 }
  92. 92 for y in 0..<h {
  93. 93● for x in 0..<w {
  94. 94● let nx = x + dx
  95. 95 let ny = y + dy
  96. 96 if nx >= 0 && nx < w && ny >= 0 && ny < h {
  97. 97● let v1 = gray[y * w + x]
  98. 98 let v2 = gray[ny * w + nx]
  99. 99 glcm[v1][v2] += 1
  100. 100 }
  101. 101 }
  102. 102 }
  103. 103 glcm
  104. 104}
  105. 105
  106. 106///|
  107. 107/// 从 GLCM 计算纹理特征。
  108. 108pub fn glcm_features(glcm : Array[Array[Int32]]) -> GlcmFeatures {
  109. 109● let levels = glcm.length()
  110. 110 if levels == 0 {
  111. 111● return GlcmFeatures::{
  112. 112 contrast: 0.0F,
  113. 113 correlation: 0.0F,
  114. 114 energy: 0.0F,
  115. 115 homogeneity: 0.0F,
  116. 116 entropy: 0.0F,
  117. 117 asm_value: 0.0F,
  118. 118 dissimilarity: 0.0F,
  119. 119 }
  120. 120 }
  121. 121 // 归一化
  122. 122 let mut total = 0.0F
  123. 123 for i in 0..<levels {
  124. 124● for j in 0..<levels {
  125. 125● total += Float::from_int(glcm[i][j])
  126. 126 }
  127. 127 }
  128. 128 if total == 0.0F {
  129. 129● return GlcmFeatures::{
  130. 130 contrast: 0.0F,
  131. 131 correlation: 0.0F,
  132. 132 energy: 0.0F,
  133. 133 homogeneity: 0.0F,
  134. 134 entropy: 0.0F,
  135. 135 asm_value: 0.0F,
  136. 136 dissimilarity: 0.0F,
  137. 137 }
  138. 138 }
  139. 139 // 计算均值和标准差
  140. 140 let mut mean_i = 0.0F
  141. 141 let mut mean_j = 0.0F
  142. 142 for i in 0..<levels {
  143. 143● for j in 0..<levels {
  144. 144● let p = Float::from_int(glcm[i][j]) / total
  145. 145● mean_i += Float::from_int(i) * p
  146. 146● mean_j += Float::from_int(j) * p
  147. 147 }
  148. 148 }
  149. 149 let mut var_i = 0.0F
  150. 150 let mut var_j = 0.0F
  151. 151 for i in 0..<levels {
  152. 152● for j in 0..<levels {
  153. 153● let p = Float::from_int(glcm[i][j]) / total
  154. 154● var_i += p * (Float::from_int(i) - mean_i) * (Float::from_int(i) - mean_i)
  155. 155● var_j += p * (Float::from_int(j) - mean_j) * (Float::from_int(j) - mean_j)
  156. 156 }
  157. 157 }
  158. 158● let std_i = var_i.sqrt()
  159. 159● let std_j = var_j.sqrt()
  160. 160 // 计算特征
  161. 161 let mut contrast = 0.0F
  162. 162 let mut correlation = 0.0F
  163. 163 let mut asm_val = 0.0F
  164. 164 let mut homogeneity = 0.0F
  165. 165 let mut entropy = 0.0F
  166. 166 let mut dissimilarity = 0.0F
  167. 167 for i in 0..<levels {
  168. 168● for j in 0..<levels {
  169. 169● let p = Float::from_int(glcm[i][j]) / total
  170. 170 if p > 0.0F {
  171. 171● let diff = Float::from_int(i - j)
  172. 172● let abs_diff = diff.abs()
  173. 173 contrast += p * diff * diff
  174. 174 dissimilarity += p * abs_diff
  175. 175 asm_val += p * p
  176. 176 homogeneity += p / (1.0F + diff * diff)
  177. 177● entropy += -p * Float::from_double(@math.log2(p.to_double()))
  178. 178 if std_i > 0.0F && std_j > 0.0F {
  179. 179● correlation += p *
  180. 180● (Float::from_int(i) - mean_i) *
  181. 181● (Float::from_int(j) - mean_j) /
  182. 182 (std_i * std_j)
  183. 183 }
  184. 184 }
  185. 185 }
  186. 186 }
  187. 187 GlcmFeatures::{
  188. 188 contrast,
  189. 189 correlation,
  190. 190● energy: asm_val.sqrt(),
  191. 191 homogeneity,
  192. 192 entropy,
  193. 193 asm_value: asm_val,
  194. 194 dissimilarity,
  195. 195 }
  196. 196}
  197. 197
  198. 198///|
  199. 199/// 计算多方向 GLCM 特征(0°, 45°, 90°, 135°)。
  200. 200/// 返回 4 个方向的纹理特征数组。
  201. 201pub fn glcm_features_multi_direction(
  202. 202 img : @types.Image,
  203. 203 levels~ : Int32 = 256,
  204. 204) -> Array[GlcmFeatures] raise @types.LoadError {
  205. 205 // 输入验证
  206. 206● if img.width <= 0 || img.height <= 0 {
  207. 207○ raise @types.LoadError::DecodeFailed(
  208. 208 "glcm_features_multi_direction: 图像尺寸必须大于0",
  209. 209 )
  210. 210 }
  211. 211 if img.channels != 1 && img.channels != 3 && img.channels != 4 {
  212. 212○ raise @types.LoadError::DecodeFailed(
  213. 213 "glcm_features_multi_direction: 仅支持 channels=1/3/4",
  214. 214 )
  215. 215 }
  216. 216 let expected_size = img.width * img.height * img.channels
  217. 217● if img.data.length() != expected_size {
  218. 218○ raise @types.LoadError::DecodeFailed(
  219. 219 "glcm_features_multi_direction: 数据大小不匹配,期望 " +
  220. 220○ expected_size.to_string() +
  221. 221 ",实际 " +
  222. 222○ img.data.length().to_string(),
  223. 223 )
  224. 224 }
  225. 225 if levels < 2 || levels > 256 {
  226. 226○ raise @types.LoadError::DecodeFailed(
  227. 227 "glcm_features_multi_direction: levels必须在2..256之间",
  228. 228 )
  229. 229 }
  230. 230
  231. 231 let directions = [(1, 0), (1, 1), (0, 1), (-1, 1)]
  232. 232 let result : Array[GlcmFeatures] = []
  233. 233 for dir in directions {
  234. 234● let (dx, dy) = dir
  235. 235● let glcm = compute_glcm(img, dx, dy, levels~)
  236. 236● result.push(glcm_features(glcm))
  237. 237 }
  238. 238 result
  239. 239}