1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
// Lucas-Kanade 稀疏光流(纯 MoonBit)
// 在两帧之间跟踪特征点的运动
// 参考: Lucas & Kanade, "An Iterative Image Registration Technique"
// (1981)

///|
/// 光流结果
pub(all) struct FlowResult {
  x : Float // 原始 x 坐标
  y : Float // 原始 y 坐标
  u : Float // x 方向位移
  v : Float // y 方向位移
  found : Bool // 是否成功跟踪
}

///|
/// Lucas-Kanade 稀疏光流。
/// 在 prev 和 next 两帧之间跟踪 points 中的特征点。
/// window_size 为搜索窗口大小(默认15),max_iter 为迭代次数(默认20)。
/// 返回每个点的光流结果。
pub fn lucas_kanade(
  prev : @types.Image,
  next : @types.Image,
  points : Array[(Int, Int)],
  window_size? : Int = 15,
  max_iter? : Int = 20,
) -> Array[FlowResult] raise @types.LoadError {
  // 输入验证
  if prev.width <= 0 || prev.height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "lucas_kanade: prev图像尺寸必须大于0",
    )
  }
  if next.width <= 0 || next.height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "lucas_kanade: next图像尺寸必须大于0",
    )
  }
  if prev.channels < 1 || prev.channels > 4 {
    raise @types.LoadError::DecodeFailed(
      "lucas_kanade: prev通道数必须在1-4之间",
    )
  }
  if next.channels < 1 || next.channels > 4 {
    raise @types.LoadError::DecodeFailed(
      "lucas_kanade: next通道数必须在1-4之间",
    )
  }
  let prev_expected_size = prev.width * prev.height * prev.channels
  if prev.data.length() != prev_expected_size {
    raise @types.LoadError::DecodeFailed(
      "lucas_kanade: prev数据大小不匹配,期望 " +
      prev_expected_size.to_string() +
      ",实际 " +
      prev.data.length().to_string(),
    )
  }
  let next_expected_size = next.width * next.height * next.channels
  if next.data.length() != next_expected_size {
    raise @types.LoadError::DecodeFailed(
      "lucas_kanade: next数据大小不匹配,期望 " +
      next_expected_size.to_string() +
      ",实际 " +
      next.data.length().to_string(),
    )
  }
  if prev.width != next.width || prev.height != next.height {
    raise @types.LoadError::DecodeFailed(
      "lucas_kanade: 两帧图像尺寸必须一致",
    )
  }
  if points.length() == 0 {
    raise @types.LoadError::DecodeFailed(
      "lucas_kanade: points数组不能为空",
    )
  }
  if window_size < 3 || window_size > 31 || window_size % 2 == 0 {
    raise @types.LoadError::DecodeFailed(
      "lucas_kanade: window_size必须为3..31之间的奇数",
    )
  }
  if max_iter <= 0 {
    raise @types.LoadError::DecodeFailed("lucas_kanade: max_iter必须大于0")
  }
  let w = prev.width
  let h = prev.height
  // 转灰度
  let (gray_prev, _) = to_gray_lk(prev)
  let (gray_next, _) = to_gray_lk(next)
  let half_win = window_size / 2
  let results : Array[FlowResult] = []
  for i in 0..<points.length() {
    let (px, py) = points[i]
    // 初始化位移
    let mut u = 0.0F
    let mut v = 0.0F
    let mut found = false
    // 迭代求解
    for _ in 0..<max_iter {
      // 构建 Lucas-Kanade 方程:A^T A * d = A^T b
      let mut ixx = 0.0F
      let mut ixy = 0.0F
      let mut iyy = 0.0F
      let mut ix_it = 0.0F
      let mut iy_it = 0.0F
      for dy in -half_win..<=half_win {
        for dx in -half_win..<=half_win {
          let x = px + dx
          let y = py + dy
          if x < 1 || x >= w - 1 || y < 1 || y >= h - 1 {
            continue
          }
          // 空间梯度(中心差分)
          let ix = Float::from_int(
              gray_prev[y * w + x + 1] - gray_prev[y * w + x - 1],
            ) /
            2.0F
          let iy = Float::from_int(
              gray_prev[(y + 1) * w + x] - gray_prev[(y - 1) * w + x],
            ) /
            2.0F
          // 时间差分:I_t = I_next(x+u, y+v) - I_prev(x, y)
          let nx = Float::from_int(x) + u
          let ny = Float::from_int(y) + v
          let next_val = sample_bilinear_lk(gray_next, w, h, nx, ny)
          let prev_val = Float::from_int(gray_prev[y * w + x])
          let it = next_val - prev_val
          // 累加 A^T A 和 A^T b
          ixx += ix * ix
          ixy += ix * iy
          iyy += iy * iy
          ix_it += ix * it
          iy_it += iy * it
        }
      }
      // 求解 2x2 线性方程
      let det = ixx * iyy - ixy * ixy
      if det.abs() < 0.01F {
        break
      }
      let du = -(iyy * ix_it - ixy * iy_it) / det
      let dv = -(ixx * iy_it - ixy * ix_it) / det
      u += du
      v += dv
      // 收敛检测
      if du.abs() < 0.01F && dv.abs() < 0.01F {
        found = true
        break
      }
      found = true
    }
    results.push(FlowResult::{
      x: Float::from_int(px),
      y: Float::from_int(py),
      u,
      v,
      found,
    })
  }
  results
}

