image.process\transform\seam_carving.mbt

89.2% · 99 / 111 points · 344 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/// # Seam Carving 接缝裁剪 seam carving
  3. 3///
  4. 4/// 图像处理算法模块
  5. 5/// 图像处理算法模块 Image processing algorithm module
  6. 6// 接缝裁剪(Seam Carving)— 内容感知缩放
  7. 7// 纯 MoonBit 实现,基于 Avidan & Shamir 2007
  8. 8
  9. 9///|
  10. 10/// 计算图像能量图(Sobel 梯度幅值 |Gx| + |Gy|)
  11. 11pub fn compute_energy(img : @types.Image) -> Array[Double] {
  12. 12● let w = img.width
  13. 13 let h = img.height
  14. 14 let ch = img.channels
  15. 15● let energy = Array::make(w * h, 0.0)
  16. 16 for y in 0..<h {
  17. 17● for x in 0..<w {
  18. 18● let ym = if y == 0 { 0 } else { y - 1 }
  19. 19● let yp = if y == h - 1 { h - 1 } else { y + 1 }
  20. 20● let xm = if x == 0 { 0 } else { x - 1 }
  21. 21● let xp = if x == w - 1 { w - 1 } else { x + 1 }
  22. 22 let mut gx = 0.0
  23. 23 let mut gy = 0.0
  24. 24 for c in 0..<ch {
  25. 25● let left = img.data[(y * w + xm) * ch + c].to_int()
  26. 26● let right = img.data[(y * w + xp) * ch + c].to_int()
  27. 27● let top = img.data[(ym * w + x) * ch + c].to_int()
  28. 28● let bottom = img.data[(yp * w + x) * ch + c].to_int()
  29. 29● gx += (right - left).to_double()
  30. 30● gy += (bottom - top).to_double()
  31. 31 }
  32. 32◐ let abs_gx = if gx < 0.0 { -gx } else { gx }
  33. 33◐ let abs_gy = if gy < 0.0 { -gy } else { gy }
  34. 34 energy[y * w + x] = abs_gx + abs_gy
  35. 35 }
  36. 36 }
  37. 37 energy
  38. 38}
  39. 39
  40. 40///|
  41. 41/// 找垂直最小能量 seam(返回每行的列索引,长度为 h)
  42. 42pub fn find_vertical_seam(
  43. 43 energy : Array[Double],
  44. 44 w : Int32,
  45. 45 h : Int32,
  46. 46) -> Array[Int32] {
  47. 47● let m = Array::make(w * h, 0.0)
  48. 48 for j in 0..<w {
  49. 49● m[j] = energy[j]
  50. 50 }
  51. 51 for i in 1..<h {
  52. 52● for j in 0..<w {
  53. 53● let mut min_prev = m[(i - 1) * w + j]
  54. 54 if j > 0 {
  55. 55● let v = m[(i - 1) * w + j - 1]
  56. 56 if v < min_prev {
  57. 57● min_prev = v
  58. 58 }
  59. 59 }
  60. 60 if j < w - 1 {
  61. 61● let v = m[(i - 1) * w + j + 1]
  62. 62 if v < min_prev {
  63. 63● min_prev = v
  64. 64 }
  65. 65 }
  66. 66 m[i * w + j] = energy[i * w + j] + min_prev
  67. 67 }
  68. 68 }
  69. 69● let seam = Array::make(h, 0)
  70. 70 let mut min_val = m[(h - 1) * w]
  71. 71 let mut min_j = 0
  72. 72 for j in 1..<w {
  73. 73● let v = m[(h - 1) * w + j]
  74. 74 if v < min_val {
  75. 75● min_val = v
  76. 76 min_j = j
  77. 77 }
  78. 78 }
  79. 79 seam[h - 1] = min_j
  80. 80 for i in 1..<h {
  81. 81● let row = h - i
  82. 82 let prev_j = seam[row]
  83. 83 let mut best_j = prev_j
  84. 84 let mut best_val = m[(row - 1) * w + prev_j]
  85. 85 if prev_j > 0 {
  86. 86● let v = m[(row - 1) * w + prev_j - 1]
  87. 87 if v < best_val {
  88. 88● best_val = v
  89. 89 best_j = prev_j - 1
  90. 90 }
  91. 91 }
  92. 92 if prev_j < w - 1 {
  93. 93● let v = m[(row - 1) * w + prev_j + 1]
  94. 94 if v < best_val {
  95. 95● best_val = v
  96. 96 best_j = prev_j + 1
  97. 97 }
  98. 98 }
  99. 99 seam[row - 1] = best_j
  100. 100 }
  101. 101 seam
  102. 102}
  103. 103
  104. 104///|
  105. 105/// 移除垂直 seam,宽度减 1
  106. 106pub fn remove_vertical_seam(
  107. 107 img : @types.Image,
  108. 108 seam : Array[Int32],
  109. 109) -> @types.Image {
  110. 110● let w = img.width
  111. 111 let h = img.height
  112. 112 let ch = img.channels
  113. 113 let new_w = w - 1
  114. 114● let out : Array[Byte] = Array::make(new_w * h * ch, b'\x00')
  115. 115 for y in 0..<h {
  116. 116● let seam_x = seam[y]
  117. 117 let dst_row = y * new_w * ch
  118. 118 let src_row = y * w * ch
  119. 119 for x in 0..<seam_x {
  120. 120○ for c in 0..<ch {
  121. 121○ out[dst_row + x * ch + c] = img.data[src_row + x * ch + c]
  122. 122 }
  123. 123 }
  124. 124 for x in seam_x..<new_w {
  125. 125● for c in 0..<ch {
  126. 126● out[dst_row + x * ch + c] = img.data[src_row + (x + 1) * ch + c]
  127. 127 }
  128. 128 }
  129. 129 }
  130. 130 @types.Image::{
  131. 131 width: new_w,
  132. 132 height: h,
  133. 133 channels: ch,
  134. 134● data: Bytes::from_array(out),
  135. 135 }
  136. 136}
  137. 137
  138. 138///|
  139. 139/// 找水平最小能量 seam(返回每列的行索引,长度为 w)
  140. 140pub fn find_horizontal_seam(
  141. 141 energy : Array[Double],
  142. 142 w : Int32,
  143. 143 h : Int32,
  144. 144) -> Array[Int32] {
  145. 145● let m = Array::make(w * h, 0.0)
  146. 146 for i in 0..<h {
  147. 147● for j in 0..<w {
  148. 148● m[i * w + j] = energy[i * w + j]
  149. 149 }
  150. 150 }
  151. 151 for j in 1..<w {
  152. 152● for i in 0..<h {
  153. 153● let mut min_prev = m[i * w + j - 1]
  154. 154 if i > 0 {
  155. 155● let v = m[(i - 1) * w + j - 1]
  156. 156 if v < min_prev {
  157. 157● min_prev = v
  158. 158 }
  159. 159 }
  160. 160 if i < h - 1 {
  161. 161● let v = m[(i + 1) * w + j - 1]
  162. 162 if v < min_prev {
  163. 163● min_prev = v
  164. 164 }
  165. 165 }
  166. 166 m[i * w + j] = energy[i * w + j] + min_prev
  167. 167 }
  168. 168 }
  169. 169● let seam = Array::make(w, 0)
  170. 170 let mut min_val = m[w - 1]
  171. 171 let mut min_i = 0
  172. 172 for i in 1..<h {
  173. 173● let v = m[i * w + w - 1]
  174. 174 if v < min_val {
  175. 175● min_val = v
  176. 176 min_i = i
  177. 177 }
  178. 178 }
  179. 179 seam[w - 1] = min_i
  180. 180 for j in 1..<w {
  181. 181● let col = w - j
  182. 182 let prev_i = seam[col]
  183. 183 let mut best_i = prev_i
  184. 184 let mut best_val = m[prev_i * w + col - 1]
  185. 185 if prev_i > 0 {
  186. 186● let v = m[(prev_i - 1) * w + col - 1]
  187. 187 if v < best_val {
  188. 188○ best_val = v
  189. 189 best_i = prev_i - 1
  190. 190 }
  191. 191 }
  192. 192 if prev_i < h - 1 {
  193. 193● let v = m[(prev_i + 1) * w + col - 1]
  194. 194 if v < best_val {
  195. 195○ best_val = v
  196. 196 best_i = prev_i + 1
  197. 197 }
  198. 198 }
  199. 199 seam[col - 1] = best_i
  200. 200 }
  201. 201 seam
  202. 202}
  203. 203
  204. 204///|
  205. 205/// 移除水平 seam,高度减 1
  206. 206pub fn remove_horizontal_seam(
  207. 207 img : @types.Image,
  208. 208 seam : Array[Int32],
  209. 209) -> @types.Image {
  210. 210● let w = img.width
  211. 211 let h = img.height
  212. 212 let ch = img.channels
  213. 213 let new_h = h - 1
  214. 214● let out : Array[Byte] = Array::make(w * new_h * ch, b'\x00')
  215. 215 for x in 0..<w {
  216. 216● let seam_y = seam[x]
  217. 217 for y in 0..<seam_y {
  218. 218○ for c in 0..<ch {
  219. 219○ out[(y * w + x) * ch + c] = img.data[(y * w + x) * ch + c]
  220. 220 }
  221. 221 }
  222. 222 for y in seam_y..<new_h {
  223. 223● for c in 0..<ch {
  224. 224● out[(y * w + x) * ch + c] = img.data[((y + 1) * w + x) * ch + c]
  225. 225 }
  226. 226 }
  227. 227 }
  228. 228 @types.Image::{
  229. 229 width: w,
  230. 230 height: new_h,
  231. 231 channels: ch,
  232. 232● data: Bytes::from_array(out),
  233. 233 }
  234. 234}
  235. 235
  236. 236///|
  237. 237/// 插入垂直 seam,宽度加 1
  238. 238fn insert_vertical_seam(
  239. 239 img : @types.Image,
  240. 240 seam : Array[Int32],
  241. 241) -> @types.Image {
  242. 242● let w = img.width
  243. 243 let h = img.height
  244. 244 let ch = img.channels
  245. 245 let new_w = w + 1
  246. 246● let out : Array[Byte] = Array::make(new_w * h * ch, b'\x00')
  247. 247 for y in 0..<h {
  248. 248● let seam_x = seam[y]
  249. 249 let dst_row = y * new_w * ch
  250. 250 let src_row = y * w * ch
  251. 251 for x in 0..<seam_x {
  252. 252○ for c in 0..<ch {
  253. 253○ out[dst_row + x * ch + c] = img.data[src_row + x * ch + c]
  254. 254 }
  255. 255 }
  256. 256 for c in 0..<ch {
  257. 257● out[dst_row + seam_x * ch + c] = img.data[src_row + seam_x * ch + c]
  258. 258 }
  259. 259 for x in seam_x..<w {
  260. 260● for c in 0..<ch {
  261. 261● out[dst_row + (x + 1) * ch + c] = img.data[src_row + x * ch + c]
  262. 262 }
  263. 263 }
  264. 264 }
  265. 265 @types.Image::{
  266. 266 width: new_w,
  267. 267 height: h,
  268. 268 channels: ch,
  269. 269● data: Bytes::from_array(out),
  270. 270 }
  271. 271}
  272. 272
  273. 273///|
  274. 274/// 插入水平 seam,高度加 1
  275. 275fn insert_horizontal_seam(
  276. 276 img : @types.Image,
  277. 277 seam : Array[Int32],
  278. 278) -> @types.Image {
  279. 279● let w = img.width
  280. 280 let h = img.height
  281. 281 let ch = img.channels
  282. 282 let new_h = h + 1
  283. 283● let out : Array[Byte] = Array::make(w * new_h * ch, b'\x00')
  284. 284 for x in 0..<w {
  285. 285● let seam_y = seam[x]
  286. 286 for y in 0..<seam_y {
  287. 287○ for c in 0..<ch {
  288. 288○ out[(y * w + x) * ch + c] = img.data[(y * w + x) * ch + c]
  289. 289 }
  290. 290 }
  291. 291 for c in 0..<ch {
  292. 292● out[(seam_y * w + x) * ch + c] = img.data[(seam_y * w + x) * ch + c]
  293. 293 }
  294. 294 for y in seam_y..<h {
  295. 295● for c in 0..<ch {
  296. 296● out[((y + 1) * w + x) * ch + c] = img.data[(y * w + x) * ch + c]
  297. 297 }
  298. 298 }
  299. 299 }
  300. 300 @types.Image::{
  301. 301 width: w,
  302. 302 height: new_h,
  303. 303 channels: ch,
  304. 304● data: Bytes::from_array(out),
  305. 305 }
  306. 306}
  307. 307
  308. 308///|
  309. 309/// 内容感知缩放(Seam Carving)
  310. 310/// 支持缩小和放大,放大上限为原尺寸 2 倍
  311. 311pub fn seam_carve_resize(
  312. 312 img : @types.Image,
  313. 313 new_w : Int32,
  314. 314 new_h : Int32,
  315. 315) -> @types.Image raise @types.LoadError {
  316. 316● if new_w <= 0 || new_h <= 0 {
  317. 317● raise @types.LoadError::DecodeFailed("目标尺寸必须为正数")
  318. 318 }
  319. 319 if new_w > img.width * 2 || new_h > img.height * 2 {
  320. 320● raise @types.LoadError::DecodeFailed("放大不能超过原尺寸的 2 倍")
  321. 321 }
  322. 322 let mut result = img
  323. 323 while result.width > new_w {
  324. 324● let energy = compute_energy(result)
  325. 325● let seam = find_vertical_seam(energy, result.width, result.height)
  326. 326● result = remove_vertical_seam(result, seam)
  327. 327 }
  328. 328 while result.width < new_w {
  329. 329● let energy = compute_energy(result)
  330. 330● let seam = find_vertical_seam(energy, result.width, result.height)
  331. 331● result = insert_vertical_seam(result, seam)
  332. 332 }
  333. 333 while result.height > new_h {
  334. 334● let energy = compute_energy(result)
  335. 335● let seam = find_horizontal_seam(energy, result.width, result.height)
  336. 336● result = remove_horizontal_seam(result, seam)
  337. 337 }
  338. 338 while result.height < new_h {
  339. 339● let energy = compute_energy(result)
  340. 340● let seam = find_horizontal_seam(energy, result.width, result.height)
  341. 341● result = insert_horizontal_seam(result, seam)
  342. 342 }
  343. 343 result
  344. 344}