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
25 changes: 25 additions & 0 deletions .cproject
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
norru marked this conversation as resolved.
Show resolved Hide resolved
<?fileVersion 4.0.0?><cproject storage_type_id="org.eclipse.cdt.core.XmlProjectDescriptionStorage">
<storageModule moduleId="org.eclipse.cdt.core.settings">
<cconfiguration id="org.eclipse.cdt.core.default.config.2008514342">
<storageModule buildSystemId="org.eclipse.cdt.core.defaultConfigDataProvider" id="org.eclipse.cdt.core.default.config.2008514342" moduleId="org.eclipse.cdt.core.settings" name="Configuration">
<externalSettings/>
<extensions/>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.externalSettings"/>
</cconfiguration>
</storageModule>
<storageModule moduleId="org.eclipse.cdt.core.LanguageSettingsProviders"/>
<storageModule moduleId="org.eclipse.cdt.internal.ui.text.commentOwnerProjectMappings"/>
<storageModule moduleId="org.eclipse.cdt.core.pathentry">
<pathentry include="/usr/include" kind="inc" path="" system="true"/>
<pathentry include="/usr/include/linux" kind="inc" path="" system="true"/>
<pathentry include="/usr/include/x86_64-linux-gnu" kind="inc" path="" system="true"/>
<pathentry include="/usr/include/x86_64-linux-gnu/c++/7" kind="inc" path="" system="true"/>
<pathentry include="/usr/include/c++/7" kind="inc" path="" system="true"/>
<pathentry include="/usr/include/c++/7/tr1" kind="inc" path="" system="true"/>
<pathentry include="/usr/local/include" kind="inc" path="" system="true"/>
<pathentry kind="src" path=""/>
<pathentry kind="out" path="target"/>
</storageModule>
</cproject>
20 changes: 20 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>cv-rs</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.cdt.core.cBuilder</name>
<triggers>clean,full,incremental,</triggers>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.cdt.core.cnature</nature>
<nature>org.eclipse.cdt.core.ccnature</nature>
<nature>org.eclipse.cdt.make.core.makeNature</nature>
</natures>
</projectDescription>
15 changes: 15 additions & 0 deletions .settings/org.eclipse.cdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
eclipse.preferences.version=1
indexer/indexAllFiles=true
indexer/indexAllHeaderVersions=true
indexer/indexAllVersionsSpecificHeaders=
indexer/indexOnOpen=true
indexer/indexUnusedHeadersWithAlternateLang=true
indexer/indexUnusedHeadersWithDefaultLang=true
indexer/indexerId=org.eclipse.cdt.core.fastIndexer
indexer/skipFilesLargerThanMB=100
indexer/skipImplicitReferences=false
indexer/skipIncludedFilesLargerThanMB=100
indexer/skipMacroReferences=false
indexer/skipReferences=false
indexer/skipTypeReferences=false
indexer/useHeuristicIncludeResolution=false
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
126 changes: 126 additions & 0 deletions native/optflow.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
#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) {
auto array_from = cv::InputArray(*from);
auto array_to = cv::InputArray(*to);
auto array_out = cv::OutputArray(*out);
cv::optflow::calcOpticalFlowSF(array_from,
array_to,
array_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 array_from = cv::InputArray(*from);
auto array_to = cv::InputArray(*to);
auto array_out = cv::InputOutputArray(*out);

auto optical_flow = cv::optflow::createOptFlow_DeepFlow();

optical_flow->calc(array_from, array_to, array_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 array_from = cv::InputArray(*from);
auto array_to = cv::InputArray(*to);
auto array_out = cv::InputOutputArray(*out);
norru marked this conversation as resolved.
Show resolved Hide resolved

auto optical_flow =
cv::FarnebackOpticalFlow::create(numLevels, pyrScale, fastPyramids, winSize, numIters, polyN, polySigma, flags);

optical_flow->calc(array_from, array_to, array_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 array_from = cv::InputArray(*from);
auto array_to = cv::InputArray(*to);
auto array_out = cv::InputOutputArray(*out);

auto optical_flow = cv::DualTVL1OpticalFlow::create(tau,
lambda,
theta,
nscales,
warps,
epsilon,
innerIterations,
outerIterations,
scaleStep,
gamma,
medianFiltering,
useInitialFlow);

optical_flow->calc(array_from, array_to, array_out);
}

void calc_optical_flow_dis(cv::Mat* from, cv::Mat* to, cv::Mat* out, unsigned int preset) {
auto array_from = cv::InputArray(*from);
auto array_to = cv::InputArray(*to);
auto array_out = cv::InputOutputArray(*out);

auto optical_flow = cv::optflow::createOptFlow_DIS(preset);
optical_flow->calc(array_from, array_to, array_out);
}

void calc_optical_flow_std(cv::Mat* from, cv::Mat* to, cv::Mat* out) {
auto array_from = cv::InputArray(*from);
auto array_to = cv::InputArray(*to);
auto array_out = cv::InputOutputArray(*out);

auto optical_flow = cv::optflow::createOptFlow_SparseToDense();
optical_flow->calc(array_from, array_to, array_out);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can these calls be wrapped? these functions could throw exceptions

see my pr here - using EmptyResult::FromFunction or Result::FromFunction would be useful so that errors don't panic

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They probably can and I'm pretty sure there'll be plenty of other preconditions that can be verified, but I can't dedicate more time to refine the wrapper at this stage.

}
}
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