///|
/// Horn-Schunck 密集光流(简化版)。
/// 计算两帧之间所有像素的光流场。
/// alpha 为平滑权重(默认1.0),iterations 为迭代次数(默认100)。
/// 返回 (u_field, v_field),每个为 w*h 的 Float 数组。
pub fn horn_schunck(
  prev : @types.Image,
  next : @types.Image,
  alpha? : Float = 1.0F,
  iterations? : Int = 100,
) -> (Array[Float], Array[Float]) raise @types.LoadError {
  // 输入验证
  if prev.width <= 0 || prev.height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "horn_schunck: prev图像尺寸必须大于0",
    )
  }
  if next.width <= 0 || next.height <= 0 {
    raise @types.LoadError::DecodeFailed(
      "horn_schunck: next图像尺寸必须大于0",
    )
  }
  if prev.channels < 1 || prev.channels > 4 {
    raise @types.LoadError::DecodeFailed(
      "horn_schunck: prev通道数必须在1-4之间",
    )
  }
  if next.channels < 1 || next.channels > 4 {
    raise @types.LoadError::DecodeFailed(
      "horn_schunck: next通道数必须在1-4之间",
    )
  }
  let prev_expected_size = prev.width * prev.height * prev.channels
  if prev.data.length() != prev_expected_size {
    raise @types.LoadError::DecodeFailed(
      "horn_schunck: prev数据大小不匹配,期望 " +
      prev_expected_size.to_string() +
      ",实际 " +
      prev.data.length().to_string(),
    )
  }
  let next_expected_size = next.width * next.height * next.channels
  if next.data.length() != next_expected_size {
    raise @types.LoadError::DecodeFailed(
      "horn_schunck: next数据大小不匹配,期望 " +
      next_expected_size.to_string() +
      ",实际 " +
      next.data.length().to_string(),
    )
  }
  if prev.width != next.width || prev.height != next.height {
    raise @types.LoadError::DecodeFailed(
      "horn_schunck: 两帧图像尺寸必须一致",
    )
  }
  if alpha <= 0.0F {
    raise @types.LoadError::DecodeFailed("horn_schunck: alpha必须大于0")
  }
  if iterations <= 0 {
    raise @types.LoadError::DecodeFailed(
      "horn_schunck: iterations必须大于0",
    )
  }
  let w = prev.width
  let h = prev.height
  let n = w * h
  let (gray_prev, _) = to_gray_lk(prev)
  let (gray_next, _) = to_gray_lk(next)
  // 计算空间梯度和时间梯度
  let ex : Array[Float] = Array::make(n, 0.0F)
  let ey : Array[Float] = Array::make(n, 0.0F)
  let et : Array[Float] = Array::make(n, 0.0F)
  for y in 0..<h {
    for x in 0..<w {
      let idx = y * w + x
      let xl = if x > 0 { x - 1 } else { 0 }
      let xr = if x < w - 1 { x + 1 } else { w - 1 }
      let yu = if y > 0 { y - 1 } else { 0 }
      let yd = if y < h - 1 { y + 1 } else { h - 1 }
      // Ex = (I(x+1,y) - I(x-1,y) + J(x+1,y) - J(x-1,y)) / 4
      ex[idx] = Float::from_int(
          gray_prev[y * w + xr] -
          gray_prev[y * w + xl] +
          gray_next[y * w + xr] -
          gray_next[y * w + xl],
        ) /
        4.0F
      // Ey = (I(x,y+1) - I(x,y-1) + J(x,y+1) - J(x,y-1)) / 4
      ey[idx] = Float::from_int(
          gray_prev[yd * w + x] -
          gray_prev[yu * w + x] +
          gray_next[yd * w + x] -
          gray_next[yu * w + x],
        ) /
        4.0F
      // Et = J(x,y) - I(x,y)
      et[idx] = Float::from_int(gray_next[idx] - gray_prev[idx])
    }
  }
  // 初始化光流场
  let u : Array[Float] = Array::make(n, 0.0F)
  let v : Array[Float] = Array::make(n, 0.0F)
  let alpha_sq = alpha * alpha
  // 迭代求解
  for _ in 0..<iterations {
    let u_prev = u.copy()
    let v_prev = v.copy()
    for y in 0..<h {
      for x in 0..<w {
        let idx = y * w + x
        // 计算 u, v 的邻域平均(4 邻域)
        let xl = if x > 0 { idx - 1 } else { idx }
        let xr = if x < w - 1 { idx + 1 } else { idx }
        let yu = if y > 0 { idx - w } else { idx }
        let yd = if y < h - 1 { idx + w } else { idx }
        let u_avg = (u_prev[xl] + u_prev[xr] + u_prev[yu] + u_prev[yd]) / 4.0F
        let v_avg = (v_prev[xl] + v_prev[xr] + v_prev[yu] + v_prev[yd]) / 4.0F
        // Horn-Schunck 更新
        let denom = alpha_sq + ex[idx] * ex[idx] + ey[idx] * ey[idx]
        if denom > 0.0F {
          let common = ex[idx] * u_avg + ey[idx] * v_avg + et[idx]
          u[idx] = u_avg - ex[idx] * common / denom
          v[idx] = v_avg - ey[idx] * common / denom
        }
      }
    }
  }
  (u, v)
}

