-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptical_flow.zig
More file actions
77 lines (64 loc) · 2.27 KB
/
optical_flow.zig
File metadata and controls
77 lines (64 loc) · 2.27 KB
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
const std = @import("std");
const cv = @import("zopencv");
pub fn main() !void {
std.debug.print("=== Optical Flow Example ===\n", .{});
std.debug.print("Demonstrates dense optical flow (Farneback) on video\n\n", .{});
const video_path = "testdata/videos/vtest.avi";
var cap = try cv.videoio.VideoCapture.init();
defer cap.deinit();
const opened = try cap.open(video_path);
if (!opened or !cap.isOpened()) {
std.debug.print("❌ Failed to open: {s}\n", .{video_path});
return;
}
std.debug.print("✅ Opened: {s}\n", .{video_path});
var frame = try cv.Mat.init();
defer frame.deinit();
var gray = try cv.Mat.init();
defer gray.deinit();
var prev_gray = try cv.Mat.init();
defer prev_gray.deinit();
var flow = try cv.Mat.init();
defer flow.deinit();
// Read first frame
if (!cap.read(&frame) or frame.empty()) {
std.debug.print("❌ Cannot read first frame\n", .{});
return;
}
cv.imgproc.cvtColor(frame, &prev_gray, .bgr2gray);
var frame_count: u32 = 0;
const max_frames: u32 = 50;
while (cap.read(&frame)) {
if (frame.empty()) break;
if (frame_count >= max_frames) break;
frame_count += 1;
cv.imgproc.cvtColor(frame, &gray, .bgr2gray);
// Dense optical flow (Farneback)
cv.video.calcOpticalFlowFarneback(
prev_gray,
gray,
&flow,
0.5, // pyr_scale
3, // levels
15, // winsize
3, // iterations
5, // poly_n
1.2, // poly_sigma
0, // flags
);
if (frame_count % 10 == 0) {
std.debug.print(" Frame {d}: flow size = {d}x{d}\n", .{
frame_count, flow.cols(), flow.rows(),
});
}
// Save a sample flow visualization at frame 20
if (frame_count == 20) {
try cv.imgcodecs.imwrite("examples/tmp/optical_flow_frame.jpg", frame);
std.debug.print(" 💾 Saved flow frame at frame 20\n", .{});
}
// Copy current to previous
gray.copyTo(&prev_gray);
}
std.debug.print("\n📊 Computed optical flow for {d} frames\n", .{frame_count});
std.debug.print("🎉 Optical flow complete!\n", .{});
}