Skip to content

Unit/opt flow #611

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added tests/data/optflow/1080p_00.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tests/data/optflow/1080p_01.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
92 changes: 92 additions & 0 deletions tests/notebooks/calc_opt_flow_pyr_lk.ipynb

Large diffs are not rendered by default.

85 changes: 85 additions & 0 deletions tests/optflowpyrlk.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#![cfg(ocvrs_has_module_video)]

use std::cmp::Ordering;

use opencv as cv;
use opencv::core as cv_core;
use cv::{features2d::*, Result};
use cv_core::{ Size, TermCriteria, VectorToVec, KeyPoint, Mat, Ptr, Vector, Point2f};
use cv::imgcodecs::*;

#[test]
pub fn check_optical_flow_points() -> Result<()>{
//let mut detector = ORBFeatureDetector::create();
let mut detector = AKAZE::create_def()?;

let prev_img = imread("tests/data/optflow/1080p_00.png", ImreadModes::IMREAD_GRAYSCALE.into()).unwrap();
let cur_img = imread("tests/data/optflow/1080p_01.png", ImreadModes::IMREAD_GRAYSCALE.into()).unwrap();

let mut prev_kps = Vector::<KeyPoint>::new();
let mut prev_desc = Mat::default();
let mask = Mat::default();
let _ = detector.detect_and_compute(&prev_img, &mask,&mut prev_kps, &mut prev_desc, false);

let mut ref_pts = Vector::<Point2f>::new();
let _ = KeyPoint::convert_def(&prev_kps, &mut ref_pts); //Converts the KeyPoints to raw Points2f

debug_keypoints(&ref_pts);

let next_kps = track_pyr_lk(&prev_img, &cur_img, &ref_pts);

println!("dumping key_points: ");
debug_keypoints(&next_kps);

let detects = next_kps.len();
println!("detects: {}", detects);

// NOTE: See python results in tests/notebook/calc_opt_flow_pyr_lk.ipynb
//
// I assume This should FAIL as the python bindings return nonzero results
// I mean like some items might be zero but not all ??
let total: f32 = next_kps.iter().map(|k| k.x + k.y).sum();
assert_ne!(total, 0.0);

Ok(())
}

fn track_pyr_lk(prev_img: &Mat, cur_img: &Mat, ref_pts: &Vector<Point2f>) -> Vector::<Point2f>{

let mut cur_key_points = Vector::<Point2f>::new();
let min_eig = 1e-4;
let flags = 0; // OPTFLOW_LK_GET_MIN_EIGENVALS vs OPTFLOW_USE_INITIAL_FLOW
let size = Size::new(21,21);
let crit = TermCriteria::default().unwrap(); // TermCriteria::new(typ, 30 , 0.01).unwrap();
let max_levels=3;
let mut err = Vector::<f32>::new();
let mut status = Vector::<u8>::new();

let _ =cv::video::calc_optical_flow_pyr_lk(
&prev_img,
&cur_img,
&ref_pts,
&mut cur_key_points,
&mut status,
&mut err,
size,
max_levels,
crit,
flags,
min_eig,
);

cur_key_points

}

pub fn debug_keypoints(kps: &Vector<Point2f>){
let points = kps.to_vec();
println!("[debug] keypoints len:{}" , kps.len());

if kps.len() < 3 { return ; };

points[0..3].iter().for_each(|v| println!("{}, {}", v.x, v.y));
println!("....,");
points[points.len()-3..].iter().for_each(|v| println!("{}, {}", v.x, v.y));
}
Loading