image.process\edge\edge_detect_16f.mbt

91.2% · 206 / 226 points · 627 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/// # 边缘检测 16-bit/float 变体 Edge detection 16-bit/float variants
  3. 3///
  4. 4/// 16位/浮点 Sobel 边缘检测 edge_detect_sobel_16 / edge_detect_sobel_f
  5. 5/// 16位/浮点 Sobel 边缘检测 16-bit/float Sobel edge detection
  6. 6// 边缘检测 16-bit/float 变体:edge_detect_sobel
  7. 7// 支持 Image16 / ImageF 两种类型
  8. 8
  9. 9///|
  10. 10fn read_u16_le_e(data : Bytes, offset : Int32) -> Int32 {
  11. 11● data[offset].to_int() + data[offset + 1].to_int() * 256
  12. 12}
  13. 13
  14. 14///|
  15. 15fn write_u16_le_e(buf : Array[Byte], offset : Int32, val : Int32) -> Unit {
  16. 16● buf[offset] = (val & 0xFF).to_byte()
  17. 17● buf[offset + 1] = ((val >> 8) & 0xFF).to_byte()
  18. 18}
  19. 19
  20. 20///|
  21. 21fn read_f32_le_e(data : Bytes, offset : Int32) -> Float {
  22. 22● let b0 = data[offset].to_int()
  23. 23● let b1 = data[offset + 1].to_int()
  24. 24● let b2 = data[offset + 2].to_int()
  25. 25● let b3 = data[offset + 3].to_int()
  26. 26 let bits = b0 | (b1 << 8) | (b2 << 16) | (b3 << 24)
  27. 27● Float::reinterpret_from_int(bits)
  28. 28}
  29. 29
  30. 30///|
  31. 31fn write_f32_le_e(buf : Array[Byte], offset : Int32, val : Float) -> Unit {
  32. 32● let bits = val.reinterpret_as_int()
  33. 33● buf[offset] = (bits & 0xFF).to_byte()
  34. 34● buf[offset + 1] = ((bits >> 8) & 0xFF).to_byte()
  35. 35● buf[offset + 2] = ((bits >> 16) & 0xFF).to_byte()
  36. 36● buf[offset + 3] = ((bits >> 24) & 0xFF).to_byte()
  37. 37}
  38. 38
  39. 39///|
  40. 40/// 16-bit Sobel 边缘检测。先转灰度,然后计算梯度幅值。
  41. 41/// 输出为单通道灰度图(channels=1)的 Image16。
  42. 42/// @see edge_detect_sobel 8-bit 版本,本函数为 Image16 变体
  43. 43pub fn edge_detect_sobel_16(
  44. 44 img : @types.Image16,
  45. 45) -> @types.Image16 raise @types.LoadError {
  46. 46 // 输入验证
  47. 47● if img.width <= 0 || img.height <= 0 {
  48. 48● raise @types.LoadError::DecodeFailed(
  49. 49 "edge_detect_sobel_16: 图像尺寸必须大于0",
  50. 50 )
  51. 51 }
  52. 52 if img.channels < 1 || img.channels > 4 {
  53. 53● raise @types.LoadError::DecodeFailed(
  54. 54 "edge_detect_sobel_16: 通道数必须在1-4之间",
  55. 55 )
  56. 56 }
  57. 57 let expected_size = img.width * img.height * img.channels * 2
  58. 58● if img.data.length() != expected_size {
  59. 59● raise @types.LoadError::DecodeFailed(
  60. 60 "edge_detect_sobel_16: 数据大小不匹配,期望 " +
  61. 61● expected_size.to_string() +
  62. 62 ",实际 " +
  63. 63● img.data.length().to_string(),
  64. 64 )
  65. 65 }
  66. 66
  67. 67 let w = img.width
  68. 68 let h = img.height
  69. 69 let ch = img.channels
  70. 70 let n = w * h
  71. 71 let bpp = ch * 2
  72. 72 // 先转灰度
  73. 73● let gray : Array[Int32] = Array::make(n, 0)
  74. 74 for i in 0..<n {
  75. 75● if ch == 1 {
  76. 76● gray[i] = read_u16_le_e(img.data, i * bpp)
  77. 77 } else {
  78. 78● let r = read_u16_le_e(img.data, i * bpp)
  79. 79● let g = read_u16_le_e(img.data, i * bpp + 2)
  80. 80● let b = read_u16_le_e(img.data, i * bpp + 4)
  81. 81 gray[i] = (r * 299 + g * 587 + b * 114) / 1000
  82. 82 }
  83. 83 }
  84. 84 // Sobel 卷积
  85. 85● let out : Array[Byte] = Array::make(n * 2, b'\x00')
  86. 86 // 内部区域
  87. 87 for y in 1..<(h - 1) {
  88. 88● for x in 1..<(w - 1) {
  89. 89● let idx = y * w + x
  90. 90 let idx_l = idx - 1
  91. 91 let idx_r = idx + 1
  92. 92 let idx_t = idx - w
  93. 93 let idx_b = idx + w
  94. 94 let gx = -gray[idx_t - 1] +
  95. 95 gray[idx_t + 1] -
  96. 96 2 * gray[idx_l] +
  97. 97 2 * gray[idx_r] -
  98. 98 gray[idx_b - 1] +
  99. 99 gray[idx_b + 1]
  100. 100 let gy = -gray[idx_t - 1] -
  101. 101 2 * gray[idx_t] -
  102. 102 gray[idx_t + 1] +
  103. 103 gray[idx_b - 1] +
  104. 104 2 * gray[idx_b] +
  105. 105 gray[idx_b + 1]
  106. 106● let mag = Float::from_int(gx * gx + gy * gy).sqrt()
  107. 107● let v = (mag + 0.5F).to_int().clamp(min=0, max=65535)
  108. 108● write_u16_le_e(out, idx * 2, v)
  109. 109 }
  110. 110 }
  111. 111 // 边界区域
  112. 112 for y in 0..<h {
  113. 113● for x in 0..<w {
  114. 114● if x >= 1 && x < w - 1 && y >= 1 && y < h - 1 {
  115. 115● continue
  116. 116 }
  117. 117● let x_l = @types.clamp_int(x - 1, 0, w - 1)
  118. 118● let x_r = @types.clamp_int(x + 1, 0, w - 1)
  119. 119● let y_t = @types.clamp_int(y - 1, 0, h - 1)
  120. 120● let y_b = @types.clamp_int(y + 1, 0, h - 1)
  121. 121 let gx = -gray[y_t * w + x_l] +
  122. 122 gray[y_t * w + x_r] -
  123. 123 2 * gray[y * w + x_l] +
  124. 124 2 * gray[y * w + x_r] -
  125. 125 gray[y_b * w + x_l] +
  126. 126 gray[y_b * w + x_r]
  127. 127 let gy = -gray[y_t * w + x_l] -
  128. 128 2 * gray[y_t * w + x] -
  129. 129 gray[y_t * w + x_r] +
  130. 130 gray[y_b * w + x_l] +
  131. 131 2 * gray[y_b * w + x] +
  132. 132 gray[y_b * w + x_r]
  133. 133● let mag = Float::from_int(gx * gx + gy * gy).sqrt()
  134. 134● let v = (mag + 0.5F).to_int().clamp(min=0, max=65535)
  135. 135● write_u16_le_e(out, (y * w + x) * 2, v)
  136. 136 }
  137. 137 }
  138. 138 @types.Image16::{
  139. 139 width: w,
  140. 140 height: h,
  141. 141 channels: 1,
  142. 142● data: Bytes::from_array(out),
  143. 143 }
  144. 144}
  145. 145
  146. 146///|
  147. 147/// float Sobel 边缘检测。先转灰度,然后计算梯度幅值。
  148. 148/// 输出为单通道灰度图(channels=1)的 ImageF。
  149. 149/// @see edge_detect_sobel 8-bit 版本,本函数为 ImageF 变体
  150. 150pub fn edge_detect_sobel_f(
  151. 151 img : @types.ImageF,
  152. 152) -> @types.ImageF raise @types.LoadError {
  153. 153 // 输入验证
  154. 154● if img.width <= 0 || img.height <= 0 {
  155. 155● raise @types.LoadError::DecodeFailed(
  156. 156 "edge_detect_sobel_f: 图像尺寸必须大于0",
  157. 157 )
  158. 158 }
  159. 159 if img.channels < 1 || img.channels > 4 {
  160. 160● raise @types.LoadError::DecodeFailed(
  161. 161 "edge_detect_sobel_f: 通道数必须在1-4之间",
  162. 162 )
  163. 163 }
  164. 164 let expected_size = img.width * img.height * img.channels * 4
  165. 165● if img.data.length() != expected_size {
  166. 166● raise @types.LoadError::DecodeFailed(
  167. 167 "edge_detect_sobel_f: 数据大小不匹配,期望 " +
  168. 168● expected_size.to_string() +
  169. 169 ",实际 " +
  170. 170● img.data.length().to_string(),
  171. 171 )
  172. 172 }
  173. 173
  174. 174 let w = img.width
  175. 175 let h = img.height
  176. 176 let ch = img.channels
  177. 177 let n = w * h
  178. 178 let bpp = ch * 4
  179. 179 // 先转灰度
  180. 180● let gray : Array[Float] = Array::make(n, 0.0F)
  181. 181 for i in 0..<n {
  182. 182● if ch == 1 {
  183. 183● gray[i] = read_f32_le_e(img.data, i * bpp)
  184. 184 } else {
  185. 185● let r = read_f32_le_e(img.data, i * bpp)
  186. 186● let g = read_f32_le_e(img.data, i * bpp + 4)
  187. 187● let b = read_f32_le_e(img.data, i * bpp + 8)
  188. 188 gray[i] = r * 0.299F + g * 0.587F + b * 0.114F
  189. 189 }
  190. 190 }
  191. 191 // Sobel 卷积
  192. 192● let out : Array[Byte] = Array::make(n * 4, b'\x00')
  193. 193 // 内部区域
  194. 194 for y in 1..<(h - 1) {
  195. 195● for x in 1..<(w - 1) {
  196. 196● let idx = y * w + x
  197. 197 let idx_l = idx - 1
  198. 198 let idx_r = idx + 1
  199. 199 let idx_t = idx - w
  200. 200 let idx_b = idx + w
  201. 201 let gx = -gray[idx_t - 1] +
  202. 202 gray[idx_t + 1] -
  203. 203 2.0F * gray[idx_l] +
  204. 204 2.0F * gray[idx_r] -
  205. 205 gray[idx_b - 1] +
  206. 206 gray[idx_b + 1]
  207. 207 let gy = -gray[idx_t - 1] -
  208. 208 2.0F * gray[idx_t] -
  209. 209 gray[idx_t + 1] +
  210. 210 gray[idx_b - 1] +
  211. 211 2.0F * gray[idx_b] +
  212. 212 gray[idx_b + 1]
  213. 213● let mag = (gx * gx + gy * gy).sqrt()
  214. 214● write_f32_le_e(out, idx * 4, mag)
  215. 215 }
  216. 216 }
  217. 217 // 边界区域
  218. 218 for y in 0..<h {
  219. 219● for x in 0..<w {
  220. 220● if x >= 1 && x < w - 1 && y >= 1 && y < h - 1 {
  221. 221● continue
  222. 222 }
  223. 223● let x_l = @types.clamp_int(x - 1, 0, w - 1)
  224. 224● let x_r = @types.clamp_int(x + 1, 0, w - 1)
  225. 225● let y_t = @types.clamp_int(y - 1, 0, h - 1)
  226. 226● let y_b = @types.clamp_int(y + 1, 0, h - 1)
  227. 227 let gx = -gray[y_t * w + x_l] +
  228. 228 gray[y_t * w + x_r] -
  229. 229 2.0F * gray[y * w + x_l] +
  230. 230 2.0F * gray[y * w + x_r] -
  231. 231 gray[y_b * w + x_l] +
  232. 232 gray[y_b * w + x_r]
  233. 233 let gy = -gray[y_t * w + x_l] -
  234. 234 2.0F * gray[y_t * w + x] -
  235. 235 gray[y_t * w + x_r] +
  236. 236 gray[y_b * w + x_l] +
  237. 237 2.0F * gray[y_b * w + x] +
  238. 238 gray[y_b * w + x_r]
  239. 239● let mag = (gx * gx + gy * gy).sqrt()
  240. 240● write_f32_le_e(out, (y * w + x) * 4, mag)
  241. 241 }
  242. 242 }
  243. 243 @types.ImageF::{
  244. 244 width: w,
  245. 245 height: h,
  246. 246 channels: 1,
  247. 247● data: Bytes::from_array(out),
  248. 248 }
  249. 249}
  250. 250
  251. 251///|
  252. 252/// 16-bit Laplacian 边缘检测。使用 3x3 Laplacian 核:
  253. 253/// 拉普拉斯核 [0, 1, 0; 1, -4, 1; 0, 1, 0]
  254. 254/// 输出单通道灰度图(channels=1)的 Image16,值为梯度绝对值。
  255. 255/// @see edge_detect_laplacian 8-bit 版本,本函数为 Image16 变体
  256. 256pub fn edge_detect_laplacian_16(
  257. 257 img : @types.Image16,
  258. 258) -> @types.Image16 raise @types.LoadError {
  259. 259 // 输入验证
  260. 260● if img.width <= 0 || img.height <= 0 {
  261. 261● raise @types.LoadError::DecodeFailed(
  262. 262 "edge_detect_laplacian_16: 图像尺寸必须大于0",
  263. 263 )
  264. 264 }
  265. 265 if img.channels < 1 || img.channels > 4 {
  266. 266● raise @types.LoadError::DecodeFailed(
  267. 267 "edge_detect_laplacian_16: 通道数必须在1-4之间",
  268. 268 )
  269. 269 }
  270. 270 let expected_size = img.width * img.height * img.channels * 2
  271. 271● if img.data.length() != expected_size {
  272. 272○ raise @types.LoadError::DecodeFailed(
  273. 273 "edge_detect_laplacian_16: 数据大小不匹配,期望 " +
  274. 274○ expected_size.to_string() +
  275. 275 ",实际 " +
  276. 276○ img.data.length().to_string(),
  277. 277 )
  278. 278 }
  279. 279
  280. 280 let w = img.width
  281. 281 let h = img.height
  282. 282 let ch = img.channels
  283. 283 let n = w * h
  284. 284 let bpp = ch * 2
  285. 285 // 先转灰度
  286. 286● let gray : Array[Int32] = Array::make(n, 0)
  287. 287 for i in 0..<n {
  288. 288● if ch == 1 {
  289. 289○ gray[i] = read_u16_le_e(img.data, i * bpp)
  290. 290 } else {
  291. 291● let r = read_u16_le_e(img.data, i * bpp)
  292. 292● let g = read_u16_le_e(img.data, i * bpp + 2)
  293. 293● let b = read_u16_le_e(img.data, i * bpp + 4)
  294. 294 gray[i] = (r * 77 + g * 150 + b * 29) / 256
  295. 295 }
  296. 296 }
  297. 297● let out : Array[Byte] = Array::make(n * 2, b'\x00')
  298. 298 // 内部区域
  299. 299 for y in 1..<(h - 1) {
  300. 300● for x in 1..<(w - 1) {
  301. 301● let idx = y * w + x
  302. 302 let center = gray[idx]
  303. 303 let u = gray[idx - w]
  304. 304 let d = gray[idx + w]
  305. 305 let l = gray[idx - 1]
  306. 306 let r = gray[idx + 1]
  307. 307 let lap = u + d + l + r - 4 * center
  308. 308● let val = lap.abs()
  309. 309● let clamped = if val > 65535 { 65535 } else { val }
  310. 310● write_u16_le_e(out, idx * 2, clamped)
  311. 311 }
  312. 312 }
  313. 313 // 边界区域
  314. 314 for y in 0..<h {
  315. 315● for x in 0..<w {
  316. 316● if x >= 1 && x < w - 1 && y >= 1 && y < h - 1 {
  317. 317● continue
  318. 318 }
  319. 319● let cx = @types.clamp_int(x, 0, w - 1)
  320. 320● let cy = @types.clamp_int(y, 0, h - 1)
  321. 321● let up = @types.clamp_int(y - 1, 0, h - 1)
  322. 322● let down = @types.clamp_int(y + 1, 0, h - 1)
  323. 323● let left = @types.clamp_int(x - 1, 0, w - 1)
  324. 324● let right = @types.clamp_int(x + 1, 0, w - 1)
  325. 325 let center = gray[cy * w + cx]
  326. 326 let u = gray[up * w + cx]
  327. 327 let d = gray[down * w + cx]
  328. 328 let l = gray[cy * w + left]
  329. 329 let r = gray[cy * w + right]
  330. 330 let lap = u + d + l + r - 4 * center
  331. 331● let val = lap.abs()
  332. 332● let clamped = if val > 65535 { 65535 } else { val }
  333. 333● write_u16_le_e(out, (y * w + x) * 2, clamped)
  334. 334 }
  335. 335 }
  336. 336 @types.Image16::{
  337. 337 width: w,
  338. 338 height: h,
  339. 339 channels: 1,
  340. 340● data: Bytes::from_array(out),
  341. 341 }
  342. 342}
  343. 343
  344. 344///|
  345. 345/// float Laplacian 边缘检测。使用 3x3 Laplacian 核:
  346. 346/// 拉普拉斯核 [0, 1, 0; 1, -4, 1; 0, 1, 0]
  347. 347/// 输出单通道灰度图(channels=1)的 ImageF,值为梯度绝对值。
  348. 348/// @see edge_detect_laplacian 8-bit 版本,本函数为 ImageF 变体
  349. 349pub fn edge_detect_laplacian_f(
  350. 350 img : @types.ImageF,
  351. 351) -> @types.ImageF raise @types.LoadError {
  352. 352 // 输入验证
  353. 353● if img.width <= 0 || img.height <= 0 {
  354. 354● raise @types.LoadError::DecodeFailed(
  355. 355 "edge_detect_laplacian_f: 图像尺寸必须大于0",
  356. 356 )
  357. 357 }
  358. 358 if img.channels < 1 || img.channels > 4 {
  359. 359● raise @types.LoadError::DecodeFailed(
  360. 360 "edge_detect_laplacian_f: 通道数必须在1-4之间",
  361. 361 )
  362. 362 }
  363. 363 let expected_size = img.width * img.height * img.channels * 4
  364. 364● if img.data.length() != expected_size {
  365. 365○ raise @types.LoadError::DecodeFailed(
  366. 366 "edge_detect_laplacian_f: 数据大小不匹配,期望 " +
  367. 367○ expected_size.to_string() +
  368. 368 ",实际 " +
  369. 369○ img.data.length().to_string(),
  370. 370 )
  371. 371 }
  372. 372
  373. 373 let w = img.width
  374. 374 let h = img.height
  375. 375 let ch = img.channels
  376. 376 let n = w * h
  377. 377 let bpp = ch * 4
  378. 378 // 先转灰度
  379. 379● let gray : Array[Float] = Array::make(n, 0.0F)
  380. 380 for i in 0..<n {
  381. 381● if ch == 1 {
  382. 382○ gray[i] = read_f32_le_e(img.data, i * bpp)
  383. 383 } else {
  384. 384● let r = read_f32_le_e(img.data, i * bpp)
  385. 385● let g = read_f32_le_e(img.data, i * bpp + 4)
  386. 386● let b = read_f32_le_e(img.data, i * bpp + 8)
  387. 387 gray[i] = r * 0.299F + g * 0.587F + b * 0.114F
  388. 388 }
  389. 389 }
  390. 390● let out : Array[Byte] = Array::make(n * 4, b'\x00')
  391. 391 // 内部区域
  392. 392 for y in 1..<(h - 1) {
  393. 393● for x in 1..<(w - 1) {
  394. 394● let idx = y * w + x
  395. 395 let center = gray[idx]
  396. 396 let u = gray[idx - w]
  397. 397 let d = gray[idx + w]
  398. 398 let l = gray[idx - 1]
  399. 399 let r = gray[idx + 1]
  400. 400 let lap = u + d + l + r - 4.0F * center
  401. 401● write_f32_le_e(out, idx * 4, lap.abs())
  402. 402 }
  403. 403 }
  404. 404 // 边界区域
  405. 405 for y in 0..<h {
  406. 406● for x in 0..<w {
  407. 407● if x >= 1 && x < w - 1 && y >= 1 && y < h - 1 {
  408. 408● continue
  409. 409 }
  410. 410● let cx = @types.clamp_int(x, 0, w - 1)
  411. 411● let cy = @types.clamp_int(y, 0, h - 1)
  412. 412● let up = @types.clamp_int(y - 1, 0, h - 1)
  413. 413● let down = @types.clamp_int(y + 1, 0, h - 1)
  414. 414● let left = @types.clamp_int(x - 1, 0, w - 1)
  415. 415● let right = @types.clamp_int(x + 1, 0, w - 1)
  416. 416 let center = gray[cy * w + cx]
  417. 417 let u = gray[up * w + cx]
  418. 418 let d = gray[down * w + cx]
  419. 419 let l = gray[cy * w + left]
  420. 420 let r = gray[cy * w + right]
  421. 421 let lap = u + d + l + r - 4.0F * center
  422. 422● write_f32_le_e(out, (y * w + x) * 4, lap.abs())
  423. 423 }
  424. 424 }
  425. 425 @types.ImageF::{
  426. 426 width: w,
  427. 427 height: h,
  428. 428 channels: 1,
  429. 429● data: Bytes::from_array(out),
  430. 430 }
  431. 431}
  432. 432
  433. 433///|
  434. 434/// 16-bit Prewitt 边缘检测。使用两个 3x3 Prewitt 核计算梯度幅值。
  435. 435/// Prewitt 水平梯度 Gx = [-1, 0, 1; -1, 0, 1; -1, 0, 1]
  436. 436/// Prewitt 垂直梯度 Gy = [-1, -1, -1; 0, 0, 0; 1, 1, 1]
  437. 437/// 输出单通道灰度图(channels=1)的 Image16。
  438. 438/// @see edge_detect_prewitt 8-bit 版本,本函数为 Image16 变体
  439. 439pub fn edge_detect_prewitt_16(
  440. 440 img : @types.Image16,
  441. 441) -> @types.Image16 raise @types.LoadError {
  442. 442 // 输入验证
  443. 443● if img.width <= 0 || img.height <= 0 {
  444. 444● raise @types.LoadError::DecodeFailed(
  445. 445 "edge_detect_prewitt_16: 图像尺寸必须大于0",
  446. 446 )
  447. 447 }
  448. 448 if img.channels < 1 || img.channels > 4 {
  449. 449○ raise @types.LoadError::DecodeFailed(
  450. 450 "edge_detect_prewitt_16: 通道数必须在1-4之间",
  451. 451 )
  452. 452 }
  453. 453 let expected_size = img.width * img.height * img.channels * 2
  454. 454● if img.data.length() != expected_size {
  455. 455● raise @types.LoadError::DecodeFailed(
  456. 456 "edge_detect_prewitt_16: 数据大小不匹配,期望 " +
  457. 457● expected_size.to_string() +
  458. 458 ",实际 " +
  459. 459● img.data.length().to_string(),
  460. 460 )
  461. 461 }
  462. 462
  463. 463 let w = img.width
  464. 464 let h = img.height
  465. 465 let ch = img.channels
  466. 466 let n = w * h
  467. 467 let bpp = ch * 2
  468. 468 // 先转灰度
  469. 469● let gray : Array[Int32] = Array::make(n, 0)
  470. 470 for i in 0..<n {
  471. 471● if ch == 1 {
  472. 472○ gray[i] = read_u16_le_e(img.data, i * bpp)
  473. 473 } else {
  474. 474● let r = read_u16_le_e(img.data, i * bpp)
  475. 475● let g = read_u16_le_e(img.data, i * bpp + 2)
  476. 476● let b = read_u16_le_e(img.data, i * bpp + 4)
  477. 477 gray[i] = (r * 77 + g * 150 + b * 29) / 256
  478. 478 }
  479. 479 }
  480. 480● let out : Array[Byte] = Array::make(n * 2, b'\x00')
  481. 481 // 内部区域
  482. 482 for y in 1..<(h - 1) {
  483. 483● for x in 1..<(w - 1) {
  484. 484● let idx = y * w + x
  485. 485 let ul = gray[idx - w - 1]
  486. 486 let ur = gray[idx - w + 1]
  487. 487 let ll = gray[idx + w - 1]
  488. 488 let lr = gray[idx + w + 1]
  489. 489 let l = gray[idx - 1]
  490. 490 let r = gray[idx + 1]
  491. 491 let u = gray[idx - w]
  492. 492 let d = gray[idx + w]
  493. 493 let gx = ur + r + lr - ul - l - ll
  494. 494 let gy = ll + d + lr - ul - u - ur
  495. 495● let mag = Float::from_int(gx * gx + gy * gy).sqrt().to_int()
  496. 496◐ let clamped = if mag > 65535 { 65535 } else { mag }
  497. 497● write_u16_le_e(out, idx * 2, clamped)
  498. 498 }
  499. 499 }
  500. 500 // 边界区域
  501. 501 for y in 0..<h {
  502. 502● for x in 0..<w {
  503. 503● if x >= 1 && x < w - 1 && y >= 1 && y < h - 1 {
  504. 504● continue
  505. 505 }
  506. 506● let up = @types.clamp_int(y - 1, 0, h - 1)
  507. 507● let down = @types.clamp_int(y + 1, 0, h - 1)
  508. 508● let left = @types.clamp_int(x - 1, 0, w - 1)
  509. 509● let right = @types.clamp_int(x + 1, 0, w - 1)
  510. 510 let ul = gray[up * w + left]
  511. 511 let ur = gray[up * w + right]
  512. 512 let ll = gray[down * w + left]
  513. 513 let lr = gray[down * w + right]
  514. 514 let l = gray[y * w + left]
  515. 515 let r = gray[y * w + right]
  516. 516 let u = gray[up * w + x]
  517. 517 let d = gray[down * w + x]
  518. 518 let gx = ur + r + lr - ul - l - ll
  519. 519 let gy = ll + d + lr - ul - u - ur
  520. 520● let mag = Float::from_int(gx * gx + gy * gy).sqrt().to_int()
  521. 521◐ let clamped = if mag > 65535 { 65535 } else { mag }
  522. 522● write_u16_le_e(out, (y * w + x) * 2, clamped)
  523. 523 }
  524. 524 }
  525. 525 @types.Image16::{
  526. 526 width: w,
  527. 527 height: h,
  528. 528 channels: 1,
  529. 529● data: Bytes::from_array(out),
  530. 530 }
  531. 531}
  532. 532
  533. 533///|
  534. 534/// float Prewitt 边缘检测。使用两个 3x3 Prewitt 核计算梯度幅值。
  535. 535/// 输出单通道灰度图(channels=1)的 ImageF。
  536. 536/// @see edge_detect_prewitt 8-bit 版本,本函数为 ImageF 变体
  537. 537pub fn edge_detect_prewitt_f(
  538. 538 img : @types.ImageF,
  539. 539) -> @types.ImageF raise @types.LoadError {
  540. 540 // 输入验证
  541. 541● if img.width <= 0 || img.height <= 0 {
  542. 542● raise @types.LoadError::DecodeFailed(
  543. 543 "edge_detect_prewitt_f: 图像尺寸必须大于0",
  544. 544 )
  545. 545 }
  546. 546 if img.channels < 1 || img.channels > 4 {
  547. 547○ raise @types.LoadError::DecodeFailed(
  548. 548 "edge_detect_prewitt_f: 通道数必须在1-4之间",
  549. 549 )
  550. 550 }
  551. 551 let expected_size = img.width * img.height * img.channels * 4
  552. 552● if img.data.length() != expected_size {
  553. 553● raise @types.LoadError::DecodeFailed(
  554. 554 "edge_detect_prewitt_f: 数据大小不匹配,期望 " +
  555. 555● expected_size.to_string() +
  556. 556 ",实际 " +
  557. 557● img.data.length().to_string(),
  558. 558 )
  559. 559 }
  560. 560
  561. 561 let w = img.width
  562. 562 let h = img.height
  563. 563 let ch = img.channels
  564. 564 let n = w * h
  565. 565 let bpp = ch * 4
  566. 566 // 先转灰度
  567. 567● let gray : Array[Float] = Array::make(n, 0.0F)
  568. 568 for i in 0..<n {
  569. 569● if ch == 1 {
  570. 570○ gray[i] = read_f32_le_e(img.data, i * bpp)
  571. 571 } else {
  572. 572● let r = read_f32_le_e(img.data, i * bpp)
  573. 573● let g = read_f32_le_e(img.data, i * bpp + 4)
  574. 574● let b = read_f32_le_e(img.data, i * bpp + 8)
  575. 575 gray[i] = r * 0.299F + g * 0.587F + b * 0.114F
  576. 576 }
  577. 577 }
  578. 578● let out : Array[Byte] = Array::make(n * 4, b'\x00')
  579. 579 // 内部区域
  580. 580 for y in 1..<(h - 1) {
  581. 581● for x in 1..<(w - 1) {
  582. 582● let idx = y * w + x
  583. 583 let ul = gray[idx - w - 1]
  584. 584 let ur = gray[idx - w + 1]
  585. 585 let ll = gray[idx + w - 1]
  586. 586 let lr = gray[idx + w + 1]
  587. 587 let l = gray[idx - 1]
  588. 588 let r = gray[idx + 1]
  589. 589 let u = gray[idx - w]
  590. 590 let d = gray[idx + w]
  591. 591 let gx = ur + r + lr - ul - l - ll
  592. 592 let gy = ll + d + lr - ul - u - ur
  593. 593● let mag = (gx * gx + gy * gy).sqrt()
  594. 594● write_f32_le_e(out, idx * 4, mag)
  595. 595 }
  596. 596 }
  597. 597 // 边界区域
  598. 598 for y in 0..<h {
  599. 599● for x in 0..<w {
  600. 600● if x >= 1 && x < w - 1 && y >= 1 && y < h - 1 {
  601. 601● continue
  602. 602 }
  603. 603● let up = @types.clamp_int(y - 1, 0, h - 1)
  604. 604● let down = @types.clamp_int(y + 1, 0, h - 1)
  605. 605● let left = @types.clamp_int(x - 1, 0, w - 1)
  606. 606● let right = @types.clamp_int(x + 1, 0, w - 1)
  607. 607 let ul = gray[up * w + left]
  608. 608 let ur = gray[up * w + right]
  609. 609 let ll = gray[down * w + left]
  610. 610 let lr = gray[down * w + right]
  611. 611 let l = gray[y * w + left]
  612. 612 let r = gray[y * w + right]
  613. 613 let u = gray[up * w + x]
  614. 614 let d = gray[down * w + x]
  615. 615 let gx = ur + r + lr - ul - l - ll
  616. 616 let gy = ll + d + lr - ul - u - ur
  617. 617● let mag = (gx * gx + gy * gy).sqrt()
  618. 618● write_f32_le_e(out, (y * w + x) * 4, mag)
  619. 619 }
  620. 620 }
  621. 621 @types.ImageF::{
  622. 622 width: w,
  623. 623 height: h,
  624. 624 channels: 1,
  625. 625● data: Bytes::from_array(out),
  626. 626 }
  627. 627}