image.process\edge\edge_detect.mbt

98.1% · 103 / 105 points · 297 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/// # 边缘检测 edge detect
  3. 3///
  4. 4/// 图像处理算法模块
  5. 5/// 图像处理算法模块 Image processing algorithm module
  6. 6// 边缘检测扩展(纯 MoonBit):edge_detect_laplacian / edge_detect_prewitt / edge_detect_sobel
  7. 7// @types.clamp_byte / @types.clamp_int 来自 walkzzz/image.core/types
  8. 8
  9. 9///|
  10. 10/// Sobel 边缘检测。先转灰度,然后计算梯度幅值。
  11. 11/// 输出为单通道灰度图(channels=1)。
  12. 12pub fn edge_detect_sobel(
  13. 13 img : @types.Image,
  14. 14) -> @types.Image raise @types.LoadError {
  15. 15 // 输入验证
  16. 16● if img.width <= 0 || img.height <= 0 {
  17. 17● raise @types.LoadError::DecodeFailed(
  18. 18 "edge_detect_sobel: 图像尺寸必须大于0",
  19. 19 )
  20. 20 }
  21. 21 if img.channels < 1 || img.channels > 4 {
  22. 22● raise @types.LoadError::DecodeFailed(
  23. 23 "edge_detect_sobel: 通道数必须在1-4之间",
  24. 24 )
  25. 25 }
  26. 26 let expected_size = img.width * img.height * img.channels
  27. 27● if img.data.length() != expected_size {
  28. 28● raise @types.LoadError::DecodeFailed(
  29. 29 "edge_detect_sobel: 数据大小不匹配,期望 " +
  30. 30● expected_size.to_string() +
  31. 31 ",实际 " +
  32. 32● img.data.length().to_string(),
  33. 33 )
  34. 34 }
  35. 35
  36. 36 let w = img.width
  37. 37 let h = img.height
  38. 38 let ch = img.channels
  39. 39 let n = w * h
  40. 40 // 先转灰度
  41. 41● let gray : Array[Int32] = Array::make(n, 0)
  42. 42 for i in 0..<n {
  43. 43● if ch == 1 {
  44. 44○ gray[i] = img.data[i].to_int()
  45. 45 } else {
  46. 46● let r = img.data[i * ch].to_int()
  47. 47● let g = img.data[i * ch + 1].to_int()
  48. 48● let b = img.data[i * ch + 2].to_int()
  49. 49 gray[i] = (r * 299 + g * 587 + b * 114) / 1000
  50. 50 }
  51. 51 }
  52. 52 // Sobel 卷积
  53. 53● let out : Array[Byte] = Array::make(n, b'\x00')
  54. 54 // 内部区域:1 <= x < w-1 && 1 <= y < h-1,无需 clamp
  55. 55 for y in 1..<(h - 1) {
  56. 56● for x in 1..<(w - 1) {
  57. 57● let idx = y * w + x
  58. 58 let idx_l = idx - 1
  59. 59 let idx_r = idx + 1
  60. 60 let idx_t = idx - w
  61. 61 let idx_b = idx + w
  62. 62 // Gx = [-1 0 1; -2 0 2; -1 0 1]
  63. 63 let gx = -gray[idx_t - 1] +
  64. 64 gray[idx_t + 1] -
  65. 65 2 * gray[idx_l] +
  66. 66 2 * gray[idx_r] -
  67. 67 gray[idx_b - 1] +
  68. 68 gray[idx_b + 1]
  69. 69 // Gy = [-1 -2 -1; 0 0 0; 1 2 1]
  70. 70 let gy = -gray[idx_t - 1] -
  71. 71 2 * gray[idx_t] -
  72. 72 gray[idx_t + 1] +
  73. 73 gray[idx_b - 1] +
  74. 74 2 * gray[idx_b] +
  75. 75 gray[idx_b + 1]
  76. 76● let mag = Float::from_int(gx * gx + gy * gy).sqrt()
  77. 77● out[idx] = (mag + 0.5F).to_int().clamp(min=0, max=255).to_byte()
  78. 78 }
  79. 79 }
  80. 80 // 边界区域
  81. 81 for y in 0..<h {
  82. 82● for x in 0..<w {
  83. 83● if x >= 1 && x < w - 1 && y >= 1 && y < h - 1 {
  84. 84● continue
  85. 85 }
  86. 86● let x_l = @types.clamp_int(x - 1, 0, w - 1)
  87. 87● let x_r = @types.clamp_int(x + 1, 0, w - 1)
  88. 88● let y_t = @types.clamp_int(y - 1, 0, h - 1)
  89. 89● let y_b = @types.clamp_int(y + 1, 0, h - 1)
  90. 90 let gx = -gray[y_t * w + x_l] +
  91. 91 gray[y_t * w + x_r] -
  92. 92 2 * gray[y * w + x_l] +
  93. 93 2 * gray[y * w + x_r] -
  94. 94 gray[y_b * w + x_l] +
  95. 95 gray[y_b * w + x_r]
  96. 96 let gy = -gray[y_t * w + x_l] -
  97. 97 2 * gray[y_t * w + x] -
  98. 98 gray[y_t * w + x_r] +
  99. 99 gray[y_b * w + x_l] +
  100. 100 2 * gray[y_b * w + x] +
  101. 101 gray[y_b * w + x_r]
  102. 102● let mag = Float::from_int(gx * gx + gy * gy).sqrt()
  103. 103● out[y * w + x] = (mag + 0.5F).to_int().clamp(min=0, max=255).to_byte()
  104. 104 }
  105. 105 }
  106. 106 @types.Image::{
  107. 107 width: w,
  108. 108 height: h,
  109. 109 channels: 1,
  110. 110● data: Bytes::from_array(out),
  111. 111 }
  112. 112}
  113. 113
  114. 114///|
  115. 115/// Laplacian 边缘检测。使用 3x3 Laplacian 核:
  116. 116/// 拉普拉斯核 [0, 1, 0; 1, -4, 1; 0, 1, 0]
  117. 117/// 返回灰度图(channels=1)。
  118. 118pub fn edge_detect_laplacian(
  119. 119 img : @types.Image,
  120. 120) -> @types.Image raise @types.LoadError {
  121. 121 // 输入验证
  122. 122● if img.width <= 0 || img.height <= 0 {
  123. 123● raise @types.LoadError::DecodeFailed(
  124. 124 "edge_detect_laplacian: 图像尺寸必须大于0",
  125. 125 )
  126. 126 }
  127. 127 if img.channels < 1 || img.channels > 4 {
  128. 128● raise @types.LoadError::DecodeFailed(
  129. 129 "edge_detect_laplacian: 通道数必须在1-4之间",
  130. 130 )
  131. 131 }
  132. 132 let expected_size = img.width * img.height * img.channels
  133. 133● if img.data.length() != expected_size {
  134. 134● raise @types.LoadError::DecodeFailed(
  135. 135 "edge_detect_laplacian: 数据大小不匹配,期望 " +
  136. 136● expected_size.to_string() +
  137. 137 ",实际 " +
  138. 138● img.data.length().to_string(),
  139. 139 )
  140. 140 }
  141. 141
  142. 142 let ch = img.channels
  143. 143 let w = img.width
  144. 144 let h = img.height
  145. 145 let n = w * h
  146. 146● let out : Array[Byte] = Array::make(n, b'\x00')
  147. 147 // 预计算灰度 Int 数组
  148. 148● let gray : Array[Int32] = Array::make(n, 0)
  149. 149 for i in 0..<n {
  150. 150● if ch >= 3 {
  151. 151● let r = img.data[i * ch].to_int()
  152. 152● let g = img.data[i * ch + 1].to_int()
  153. 153● let b = img.data[i * ch + 2].to_int()
  154. 154 gray[i] = (r * 77 + g * 150 + b * 29) / 256
  155. 155 } else {
  156. 156● gray[i] = img.data[i * ch].to_int()
  157. 157 }
  158. 158 }
  159. 159 // 内部区域:x in [1, w-1), y in [1, h-1),直接访问 4 邻域
  160. 160 for y in 1..<(h - 1) {
  161. 161● for x in 1..<(w - 1) {
  162. 162● let idx = y * w + x
  163. 163 let center = gray[idx]
  164. 164 let u = gray[idx - w]
  165. 165 let d = gray[idx + w]
  166. 166 let l = gray[idx - 1]
  167. 167 let r = gray[idx + 1]
  168. 168 let lap = u + d + l + r - 4 * center
  169. 169● out[idx] = @types.clamp_byte(lap.abs())
  170. 170 }
  171. 171 }
  172. 172 // 边界区域
  173. 173 for y in 0..<h {
  174. 174● let y_inner = y >= 1 && y < h - 1
  175. 175 for x in 0..<w {
  176. 176● if y_inner && x >= 1 && x < w - 1 {
  177. 177● continue
  178. 178 }
  179. 179● let cx = @types.clamp_int(x, 0, w - 1)
  180. 180● let cy = @types.clamp_int(y, 0, h - 1)
  181. 181● let up = @types.clamp_int(y - 1, 0, h - 1)
  182. 182● let down = @types.clamp_int(y + 1, 0, h - 1)
  183. 183● let left = @types.clamp_int(x - 1, 0, w - 1)
  184. 184● let right = @types.clamp_int(x + 1, 0, w - 1)
  185. 185 let center = gray[cy * w + cx]
  186. 186 let u = gray[up * w + cx]
  187. 187 let d = gray[down * w + cx]
  188. 188 let l = gray[cy * w + left]
  189. 189 let r = gray[cy * w + right]
  190. 190 let lap = u + d + l + r - 4 * center
  191. 191● out[y * w + x] = @types.clamp_byte(lap.abs())
  192. 192 }
  193. 193 }
  194. 194 @types.Image::{
  195. 195 width: w,
  196. 196 height: h,
  197. 197 channels: 1,
  198. 198● data: Bytes::from_array(out),
  199. 199 }
  200. 200}
  201. 201
  202. 202///|
  203. 203/// Prewitt 边缘检测。使用两个 3x3 Prewitt 核计算梯度幅值。
  204. 204/// Prewitt 水平梯度 Gx = [-1, 0, 1; -1, 0, 1; -1, 0, 1]
  205. 205/// Prewitt 垂直梯度 Gy = [-1, -1, -1; 0, 0, 0; 1, 1, 1]
  206. 206/// 返回灰度图(channels=1)。
  207. 207pub fn edge_detect_prewitt(
  208. 208 img : @types.Image,
  209. 209) -> @types.Image raise @types.LoadError {
  210. 210 // 输入验证
  211. 211● if img.width <= 0 || img.height <= 0 {
  212. 212● raise @types.LoadError::DecodeFailed(
  213. 213 "edge_detect_prewitt: 图像尺寸必须大于0",
  214. 214 )
  215. 215 }
  216. 216 if img.channels < 1 || img.channels > 4 {
  217. 217● raise @types.LoadError::DecodeFailed(
  218. 218 "edge_detect_prewitt: 通道数必须在1-4之间",
  219. 219 )
  220. 220 }
  221. 221 let expected_size = img.width * img.height * img.channels
  222. 222● if img.data.length() != expected_size {
  223. 223● raise @types.LoadError::DecodeFailed(
  224. 224 "edge_detect_prewitt: 数据大小不匹配,期望 " +
  225. 225● expected_size.to_string() +
  226. 226 ",实际 " +
  227. 227● img.data.length().to_string(),
  228. 228 )
  229. 229 }
  230. 230
  231. 231 let ch = img.channels
  232. 232 let w = img.width
  233. 233 let h = img.height
  234. 234 let n = w * h
  235. 235● let out : Array[Byte] = Array::make(n, b'\x00')
  236. 236 // 预计算灰度 Int 数组
  237. 237● let gray : Array[Int32] = Array::make(n, 0)
  238. 238 for i in 0..<n {
  239. 239● if ch >= 3 {
  240. 240● let r = img.data[i * ch].to_int()
  241. 241● let g = img.data[i * ch + 1].to_int()
  242. 242● let b = img.data[i * ch + 2].to_int()
  243. 243 gray[i] = (r * 77 + g * 150 + b * 29) / 256
  244. 244 } else {
  245. 245● gray[i] = img.data[i * ch].to_int()
  246. 246 }
  247. 247 }
  248. 248 // 内部区域:x in [1, w-1), y in [1, h-1)
  249. 249 for y in 1..<(h - 1) {
  250. 250● for x in 1..<(w - 1) {
  251. 251● let idx = y * w + x
  252. 252 let ul = gray[idx - w - 1]
  253. 253 let ur = gray[idx - w + 1]
  254. 254 let ll = gray[idx + w - 1]
  255. 255 let lr = gray[idx + w + 1]
  256. 256 let l = gray[idx - 1]
  257. 257 let r = gray[idx + 1]
  258. 258 let u = gray[idx - w]
  259. 259 let d = gray[idx + w]
  260. 260 let gx = ur + r + lr - ul - l - ll
  261. 261 let gy = ll + d + lr - ul - u - ur
  262. 262● let mag = Float::from_int(gx * gx + gy * gy).sqrt()
  263. 263● out[idx] = @types.clamp_byte(mag.to_int())
  264. 264 }
  265. 265 }
  266. 266 // 边界区域
  267. 267 for y in 0..<h {
  268. 268● let y_inner = y >= 1 && y < h - 1
  269. 269 for x in 0..<w {
  270. 270● if y_inner && x >= 1 && x < w - 1 {
  271. 271● continue
  272. 272 }
  273. 273● let up = @types.clamp_int(y - 1, 0, h - 1)
  274. 274● let down = @types.clamp_int(y + 1, 0, h - 1)
  275. 275● let left = @types.clamp_int(x - 1, 0, w - 1)
  276. 276● let right = @types.clamp_int(x + 1, 0, w - 1)
  277. 277 let ul = gray[up * w + left]
  278. 278 let ur = gray[up * w + right]
  279. 279 let ll = gray[down * w + left]
  280. 280 let lr = gray[down * w + right]
  281. 281 let l = gray[y * w + left]
  282. 282 let r = gray[y * w + right]
  283. 283 let u = gray[up * w + x]
  284. 284 let d = gray[down * w + x]
  285. 285 let gx = ur + r + lr - ul - l - ll
  286. 286 let gy = ll + d + lr - ul - u - ur
  287. 287● let mag = Float::from_int(gx * gx + gy * gy).sqrt()
  288. 288● out[y * w + x] = @types.clamp_byte(mag.to_int())
  289. 289 }
  290. 290 }
  291. 291 @types.Image::{
  292. 292 width: w,
  293. 293 height: h,
  294. 294 channels: 1,
  295. 295● data: Bytes::from_array(out),
  296. 296 }
  297. 297}