Skip to content
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

Feature/optiflow #93

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
4 changes: 3 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ mod windows {
let opencv_world_entry = files.filter_map(|entry| entry.as_ref().ok()).find(|entry| {
let file_name = entry.file_name().to_string_lossy().into_owned();
(file_name.starts_with(&format!("opencv_{}", name))
|| file_name.starts_with(&format!("libopencv_{}", name))) && !file_name.ends_with("d.lib")
|| file_name.starts_with(&format!("libopencv_{}", name)))
&& !file_name.ends_with("d.lib")
});
let lib = opencv_world_entry.ok_or_else(|| {
BuildError::new(format!(
Expand Down Expand Up @@ -100,6 +101,7 @@ mod unix {
println!("cargo:rustc-link-lib=opencv_img_hash");
println!("cargo:rustc-link-lib=opencv_imgcodecs");
println!("cargo:rustc-link-lib=opencv_imgproc");
println!("cargo:rustc-link-lib=opencv_optflow");
println!("cargo:rustc-link-lib=opencv_objdetect");
println!("cargo:rustc-link-lib=opencv_text");
println!("cargo:rustc-link-lib=opencv_videoio");
Expand Down
102 changes: 102 additions & 0 deletions native/optflow.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#include "optflow.h"
#include "utils.h"

extern "C" {

void calc_optical_flow_sf(cv::Mat* from,
cv::Mat* to,
cv::Mat* out,
int layers,
int averaging_block_size,
int max_flow,
double sigma_dist,
double sigma_color,
int postprocess_window,
double sigma_dist_fix,
double sigma_color_fix,
double occ_thr,
int upscale_averaging_radius,
double upscale_sigma_dist,
double upscale_sigma_color,
double speed_up_thr) {
cv::optflow::calcOpticalFlowSF(*from,
*to,
*out,
layers,
averaging_block_size,
max_flow,
sigma_dist,
sigma_color,
postprocess_window,
sigma_dist_fix,
sigma_color_fix,
occ_thr,
upscale_averaging_radius,
upscale_sigma_dist,
upscale_sigma_color,
speed_up_thr);
}

void calc_optical_flow_df(cv::Mat* from, cv::Mat* to, cv::Mat* out) {
auto optical_flow = cv::optflow::createOptFlow_DeepFlow();
optical_flow->calc(*from, *to, *out);
}

void calc_optical_flow_farneback(cv::Mat* from,
cv::Mat* to,
cv::Mat* out,
int numLevels,
double pyrScale,
bool fastPyramids,
int winSize,
int numIters,
int polyN,
double polySigma,
int flags) {
auto optical_flow =
cv::FarnebackOpticalFlow::create(numLevels, pyrScale, fastPyramids, winSize, numIters, polyN, polySigma, flags);

optical_flow->calc(*from, *to, *out);
}

void calc_optical_flow_dtvl1(cv::Mat* from,
cv::Mat* to,
cv::Mat* out,
double tau,
double lambda,
double theta,
int nscales,
int warps,
double epsilon,
int innerIterations,
int outerIterations,
double scaleStep,
double gamma,
int medianFiltering,
bool useInitialFlow) {
auto optical_flow = cv::DualTVL1OpticalFlow::create(tau,
lambda,
theta,
nscales,
warps,
epsilon,
innerIterations,
outerIterations,
scaleStep,
gamma,
medianFiltering,
useInitialFlow);

optical_flow->calc(*from, *to, *out);
}

void calc_optical_flow_dis(cv::Mat* from, cv::Mat* to, cv::Mat* out, unsigned int preset) {
auto optical_flow = cv::optflow::createOptFlow_DIS(preset);
optical_flow->calc(*from, *to, *out);
}

void calc_optical_flow_std(cv::Mat* from, cv::Mat* to, cv::Mat* out) {
auto optical_flow = cv::optflow::createOptFlow_SparseToDense();
optical_flow->calc(*from, *to, *out);
}
}
61 changes: 61 additions & 0 deletions native/optflow.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#ifndef CV_RS_OPTFLOW_H
#define CV_RS_OPTFLOW_H
norru marked this conversation as resolved.
Show resolved Hide resolved

#include "common.h"
#include <opencv2/core.hpp>
#include <opencv2/optflow.hpp>
#include <opencv2/video/tracking.hpp>

extern "C" {
void calc_optical_flow_sf(cv::Mat* from,
cv::Mat* to,
cv::Mat* out,
int layers,
int averaging_block_size,
int max_flow,
double sigma_dist,
double sigma_color,
int postprocess_window,
double sigma_dist_fix,
double sigma_color_fix,
double occ_thr,
int upscale_averaging_radius,
double upscale_sigma_dist,
double upscale_sigma_color,
double speed_up_thr);

void calc_optical_flow_df(cv::Mat* from, cv::Mat* to, cv::Mat* out);

void calc_optical_flow_farneback(cv::Mat* from,
cv::Mat* to,
cv::Mat* out,
int numLevels = 5,
double pyrScale = 0.5,
bool fastPyramids = false,
int winSize = 13,
int numIters = 10,
int polyN = 5,
double polySigma = 1.1,
int flags = 0);

void calc_optical_flow_dtvl1(cv::Mat* from,
cv::Mat* to,
cv::Mat* out,
double tau = 0.25,
double lambda = 0.15,
double theta = 0.3,
int nscales = 5,
int warps = 5,
double epsilon = 0.01,
int innerIterations = 30,
int outerIterations = 10,
double scaleStep = 0.8,
double gamma = 0.0,
int medianFiltering = 5,
bool useInitialFlow = false);

void calc_optical_flow_std(cv::Mat* from, cv::Mat* to, cv::Mat* out);

void calc_optical_flow_dis(cv::Mat* from, cv::Mat* to, cv::Mat* out, unsigned int preset);
}
#endif
2 changes: 2 additions & 0 deletions src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,8 @@ pub enum CvType {
Cv64FC1 = 6,
/// 8 bit, two channel (rarelly seen)
Cv8UC2 = 8,
/// 32 bit signed, two channels (uv vectors)
Cv32FC2 = 13,
/// 8 bit unsigned, three channels (RGB image)
Cv8UC3 = 16,
/// 8 bit signed, three channels (RGB image)
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ pub mod imgcodecs;
pub mod imgproc;
pub mod mat;
pub mod objdetect;
pub mod optflow;
pub mod text;
pub mod video;
pub mod videoio;
Expand Down
12 changes: 11 additions & 1 deletion src/mat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,11 +345,21 @@ impl Mat {
(min, max, min_loc, max_loc)
}

/// Copy specified channels from `self` to the specified channels of `other`
/// `Mat`.
pub fn mix_channels_to<T: AsRef<[(c_int, c_int)]>>(&self, other: &mut Mat, nsrcs: usize, ndsts: usize, from_to: T) {
norru marked this conversation as resolved.
Show resolved Hide resolved
let slice = from_to.as_ref();
let ptr = slice.as_ptr() as *const c_int;
unsafe {
cv_mat_mix_channels(self.inner, nsrcs, other.inner, ndsts, ptr, slice.len());
}
}

/// Copy specified channels from `self` to the specified channels of output
/// `Mat`.
// The usage (self.depth) here is buggy, it should actually be the type!
pub fn mix_channels<T: AsRef<[(c_int, c_int)]>>(&self, nsrcs: usize, ndsts: usize, from_to: T) -> Mat {
let m = Mat::with_size(self.rows, self.cols, self.depth);
let m = Mat::with_size(self.rows, self.cols, self.cv_type() as i32);
let slice = from_to.as_ref();
let ptr = slice.as_ptr() as *const c_int;
unsafe {
Expand Down
Loading