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