From b5faa5b24923b314635284fa8ea19fe25a4de094 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Sun, 3 Nov 2019 13:39:17 +0200
Subject: [PATCH] Working smoothing channel

---
 .../include/ftl/operators/smoothing.hpp       |  2 +-
 components/operators/src/smoothing.cpp        | 35 ++++++++++---------
 components/rgbd-sources/src/source.cpp        |  2 +-
 3 files changed, 21 insertions(+), 18 deletions(-)

diff --git a/components/operators/include/ftl/operators/smoothing.hpp b/components/operators/include/ftl/operators/smoothing.hpp
index 1f6961d81..b5362ea6f 100644
--- a/components/operators/include/ftl/operators/smoothing.hpp
+++ b/components/operators/include/ftl/operators/smoothing.hpp
@@ -43,7 +43,7 @@ class SmoothChannel : public ftl::operators::Operator {
     bool apply(ftl::rgbd::Frame &in, ftl::rgbd::Frame &out, ftl::rgbd::Source *src, cudaStream_t stream) override;
 
 	private:
-	ftl::rgbd::Frame temp_[2];
+	ftl::rgbd::Frame temp_[6];
 };
 
 /**
diff --git a/components/operators/src/smoothing.cpp b/components/operators/src/smoothing.cpp
index 79f372077..61dd9f8ce 100644
--- a/components/operators/src/smoothing.cpp
+++ b/components/operators/src/smoothing.cpp
@@ -71,8 +71,9 @@ SmoothChannel::~SmoothChannel() {
 }
 
 bool SmoothChannel::apply(ftl::rgbd::Frame &in, ftl::rgbd::Frame &out, ftl::rgbd::Source *s, cudaStream_t stream) {
-	int radius = config()->value("radius", 1);
+	int radius = config()->value("radius", 3);
 	float threshold = config()->value("threshold", 30.0f);
+	int iters = max(0, min(6, config()->value("levels", 4)));
 
 	int width = s->parameters().width;
 	int height = s->parameters().height;
@@ -91,24 +92,26 @@ bool SmoothChannel::apply(ftl::rgbd::Frame &in, ftl::rgbd::Frame &out, ftl::rgbd
 		stream
 	);
 
-	width /= 2;
-	height /= 2;
+	for (int i=0; i<iters; ++i) {
+		width /= 2;
+		height /= 2;
 
-	ftl::rgbd::Camera scaledCam = s->parameters().scaled(width, height);
+		ftl::rgbd::Camera scaledCam = s->parameters().scaled(width, height);
 
-	// Downscale images for next pass
-	cv::cuda::resize(in.get<GpuMat>(Channel::Colour), temp_[0].create<GpuMat>(Channel::Colour), cv::Size(width, height), 0.0, 0.0, cv::INTER_LINEAR);
-	cv::cuda::resize(in.get<GpuMat>(Channel::Depth), temp_[0].create<GpuMat>(Channel::Depth), cv::Size(width, height), 0.0, 0.0, cv::INTER_NEAREST);
+		// Downscale images for next pass
+		cv::cuda::resize(in.get<GpuMat>(Channel::Colour), temp_[i].create<GpuMat>(Channel::Colour), cv::Size(width, height), 0.0, 0.0, cv::INTER_LINEAR);
+		cv::cuda::resize(in.get<GpuMat>(Channel::Depth), temp_[i].create<GpuMat>(Channel::Depth), cv::Size(width, height), 0.0, 0.0, cv::INTER_NEAREST);
 
-	ftl::cuda::smooth_channel(
-		temp_[0].createTexture<uchar4>(Channel::Colour),
-		temp_[0].createTexture<float>(Channel::Depth),
-		out.getTexture<float>(Channel::Smoothing),
-		scaledCam,
-		threshold,
-		radius,
-		stream
-	);
+		ftl::cuda::smooth_channel(
+			temp_[i].createTexture<uchar4>(Channel::Colour),
+			temp_[i].createTexture<float>(Channel::Depth),
+			out.getTexture<float>(Channel::Smoothing),
+			scaledCam,
+			threshold,
+			radius,
+			stream
+		);
+	}
 
 	return true;
 }
diff --git a/components/rgbd-sources/src/source.cpp b/components/rgbd-sources/src/source.cpp
index 24629e139..b0ee77a44 100644
--- a/components/rgbd-sources/src/source.cpp
+++ b/components/rgbd-sources/src/source.cpp
@@ -285,7 +285,7 @@ Camera Camera::scaled(int width, int height) const {
 	float scaleX = (float)width / (float)cam.width;
 	float scaleY = (float)height / (float)cam.height;
 
-	CHECK( abs(scaleX - scaleY) < 0.000001f );
+	CHECK( abs(scaleX - scaleY) < 0.00000001f );
 
 	Camera newcam = cam;
 	newcam.width = width;
-- 
GitLab