image.process\feature\image_hash.mbt
80% · 68 / 85 points · 216 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 图像哈希 image hash - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// 感知哈希(纯 MoonBit):ahash / dhash / phash / hamming_distance - 7
- 8
///| - 9
/// 均值哈希(aHash)。将图像缩放至 8×8 灰度,与均值比较生成 64 位哈希。 - 10
pub fn ahash(img : @types.Image) -> Array[Int32] raise @types.LoadError { - 11
// 输入验证 - 12●
if img.width <= 0 || img.height <= 0 { - 13●
raise @types.LoadError::DecodeFailed("ahash: 图像尺寸必须大于0") - 14
} - 15
if img.channels < 1 || img.channels > 4 { - 16○
raise @types.LoadError::DecodeFailed("ahash: 通道数必须在1-4之间") - 17
} - 18
let expected_size = img.width * img.height * img.channels - 19●
if img.data.length() != expected_size { - 20●
raise @types.LoadError::DecodeFailed( - 21
"ahash: 数据大小不匹配,期望 " + - 22●
expected_size.to_string() + - 23
",实际 " + - 24●
img.data.length().to_string(), - 25
) - 26
} - 27
- 28
// 缩放到 8x8 灰度 - 29●
let small = resize_to_gray_8x8(img) - 30
let mut sum = 0 - 31
for i in 0..<64 { - 32●
sum += small[i] - 33
} - 34
let mean = sum / 64 - 35●
let hash : Array[Int32] = Array::make(64, 0) - 36
for i in 0..<64 { - 37●
hash[i] = if small[i] > mean { 1 } else { 0 } - 38
} - 39
hash - 40
} - 41
- 42
///| - 43
/// 差值哈希(dHash)。将图像缩放至 9×8 灰度,比较水平相邻像素差。 - 44
pub fn dhash(img : @types.Image) -> Array[Int32] raise @types.LoadError { - 45
// 输入验证 - 46●
if img.width <= 0 || img.height <= 0 { - 47●
raise @types.LoadError::DecodeFailed("dhash: 图像尺寸必须大于0") - 48
} - 49
if img.channels < 1 || img.channels > 4 { - 50○
raise @types.LoadError::DecodeFailed("dhash: 通道数必须在1-4之间") - 51
} - 52
let expected_size = img.width * img.height * img.channels - 53●
if img.data.length() != expected_size { - 54○
raise @types.LoadError::DecodeFailed( - 55
"dhash: 数据大小不匹配,期望 " + - 56○
expected_size.to_string() + - 57
",实际 " + - 58○
img.data.length().to_string(), - 59
) - 60
} - 61
- 62●
let small = resize_to_gray_9x8(img) - 63●
let hash : Array[Int32] = Array::make(64, 0) - 64
for row in 0..<8 { - 65●
for col in 0..<8 { - 66●
hash[row * 8 + col] = if small[row * 9 + col] > small[row * 9 + col + 1] { - 67●
1 - 68
} else { - 69●
0 - 70
} - 71
} - 72
} - 73
hash - 74
} - 75
- 76
///| - 77
/// 感知哈希(pHash 简化版)。将图像缩放至 8×8 灰度,用 DCT 近似:取每行均值与全局均值比较。 - 78
/// 完整 pHash 需 2D DCT,此处用简化版本(行列均值差分),足以满足去重需求。 - 79
pub fn phash(img : @types.Image) -> Array[Int32] raise @types.LoadError { - 80
// 输入验证 - 81●
if img.width <= 0 || img.height <= 0 { - 82●
raise @types.LoadError::DecodeFailed("phash: 图像尺寸必须大于0") - 83
} - 84
if img.channels < 1 || img.channels > 4 { - 85○
raise @types.LoadError::DecodeFailed("phash: 通道数必须在1-4之间") - 86
} - 87
let expected_size = img.width * img.height * img.channels - 88●
if img.data.length() != expected_size { - 89○
raise @types.LoadError::DecodeFailed( - 90
"phash: 数据大小不匹配,期望 " + - 91○
expected_size.to_string() + - 92
",实际 " + - 93○
img.data.length().to_string(), - 94
) - 95
} - 96
- 97●
let small = resize_to_gray_8x8(img) - 98
// 简化 DCT:计算行均值和列均值 - 99●
let row_means : Array[Int32] = Array::make(8, 0) - 100●
let col_means : Array[Int32] = Array::make(8, 0) - 101
for r in 0..<8 { - 102●
let mut s = 0 - 103
for c in 0..<8 { - 104●
s += small[r * 8 + c] - 105
} - 106
row_means[r] = s / 8 - 107
} - 108
for c in 0..<8 { - 109●
let mut s = 0 - 110
for r in 0..<8 { - 111●
s += small[r * 8 + c] - 112
} - 113
col_means[c] = s / 8 - 114
} - 115
// 全局均值 - 116
let mut total = 0 - 117
for i in 0..<8 { - 118●
total += row_means[i] - 119
} - 120
let global_mean = total / 8 - 121
// 生成哈希:行均值和列均值与全局均值比较 - 122●
let hash : Array[Int32] = Array::make(64, 0) - 123
for i in 0..<8 { - 124●
hash[i] = if row_means[i] > global_mean { 1 } else { 0 } - 125
} - 126
for i in 0..<8 { - 127●
hash[8 + i] = if col_means[i] > global_mean { 1 } else { 0 } - 128
} - 129
// 填充剩余 48 位用像素级比较 - 130
for i in 0..<48 { - 131●
hash[16 + i] = if small[i + 16] > global_mean { 1 } else { 0 } - 132
} - 133
hash - 134
} - 135
- 136
///| - 137
/// 汉明距离。计算两个哈希的差异位数。 - 138
pub fn hamming_distance( - 139
h1 : Array[Int32], - 140
h2 : Array[Int32], - 141
) -> Int32 raise @types.LoadError { - 142
// 参数验证 - 143●
if h1.length() == 0 { - 144○
raise @types.LoadError::DecodeFailed( - 145
"hamming_distance: h1数组不能为空", - 146
) - 147
} - 148●
if h2.length() == 0 { - 149○
raise @types.LoadError::DecodeFailed( - 150
"hamming_distance: h2数组不能为空", - 151
) - 152
} - 153●
if h1.length() != h2.length() { - 154●
raise @types.LoadError::DecodeFailed( - 155
"hamming_distance: h1和h2长度必须相同", - 156
) - 157
} - 158
- 159●
let n = h1.length() - 160
let mut dist = 0 - 161
for i in 0..<n { - 162●
if h1[i] != h2[i] { - 163●
dist += 1 - 164
} - 165
} - 166
dist - 167
} - 168
- 169
///| - 170
fn resize_to_gray_8x8(img : @types.Image) -> Array[Int32] { - 171●
let ch = img.channels - 172
let w = img.width - 173
let h = img.height - 174●
let out : Array[Int32] = Array::make(64, 0) - 175
for y in 0..<8 { - 176●
for x in 0..<8 { - 177
// 最近邻采样 - 178●
let sx = x * w / 8 - 179
let sy = y * h / 8 - 180
let v = if ch == 1 { - 181●
img.data[sy * w + sx].to_int() - 182
} else { - 183●
let r = img.data[(sy * w + sx) * ch].to_int() - 184●
let g = img.data[(sy * w + sx) * ch + 1].to_int() - 185●
let b = img.data[(sy * w + sx) * ch + 2].to_int() - 186
(r * 299 + g * 587 + b * 114) / 1000 - 187
} - 188
out[y * 8 + x] = v - 189
} - 190
} - 191
out - 192
} - 193
- 194
///| - 195
fn resize_to_gray_9x8(img : @types.Image) -> Array[Int32] { - 196●
let ch = img.channels - 197
let w = img.width - 198
let h = img.height - 199●
let out : Array[Int32] = Array::make(72, 0) - 200
for y in 0..<8 { - 201●
for x in 0..<9 { - 202●
let sx = x * w / 9 - 203
let sy = y * h / 8 - 204
let v = if ch == 1 { - 205●
img.data[sy * w + sx].to_int() - 206
} else { - 207○
let r = img.data[(sy * w + sx) * ch].to_int() - 208○
let g = img.data[(sy * w + sx) * ch + 1].to_int() - 209○
let b = img.data[(sy * w + sx) * ch + 2].to_int() - 210
(r * 299 + g * 587 + b * 114) / 1000 - 211
} - 212
out[y * 9 + x] = v - 213
} - 214
} - 215
out - 216
}