image.vision\fieldimg\tensor_field.mbt
62.1% · 18 / 29 points · 195 linesBackground color reports the state of each line. Underlined characters mark exact instrumented byte offsets; hover them to inspect the visit count.
- 1
///| - 2
/// # 张量场 fieldimg/tensor_field - 3
/// - 4
/// 图像处理库子模块 - 5
/// 图像处理库子模块 Image processing library submodule - 6
// 张量场:2D 张量场,每个点 (x, y) 映射到 rows×cols 矩阵。 - 7
// Tensor field: 2D tensor field, each point (x, y) maps to a rows×cols matrix. - 8
// 典型应用:结构张量(2×2 对称)用于角点检测,梯度张量用于边缘分析。 - 9
- 10
///| - 11
/// 张量场:2D 张量场,data 为行主序,长度 = width * height * rows * cols。 - 12
/// 张量场:2D Tensor field: 2D - 13
#derive(Eq, @debug.Debug) - 14
pub(all) struct TensorField { - 15
width : Int32 - 16
height : Int32 - 17
rows : Int32 - 18
cols : Int32 - 19
data : Array[Float] - 20
} - 21
- 22
///| - 23
pub extend TensorField with @debug.Debug::{to_repr} - 24
- 25
///| - 26
pub extend TensorField with Eq::{not_equal, equal} - 27
- 28
///| - 29
/// 创建张量场,数据长度必须等于 width * height * rows * cols。 - 30
/// 创建张量场;数据长度须等于 width * height * rows * cols Create a tensor field; data length must equal width * height * rows * cols. - 31
pub fn TensorField::make( - 32
width : Int32, - 33
height : Int32, - 34
rows : Int32, - 35
cols : Int32, - 36
data : Array[Float], - 37
) -> TensorField raise @types.LoadError { - 38●
if width <= 0 || height <= 0 || rows <= 0 || cols <= 0 { - 39○
raise @types.LoadError::DecodeFailed( - 40
"TensorField::make: width, height, rows, cols 必须大于0", - 41
) - 42
} - 43●
if data.length() != width * height * rows * cols { - 44●
raise @types.LoadError::DecodeFailed( - 45
"TensorField::make: 数据长度必须等于 width * height * rows * cols", - 46
) - 47
} - 48
{ width, height, rows, cols, data, } - 49
} - 50
- 51
///| - 52
/// 创建全零张量场。 - 53
/// 创建全零张量场 Create an all-zero tensor field. - 54
pub fn TensorField::zeros( - 55
width : Int32, - 56
height : Int32, - 57
rows : Int32, - 58
cols : Int32, - 59
) -> TensorField raise @types.LoadError { - 60●
if width <= 0 || height <= 0 || rows <= 0 || cols <= 0 { - 61○
raise @types.LoadError::DecodeFailed( - 62
"TensorField::zeros: width, height, rows, cols 必须大于0", - 63
) - 64
} - 65
{ - 66
width, - 67
height, - 68
rows, - 69
cols, - 70●
data: Array::make(width * height * rows * cols, 0.0F), - 71
} - 72
} - 73
- 74
///| - 75
/// 求值单元素:获取点 (x, y) 处矩阵第 (r, c) 元素,边界外返回 0。 - 76
/// 求值元素 (r, c) 于 (x, y);越界返回 0 Evaluate element (r, c) at (x, y); returns 0 for out-of-bounds. - 77
pub fn TensorField::evaluate( - 78
self : TensorField, - 79
x : Int32, - 80
y : Int32, - 81
r : Int32, - 82
c : Int32, - 83
) -> Float { - 84●
if x < 0 || - 85
x >= self.width || - 86
y < 0 || - 87
y >= self.height || - 88
r < 0 || - 89
r >= self.rows || - 90
c < 0 || - 91
c >= self.cols { - 92○
return 0.0F - 93
} - 94
self.data[((y * self.width + x) * self.rows + r) * self.cols + c] - 95
} - 96
- 97
///| - 98
/// 求值单元素(边界钳制)。 - 99
/// 钳位边界求值元素 Evaluate element with clamped boundary. - 100
pub fn TensorField::evaluate_clamped( - 101
self : TensorField, - 102
x : Int32, - 103
y : Int32, - 104
r : Int32, - 105
c : Int32, - 106
) -> Float { - 107○
let cx = x.clamp(min=0, max=self.width - 1) - 108○
let cy = y.clamp(min=0, max=self.height - 1) - 109○
let cr = r.clamp(min=0, max=self.rows - 1) - 110○
let cc = c.clamp(min=0, max=self.cols - 1) - 111
self.data[((cy * self.width + cx) * self.rows + cr) * self.cols + cc] - 112
} - 113
- 114
///| - 115
/// 设值单元素:设置点 (x, y) 处矩阵第 (r, c) 元素,越界则忽略。 - 116
/// 设置元素 (r, c) 于 (x, y);越界忽略 Set element (r, c) at (x, y); out-of-bounds is ignored. - 117
pub fn TensorField::set( - 118
self : TensorField, - 119
x : Int32, - 120
y : Int32, - 121
r : Int32, - 122
c : Int32, - 123
value : Float, - 124
) -> Unit { - 125●
if x < 0 || - 126
x >= self.width || - 127
y < 0 || - 128
y >= self.height || - 129
r < 0 || - 130
r >= self.rows || - 131
c < 0 || - 132
c >= self.cols { - 133○
return - 134
} - 135
self.data[((y * self.width + x) * self.rows + r) * self.cols + c] = value - 136
} - 137
- 138
///| - 139
/// 由梯度矢量场构建结构张量场(2×2 对称)。 - 140
/// 结构张量 J = [[Ix², Ix*Iy], [Ix*Iy, Iy²]],用于角点检测。 - 141
/// 从梯度向量场构建结构张量场 Build structure tensor field (2×2 symmetric) from a gradient vector field (dim=2). - 142
/// 结构张量 J,用于角点检测 Structure tensor J = [[Ix², Ix*Iy], [Ix*Iy, Iy²]], used for corner detection. - 143
pub fn TensorField::structure_tensor( - 144
gradient : VectorField, - 145
) -> TensorField raise @types.LoadError { - 146●
if gradient.dim != 2 { - 147●
raise @types.LoadError::DecodeFailed( - 148
"structure_tensor: 梯度场维度必须为2", - 149
) - 150
} - 151
let w = gradient.width - 152
let h = gradient.height - 153
let n = w * h - 154●
let data = Array::make(n * 4, 0.0F) - 155
for i in 0..<n { - 156●
let ix = gradient.data[i * 2] - 157
let iy = gradient.data[i * 2 + 1] - 158
data[i * 4] = ix * ix - 159
data[i * 4 + 1] = ix * iy - 160
data[i * 4 + 2] = ix * iy - 161
data[i * 4 + 3] = iy * iy - 162
} - 163●
TensorField::make(w, h, 2, 2, data) - 164
} - 165
- 166
///| - 167
/// 计算结构张量的两个特征值,返回矢量场(dim=2,[λ1, λ2],λ1 ≥ λ2)。 - 168
/// 特征值用于 Harris 角点响应:R = det(J) - k * trace(J)² = λ1*λ2 - k*(λ1+λ2)²。 - 169
/// 计算结构张量特征值 Compute eigenvalues of 2×2 structure tensors, returns VectorField (dim=2, [λ1, λ2], λ1 ≥ λ2). - 170
pub fn TensorField::eigenvalues_2x2( - 171
self : TensorField, - 172
) -> VectorField raise @types.LoadError { - 173●
if self.rows != 2 || self.cols != 2 { - 174○
raise @types.LoadError::DecodeFailed( - 175
"eigenvalues_2x2: 张量必须是 2×2", - 176
) - 177
} - 178
let n = self.width * self.height - 179●
let data = Array::make(n * 2, 0.0F) - 180
for i in 0..<n { - 181●
let a = self.data[i * 4] - 182
let b = self.data[i * 4 + 1] - 183
let c = self.data[i * 4 + 2] - 184
let d = self.data[i * 4 + 3] - 185
let trace = a + d - 186
let det = a * d - b * c - 187
let disc = trace * trace - 4.0F * det - 188◐
let sq = if disc >= 0.0F { disc.sqrt() } else { 0.0F } - 189
let l1 = (trace + sq) * 0.5F - 190
let l2 = (trace - sq) * 0.5F - 191
data[i * 2] = l1 - 192
data[i * 2 + 1] = l2 - 193
} - 194●
VectorField::make(self.width, self.height, 2, data) - 195
}