image.process\feature\image_hash.mbt

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