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