From 4c0de255bd20aea618823986fb50fcf5bcb2c43f Mon Sep 17 00:00:00 2001 From: Sebastian Hahta <joseha@utu.fi> Date: Fri, 17 Apr 2020 13:57:47 +0300 Subject: [PATCH] input parameters for census and mi costs --- lib/libstereo/include/stereo.hpp | 32 ++++++++++++++++++++++++- lib/libstereo/src/costs/census.cu | 21 ++++++++++++++++ lib/libstereo/src/costs/census.hpp | 9 +++---- lib/libstereo/src/stereo_adcensussgm.cu | 2 +- lib/libstereo/src/stereo_censussgm.cu | 3 +-- lib/libstereo/src/stereo_misgm.cu | 13 ++++++++-- 6 files changed, 70 insertions(+), 10 deletions(-) diff --git a/lib/libstereo/include/stereo.hpp b/lib/libstereo/include/stereo.hpp index bd7b38910..48d58f9a0 100644 --- a/lib/libstereo/include/stereo.hpp +++ b/lib/libstereo/include/stereo.hpp @@ -87,7 +87,7 @@ public: ~StereoMiSgm(); void compute(cv::InputArray l, cv::InputArray r, cv::OutputArray disparity); - void setPrior(const cv::Mat &disp); + void setPrior(cv::InputArray disp); struct Parameters { int d_min = 0; @@ -109,6 +109,36 @@ private: }; +class StereoMiSgm2 { +public: + StereoMiSgm2(); + ~StereoMiSgm2(); + + void compute(cv::InputArray l, cv::InputArray r, cv::OutputArray disparity); + void setPrior(cv::InputArray disp); + + struct Parameters { + int d_min = 0; + int d_max = 0; + unsigned short P1 = 2; + unsigned short P2 = 8; + float uniqueness = std::numeric_limits<short>::max(); + int subpixel = 1; // subpixel interpolation method + int paths = AggregationDirections::HORIZONTAL | + AggregationDirections::VERTICAL | + AggregationDirections::DIAGONAL; + bool debug = false; + float w1 = 1.0; + float w2 = 1.0; + }; + Parameters params; + +private: + struct Impl; + Impl *impl_; +}; + + /** * Census + SGM + prior * diff --git a/lib/libstereo/src/costs/census.cu b/lib/libstereo/src/costs/census.cu index 05d61c3b7..3930e7333 100644 --- a/lib/libstereo/src/costs/census.cu +++ b/lib/libstereo/src/costs/census.cu @@ -46,3 +46,24 @@ void CensusMatchingCost::set(const Array2D<uchar> &l, const Array2D<uchar> &r) { parallel2D<algorithms::CensusTransform<9,7>>({l.data(), ct_l_.data()}, l.width, l.height); parallel2D<algorithms::CensusTransform<9,7>>({r.data(), ct_r_.data()}, r.width, r.height); } + +void CensusMatchingCost::set(cv::InputArray l, cv::InputArray r) { + if (l.type() != CV_8UC1 || r.type() != CV_8UC1) { throw std::exception(); } + if (l.rows() != r.rows() || l.cols() != r.cols() || l.rows() != height() || l.cols() != width()) { + throw std::exception(); + } + + if (l.isGpuMat() && r.isGpuMat()) { + auto ml = l.getGpuMat(); + auto mr = r.getGpuMat(); + set(Array2D<uchar>(ml), Array2D<uchar>(mr)); + } + else if (l.isMat() && r.isMat()) { + auto ml = l.getMat(); + auto mr = r.getMat(); + set(Array2D<uchar>(ml), Array2D<uchar>(mr)); + } + else { + throw std::exception(); + } +} diff --git a/lib/libstereo/src/costs/census.hpp b/lib/libstereo/src/costs/census.hpp index bc20efd31..c8467b491 100644 --- a/lib/libstereo/src/costs/census.hpp +++ b/lib/libstereo/src/costs/census.hpp @@ -39,17 +39,17 @@ public: typedef unsigned short Type; CensusMatchingCost() : DSBase<DataType>(0, 0, 0, 0) {}; - CensusMatchingCost(int width, int height, int disp_min, int disp_max, int wwidth, int wheight) + CensusMatchingCost(int width, int height, int disp_min, int disp_max) : DSBase<DataType>(width, height, disp_min, disp_max), ct_l_(width, height), ct_r_(width,height), //cost_max(wwidth * wheight), - ww_(wwidth), wh_(wheight) + ww_(9), wh_(7) { - if (wwidth != 9 || wheight != 7) { + if (ww_ != 9 || wh_ != 7) { // TODO: window size paramters (hardcoded) in matching_cost.cpp throw std::exception(); } - if (wwidth*wheight > 64) { + if (ww_*wh_ > 64) { throw std::exception(); // todo: dynamic size } @@ -57,6 +57,7 @@ public: data().ct_r = ct_r_.data(); } + void set(cv::InputArray l, cv::InputArray r); void set(const Array2D<uchar>& l, const Array2D<uchar>& r); static constexpr short COST_MAX = 9*7; // TODO: window size paramters (hardcoded) in matching_cost.cpp diff --git a/lib/libstereo/src/stereo_adcensussgm.cu b/lib/libstereo/src/stereo_adcensussgm.cu index 9cd9838cf..3baecec53 100644 --- a/lib/libstereo/src/stereo_adcensussgm.cu +++ b/lib/libstereo/src/stereo_adcensussgm.cu @@ -69,7 +69,7 @@ struct StereoADCensusSgm::Impl { Impl(int width, int height, int min_disp, int max_disp) : dsi(width, height, min_disp, max_disp), ad_cost(width, height, min_disp, max_disp), - census_cost(width, height, min_disp, max_disp, 9, 7), + census_cost(width, height, min_disp, max_disp), cost(width, height, min_disp, max_disp, ad_cost, census_cost), cost_min(width, height), cost_min_paths(width, height), diff --git a/lib/libstereo/src/stereo_censussgm.cu b/lib/libstereo/src/stereo_censussgm.cu index 7cb8f635e..f3969a5f3 100644 --- a/lib/libstereo/src/stereo_censussgm.cu +++ b/lib/libstereo/src/stereo_censussgm.cu @@ -63,8 +63,7 @@ struct StereoCensusSgm::Impl { WinnerTakesAll<DSImage16U,float> wta; Impl(int width, int height, int min_disp, int max_disp) : - //dsi(width, height, min_disp, max_disp), - cost(width, height, min_disp, max_disp, ct_windows_w, ct_windows_h), + cost(width, height, min_disp, max_disp), cost_min(width, height), cost_min_paths(width, height), uncertainty(width, height), diff --git a/lib/libstereo/src/stereo_misgm.cu b/lib/libstereo/src/stereo_misgm.cu index aebc09a65..6c54cce0c 100644 --- a/lib/libstereo/src/stereo_misgm.cu +++ b/lib/libstereo/src/stereo_misgm.cu @@ -47,6 +47,7 @@ using ftl::stereo::aggregations::StandardSGM; struct StereoMiSgm::Impl { MutualInformationMatchingCost cost; + Array2D<unsigned short> cost_min; Array2D<unsigned short> cost_min_paths; Array2D<unsigned short> uncertainty; @@ -74,8 +75,16 @@ StereoMiSgm::StereoMiSgm() : impl_(nullptr) { impl_ = new Impl(0, 0, 0, 0); } -void StereoMiSgm::setPrior(const cv::Mat &prior) { - prior.copyTo(impl_->prior); +void StereoMiSgm::setPrior(cv::InputArray prior) { + if (prior.rows() != impl_->cost.height() || prior.cols() != impl_->cost.width()) { + return; + } + if (prior.isGpuMat()) { + prior.getGpuMat().download(impl_->prior); + } + else { + prior.getMat().copyTo(impl_->prior); + } } -- GitLab