From e4ad5bfeec68b9906510bdd7e3cf1bcc707fb712 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Thu, 20 Jun 2019 11:25:28 +0300
Subject: [PATCH] Tweaks to reg parameters

---
 .../registration/src/correspondances.cpp       | 18 ++++++++++--------
 applications/registration/src/manual.cpp       |  4 ++--
 applications/registration/src/sfm.cpp          |  4 ++--
 3 files changed, 14 insertions(+), 12 deletions(-)

diff --git a/applications/registration/src/correspondances.cpp b/applications/registration/src/correspondances.cpp
index 5292905e9..6d4ac2b03 100644
--- a/applications/registration/src/correspondances.cpp
+++ b/applications/registration/src/correspondances.cpp
@@ -37,6 +37,7 @@ Correspondances::Correspondances(Correspondances *parent, Source *src)
 static void averageDepth(vector<Mat> &in, Mat &out, float varThresh) {
 	const int rows = in[0].rows;
 	const int cols = in[0].cols;
+	float varThresh2 = varThresh*varThresh;
 
 	// todo: create new only if out is empty (or wrong shape/type)
 	out = Mat(rows, cols, CV_32FC1);
@@ -62,16 +63,16 @@ static void averageDepth(vector<Mat> &in, Mat &out, float varThresh) {
 			for (int i_in = 0; i_in < in.size(); ++i_in) {
 				double d = in[i_in].at<float>(i);
 				if (d < 40.0) {
-					double delta = (d*1000.0 - sum*1000.0);
+					double delta = (d - sum);
 					var += delta*delta;
+
+					//LOG(INFO) << "VAR " << delta;
 				}
 			}
 			if (good_values > 1) var /= (double)(good_values-1);
 			else var = 0.0;
 
-			//LOG(INFO) << "VAR " << var;
-
-			if (var < varThresh) {
+			if (var < varThresh2) {
 				out.at<float>(i) = (float)sum;
 			} else {
 				out.at<float>(i) = 41.0f;
@@ -119,13 +120,14 @@ bool Correspondances::capture(cv::Mat &rgb1, cv::Mat &rgb2) {
 		targ_->grab();
 		src_->getFrames(rgb1, d1);
 		targ_->getFrames(rgb2, d2);
-		buffer[0][i] = d1;
-		buffer[1][i] = d2;
+
+		d1.copyTo(buffer[0][i]);
+		d2.copyTo(buffer[1][i]);
 
 		std::this_thread::sleep_for(std::chrono::milliseconds(50));
 	}
-	averageDepth(buffer[0], d1, 0.00000005f);
-	averageDepth(buffer[1], d2, 0.00000005f);
+	averageDepth(buffer[0], d1, 0.01f);
+	averageDepth(buffer[1], d2, 0.01f);
 	Mat d1_v, d2_v;
 	d1.convertTo(d1_v, CV_8U, 255.0f / 10.0f);
 	d2.convertTo(d2_v, CV_8U, 255.0f / 10.0f);
diff --git a/applications/registration/src/manual.cpp b/applications/registration/src/manual.cpp
index be729155c..3138a0ae8 100644
--- a/applications/registration/src/manual.cpp
+++ b/applications/registration/src/manual.cpp
@@ -328,7 +328,7 @@ void ftl::registration::manual(ftl::Configurable *root) {
 					lastScore = 1000.0;
 					do {
 						Eigen::Matrix4d tr;
-						float s = current->findBestSubset(tr, (int)(current->screenPoints().size() * 0.4f), 100);
+						float s = current->findBestSubset(tr, 20, 100); // (int)(current->screenPoints().size() * 0.4f)
 						LOG(INFO) << "SCORE = " << s;
 						if (s < lastScore) {
 							lastScore = s;
@@ -353,7 +353,7 @@ void ftl::registration::manual(ftl::Configurable *root) {
 				current->capture(f1,f2);
 
 				vector<tuple<int,int,int,int>> feat;
-				if (ftl::registration::featuresSIFT(f1, f2, feat, 2)) {
+				if (ftl::registration::featuresSIFT(f1, f2, feat, 10)) {
 					for (int j=0; j<feat.size(); j++) {
 						auto [sx,sy,tx,ty] = feat[j];
 						current->add(tx, ty, sx, sy);
diff --git a/applications/registration/src/sfm.cpp b/applications/registration/src/sfm.cpp
index 5d5e7cc9a..43711312d 100644
--- a/applications/registration/src/sfm.cpp
+++ b/applications/registration/src/sfm.cpp
@@ -109,7 +109,7 @@ bool ftl::registration::featuresChess(cv::Mat &frame1, cv::Mat &frame2, std::vec
 
 bool ftl::registration::featuresSIFT(cv::Mat &frame1, cv::Mat &frame2, std::vector<std::tuple<int,int,int,int>> &points, int K) {
 	int minHessian = 400;
-	Ptr<SIFT> detector = SIFT::create();
+	Ptr<SIFT> detector = SIFT::create(10000,3,0.04,10);
 	//detector->setHessianThreshold(minHessian);
 
 	vector<vector<KeyPoint>> keypoints;
@@ -139,7 +139,7 @@ bool ftl::registration::featuresSIFT(cv::Mat &frame1, cv::Mat &frame2, std::vect
 	std::vector<std::vector< DMatch >> matches;
 	matcher.knnMatch( descriptors[0], descriptors[1], matches, K );
 	
-	const float ratio_thresh = 0.7f;
+	const float ratio_thresh = 0.9f;
     std::vector<DMatch> good_matches;
     for (size_t i = 0; i < matches.size(); i++)
     {
-- 
GitLab