From 44c55a8a99f7f0728e3b60dc4218538ad5d07ed9 Mon Sep 17 00:00:00 2001
From: Sebastian Hahta <joseha@utu.fi>
Date: Fri, 8 Nov 2019 11:58:48 +0200
Subject: [PATCH] operator: bilateral filter

---
 .../include/ftl/operators/disparity.hpp       |  2 ++
 .../src/disparity/bilateral_filter.cpp        | 20 +++++++++++++++++++
 .../src/disparity/disparity_to_depth.cpp      |  2 +-
 .../operators/src/disparity/fixstars_sgm.cpp  |  2 +-
 .../src/sources/stereovideo/stereovideo.cpp   |  1 +
 5 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/components/operators/include/ftl/operators/disparity.hpp b/components/operators/include/ftl/operators/disparity.hpp
index 5a9533a57..efa3a5128 100644
--- a/components/operators/include/ftl/operators/disparity.hpp
+++ b/components/operators/include/ftl/operators/disparity.hpp
@@ -49,6 +49,8 @@ class DisparityBilateralFilter : public::ftl::operators::Operator {
 	private:
 	cv::Ptr<cv::cuda::DisparityBilateralFilter> filter_;
 	cv::cuda::GpuMat disp_int_;
+	cv::cuda::GpuMat disp_int_result_;
+	double scale_;
 	int radius_;
 	int iter_;
 	int max_disp_;
diff --git a/components/operators/src/disparity/bilateral_filter.cpp b/components/operators/src/disparity/bilateral_filter.cpp
index 73ded1af8..8489e6a4b 100644
--- a/components/operators/src/disparity/bilateral_filter.cpp
+++ b/components/operators/src/disparity/bilateral_filter.cpp
@@ -5,12 +5,32 @@
 using cv::cuda::GpuMat;
 using cv::Size;
 
+using ftl::codecs::Channel;
 using ftl::operators::DisparityBilateralFilter;
 
 DisparityBilateralFilter::DisparityBilateralFilter(ftl::Configurable* cfg) :
 		ftl::operators::Operator(cfg) {
 	
+	scale_ = 16.0;
 	radius_ = cfg->value("radius", 7);
 	iter_ = cfg->value("iter", 13);
 	filter_ = cv::cuda::createDisparityBilateralFilter(max_disp_ << 4, radius_, iter_);
+}
+
+bool DisparityBilateralFilter::apply(ftl::rgbd::Frame &in, ftl::rgbd::Frame &out,
+									 ftl::rgbd::Source *src, cudaStream_t stream) {
+
+	if (!in.hasChannel(Channel::Disparity) || !in.hasChannel(Channel::Colour)) {
+		return false;
+	}
+	auto cvstream = cv::cuda::StreamAccessor::wrapStream(stream);
+	const GpuMat &rgb = in.get<GpuMat>(Channel::Colour);
+	GpuMat &disp_in = in.get<GpuMat>(Channel::Disparity);
+	GpuMat &disp_out = out.create<GpuMat>(Channel::Disparity);
+	disp_out.create(disp_in.size(), disp_in.type());
+
+	disp_in.convertTo(disp_int_, CV_16SC1, scale_, cvstream);
+	filter_->apply(disp_int_, rgb, disp_int_result_, cvstream);
+	disp_int_result_.convertTo(disp_out, 1.0/scale_, cvstream);
+	return true;
 }
\ No newline at end of file
diff --git a/components/operators/src/disparity/disparity_to_depth.cpp b/components/operators/src/disparity/disparity_to_depth.cpp
index 9f793c234..5951802f0 100644
--- a/components/operators/src/disparity/disparity_to_depth.cpp
+++ b/components/operators/src/disparity/disparity_to_depth.cpp
@@ -14,7 +14,7 @@ bool DisparityToDepth::apply(ftl::rgbd::Frame &in, ftl::rgbd::Frame &out,
 	const auto params = src->parameters();
 	const GpuMat &disp = in.get<GpuMat>(Channel::Disparity);
 
-	GpuMat depth = out.create<GpuMat>(Channel::Depth);
+	GpuMat &depth = out.create<GpuMat>(Channel::Depth);
 	depth.create(disp.size(), CV_32FC1);
 
 	ftl::cuda::disparity_to_depth(disp, depth, params, stream);
diff --git a/components/operators/src/disparity/fixstars_sgm.cpp b/components/operators/src/disparity/fixstars_sgm.cpp
index 80d8761ba..831ee7a21 100644
--- a/components/operators/src/disparity/fixstars_sgm.cpp
+++ b/components/operators/src/disparity/fixstars_sgm.cpp
@@ -32,7 +32,7 @@ FixstarsSGM::FixstarsSGM(ftl::Configurable* cfg) :
 		LOG(WARNING) << "Invalid value for P1, using default (10)";
 	}
 
-	if (P2_ > P1_) {
+	if (P2_ < P1_) {
 		P2_ = P1_;
 		LOG(WARNING) << "Invalid value for P2, using value of P1 (" << P1_ << ")";
 	}
diff --git a/components/rgbd-sources/src/sources/stereovideo/stereovideo.cpp b/components/rgbd-sources/src/sources/stereovideo/stereovideo.cpp
index 58e374dcb..aaff4ec7b 100644
--- a/components/rgbd-sources/src/sources/stereovideo/stereovideo.cpp
+++ b/components/rgbd-sources/src/sources/stereovideo/stereovideo.cpp
@@ -126,6 +126,7 @@ void StereoVideoSource::init(const string &file) {
 
 	pipeline_depth_ = ftl::config::create<ftl::operators::Graph>(host_, "pipeline_disparity");
 	pipeline_depth_->append<ftl::operators::FixstarsSGM>("algorithm");
+	pipeline_depth_->append<ftl::operators::DisparityBilateralFilter>("bilateral_filter");
 	pipeline_depth_->append<ftl::operators::DisparitySmoothingOF>("optflow_filter");
 	pipeline_depth_->append<ftl::operators::DisparityToDepth>("calculate_depth");
 
-- 
GitLab