From d9b954be15d09b961d5d6e62559a7c088efd8032 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Fri, 1 Nov 2019 17:56:26 +0200
Subject: [PATCH] Iterate the MLS

---
 components/filters/src/smoothing.cpp | 36 +++++++++++++++-------------
 components/filters/src/smoothing.cu  |  2 +-
 2 files changed, 21 insertions(+), 17 deletions(-)

diff --git a/components/filters/src/smoothing.cpp b/components/filters/src/smoothing.cpp
index 517179976..e28051745 100644
--- a/components/filters/src/smoothing.cpp
+++ b/components/filters/src/smoothing.cpp
@@ -71,24 +71,28 @@ void MLSSmoother::smooth(ftl::rgbd::Frame &f, ftl::rgbd::Source *s) {
 	bool do_smooth = value("mls_smooth", false);
 	if (!do_smooth) return;
 
-	if (!f.hasChannel(Channel::Normals)) {
-		ftl::cuda::normals(
-			f.createTexture<float4>(Channel::Normals, ftl::rgbd::Format<float4>(f.get<cv::cuda::GpuMat>(Channel::Depth).size())),
+	float thresh = value("mls_threshold", 0.04f);
+	int iters = value("mls_iterations", 1);
+
+	for (int i=0; i<iters; ++i) {
+		if (i > 0 || !f.hasChannel(Channel::Normals)) {
+			ftl::cuda::normals(
+				f.createTexture<float4>(Channel::Normals, ftl::rgbd::Format<float4>(f.get<cv::cuda::GpuMat>(Channel::Depth).size())),
+				f.createTexture<float>(Channel::Depth),
+				s->parameters(), 0
+			);
+		}
+
+		ftl::cuda::mls_smooth(
+			f.createTexture<float4>(Channel::Normals),
 			f.createTexture<float>(Channel::Depth),
-			s->parameters(), 0
+			f.createTexture<float>(Channel::Depth2, ftl::rgbd::Format<float>(f.get<cv::cuda::GpuMat>(Channel::Depth).size())),
+			thresh,
+			s->parameters(),
+			0
 		);
-	}
 
-	float thresh = value("mls_threshold", 0.04f);
-	ftl::cuda::mls_smooth(
-		f.createTexture<float4>(Channel::Normals),
-		f.createTexture<float>(Channel::Depth),
-		f.createTexture<float>(Channel::Depth2, ftl::rgbd::Format<float>(f.get<cv::cuda::GpuMat>(Channel::Depth).size())),
-		thresh,
-		s->parameters(),
-		0
-	);
-
-	f.swapChannels(Channel::Depth, Channel::Depth2);
+		f.swapChannels(Channel::Depth, Channel::Depth2);
+	}
 }
 
diff --git a/components/filters/src/smoothing.cu b/components/filters/src/smoothing.cu
index 0ba749d25..d7d2a8a3f 100644
--- a/components/filters/src/smoothing.cu
+++ b/components/filters/src/smoothing.cu
@@ -79,7 +79,7 @@ void ftl::cuda::mls_smooth(
 	const dim3 gridSize((depth_out.width() + T_PER_BLOCK - 1)/T_PER_BLOCK, (depth_out.height() + T_PER_BLOCK - 1)/T_PER_BLOCK);
 	const dim3 blockSize(T_PER_BLOCK, T_PER_BLOCK);
 
-	mls_smooth_kernel<3><<<gridSize, blockSize, 0, stream>>>(normals, depth_in, depth_out, smoothing, camera);
+	mls_smooth_kernel<2><<<gridSize, blockSize, 0, stream>>>(normals, depth_in, depth_out, smoothing, camera);
 	cudaSafeCall( cudaGetLastError() );
 
 
-- 
GitLab