image.process\transform\perspective.mbt
93.3% · 56 / 60 points · 189 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 透视变换 perspective - 3
/// - 4
/// 图像处理算法模块 - 5
/// 图像处理算法模块 Image processing algorithm module - 6
// 透视变换(纯 MoonBit):get_perspective_transform + warp_perspective - 7
- 8
///| - 9
/// 3×3 透视变换矩阵 (m00, m01, m02, m10, m11, m12, m20, m21, m22) - 10
pub(all) struct PerspectiveMatrix { - 11
m00 : Float - 12
m01 : Float - 13
m02 : Float - 14
m10 : Float - 15
m11 : Float - 16
m12 : Float - 17
m20 : Float - 18
m21 : Float - 19
m22 : Float - 20
} - 21
- 22
///| - 23
/// 高斯消元法求解线性方程组 Ax=b,A 为 n×n 矩阵,b 为 n 维向量 - 24
fn solve_linear_system( - 25
a : Array[Array[Float]], - 26
b : Array[Float], - 27
n : Int32, - 28
) -> Array[Float]? { - 29●
let mat : Array[Array[Float]] = Array::make(n, []) - 30
for i in 0..<n { - 31●
mat[i] = Array::make(n + 1, 0.0F) - 32
for j in 0..<n { - 33●
mat[i][j] = a[i][j] - 34
} - 35
mat[i][n] = b[i] - 36
} - 37
// 前向消元 - 38
for col in 0..<n { - 39
// 选主元 - 40●
let mut pivot = col - 41
for row in (col + 1)..<n { - 42●
if mat[row][col].abs() > mat[pivot][col].abs() { - 43●
pivot = row - 44
} - 45
} - 46●
if mat[pivot][col].abs() < 0.0000000001F { - 47○
return None - 48
} - 49
if pivot != col { - 50●
let tmp = mat[pivot] - 51
mat[pivot] = mat[col] - 52
mat[col] = tmp - 53
} - 54
// 消元 - 55
for row in (col + 1)..<n { - 56●
let factor = mat[row][col] / mat[col][col] - 57
for j in col..<=n { - 58●
mat[row][j] -= factor * mat[col][j] - 59
} - 60
} - 61
} - 62
// 回代 - 63●
let x : Array[Float] = Array::make(n, 0.0F) - 64
for k in 0..<n { - 65●
let i = n - 1 - k - 66
let mut sum = mat[i][n] - 67
for j in (i + 1)..<n { - 68●
sum -= mat[i][j] * x[j] - 69
} - 70
x[i] = sum / mat[i][i] - 71
} - 72
Some(x) - 73
} - 74
- 75
///| - 76
/// 从 4 对源点→目标点计算 3×3 透视变换矩阵。 - 77
/// src 和 dst 均为 4 个 (Float, Float) 点。 - 78
pub fn get_perspective_transform( - 79
src : Array[(Float, Float)], - 80
dst : Array[(Float, Float)], - 81
) -> PerspectiveMatrix? { - 82●
if src.length() != 4 || dst.length() != 4 { - 83○
return None - 84
} - 85
// 构建 8×8 线性方程组 - 86●
let a : Array[Array[Float]] = Array::make(8, []) - 87●
let b : Array[Float] = Array::make(8, 0.0F) - 88
for i in 0..<4 { - 89●
let (sx, sy) = src[i] - 90
let (dx, dy) = dst[i] - 91
// 方程 1: m00*sx + m01*sy + m02 - m20*sx*dx - m21*sy*dx = dx - 92
a[i * 2] = [sx, sy, 1.0F, 0.0F, 0.0F, 0.0F, -sx * dx, -sy * dx] - 93
b[i * 2] = dx - 94
// 方程 2: m10*sx + m11*sy + m12 - m20*sx*dy - m21*sy*dy = dy - 95
a[i * 2 + 1] = [0.0F, 0.0F, 0.0F, sx, sy, 1.0F, -sx * dy, -sy * dy] - 96
b[i * 2 + 1] = dy - 97
} - 98●
let sol = solve_linear_system(a, b, 8) - 99●
match sol { - 100○
None => None - 101
Some(v) => - 102●
Some(PerspectiveMatrix::{ - 103
m00: v[0], - 104
m01: v[1], - 105
m02: v[2], - 106
m10: v[3], - 107
m11: v[4], - 108
m12: v[5], - 109
m20: v[6], - 110
m21: v[7], - 111
m22: 1.0F, - 112
}) - 113
} - 114
} - 115
- 116
///| - 117
/// 透视变换。使用 3×3 矩阵将图像变换到 new_w × new_h。 - 118
/// 采用 dst→src 映射 + 双线性插值。 - 119
pub fn warp_perspective( - 120
img : @types.Image, - 121
matrix : PerspectiveMatrix, - 122
new_w : Int32, - 123
new_h : Int32, - 124
) -> @types.Image { - 125●
let ch = img.channels - 126
let w = img.width - 127
let h = img.height - 128●
let data_int : Array[Int32] = Array::make(w * h * ch, 0) - 129
for i in 0..<(w * h * ch) { - 130●
data_int[i] = img.data[i].to_int() - 131
} - 132●
let out : Array[Byte] = Array::make(new_w * new_h * ch, b'\x00') - 133
for dy in 0..<new_h { - 134●
let fdx = 0.0F - 135●
let fdy = Float::from_int(dy) - 136
for dx in 0..<new_w { - 137●
let fdx2 = if dx == 0 { fdx } else { Float::from_int(dx) } - 138
// dst→src: 求逆映射。这里假设 matrix 是 src→dst,需要求逆。 - 139
// 简化:直接用 matrix 做 dst→src 映射(前向映射的逆) - 140
// src_x = (m00*dx + m01*dy + m02) / (m20*dx + m21*dy + m22) - 141
// src_y = (m10*dx + m11*dy + m12) / (m20*dx + m21*dy + m22) - 142
let denom = matrix.m20 * fdx2 + matrix.m21 * fdy + matrix.m22 - 143●
if denom.abs() < 0.0000000001F { - 144○
continue - 145
} - 146
let sx = (matrix.m00 * fdx2 + matrix.m01 * fdy + matrix.m02) / denom - 147
let sy = (matrix.m10 * fdx2 + matrix.m11 * fdy + matrix.m12) / denom - 148
for ci in 0..<ch { - 149●
let val = persp_sample_bilinear(data_int, w, h, ch, ci, sx, sy) - 150●
out[(dy * new_w + dx) * ch + ci] = val.to_byte() - 151
} - 152
} - 153
} - 154
@types.Image::{ - 155
width: new_w, - 156
height: new_h, - 157
channels: ch, - 158●
data: Bytes::from_array(out), - 159
} - 160
} - 161
- 162
///| - 163
fn persp_sample_bilinear( - 164
data : Array[Int32], - 165
w : Int32, - 166
h : Int32, - 167
ch : Int32, - 168
c : Int32, - 169
x : Float, - 170
y : Float, - 171
) -> Int32 { - 172●
let x0 = x.to_int() - 173●
let y0 = y.to_int() - 174
let w1 = w - 1 - 175
let h1 = h - 1 - 176●
let x0c = x0.clamp(min=0, max=w1) - 177●
let y0c = y0.clamp(min=0, max=h1) - 178●
let x1 = (x0 + 1).clamp(min=0, max=w1) - 179●
let y1 = (y0 + 1).clamp(min=0, max=h1) - 180●
let fx = x - Float::from_int(x0) - 181●
let fy = y - Float::from_int(y0) - 182
let p00 = data[(y0c * w + x0c) * ch + c] - 183
let p10 = data[(y0c * w + x1) * ch + c] - 184
let p01 = data[(y1 * w + x0c) * ch + c] - 185
let p11 = data[(y1 * w + x1) * ch + c] - 186●
let top = Float::from_int(p00) * (1.0F - fx) + Float::from_int(p10) * fx - 187●
let bot = Float::from_int(p01) * (1.0F - fx) + Float::from_int(p11) * fx - 188●
(top * (1.0F - fy) + bot * fy + 0.5F).to_int().clamp(min=0, max=255) - 189
}