// ==================== 辅助函数 ====================

///|
fn to_gray_lk(img : @types.Image) -> (Array[Int], Int) {
  let w = img.width
  let h = img.height
  let ch = img.channels
  let n = w * h
  let gray : Array[Int] = Array::make(n, 0)
  for i in 0..<n {
    if ch >= 3 {
      let r = img.data[i * ch].to_int()
      let g = img.data[i * ch + 1].to_int()
      let b = img.data[i * ch + 2].to_int()
      gray[i] = (r * 299 + g * 587 + b * 114) / 1000
    } else {
      gray[i] = img.data[i * ch].to_int()
    }
  }
  (gray, n)
}

///|
fn sample_bilinear_lk(
  gray : Array[Int],
  w : Int,
  h : Int,
  fx : Float,
  fy : Float,
) -> Float {
  let x0 = fx.to_int()
  let y0 = fy.to_int()
  let x1 = x0 + 1
  let y1 = y0 + 1
  let x0c = x0.clamp(min=0, max=w - 1)
  let x1c = x1.clamp(min=0, max=w - 1)
  let y0c = y0.clamp(min=0, max=h - 1)
  let y1c = y1.clamp(min=0, max=h - 1)
  let p00 = gray[y0c * w + x0c]
  let p01 = gray[y0c * w + x1c]
  let p10 = gray[y1c * w + x0c]
  let p11 = gray[y1c * w + x1c]
  let wx = fx - Float::from_int(x0)
  let wy = fy - Float::from_int(y0)
  Float::from_int(p00) * (1.0F - wx) * (1.0F - wy) +
  Float::from_int(p01) * wx * (1.0F - wy) +
  Float::from_int(p10) * (1.0F - wx) * wy +
  Float::from_int(p11) * wx * wy
}