From 2c378133286fa38ca7f3c1fad860c0120dc0c3a5 Mon Sep 17 00:00:00 2001
From: Nicolas Pope <nwpope@utu.fi>
Date: Tue, 12 Mar 2019 17:02:45 +0200
Subject: [PATCH] All force no stereo

---
 cv-node/include/ftl/local.hpp |  5 +++--
 cv-node/src/calibrate.cpp     | 29 +++++++++++++++++------------
 cv-node/src/local.cpp         | 15 ++++++++-------
 cv-node/src/main.cpp          |  9 ++++++---
 4 files changed, 34 insertions(+), 24 deletions(-)

diff --git a/cv-node/include/ftl/local.hpp b/cv-node/include/ftl/local.hpp
index 13196dd9e..1ada957ad 100644
--- a/cv-node/include/ftl/local.hpp
+++ b/cv-node/include/ftl/local.hpp
@@ -11,8 +11,8 @@ namespace cv {
 namespace ftl {
 class LocalSource {
 	public:
-	LocalSource(bool flip=false);
-	LocalSource(const std::string &vid, bool flip=false);
+	LocalSource(bool flip=false, bool nostereo=false);
+	LocalSource(const std::string &vid, bool flip=false, bool nostereo=false);
 	
 	bool left(cv::Mat &m);
 	bool right(cv::Mat &m);
@@ -30,6 +30,7 @@ class LocalSource {
 	bool stereo_;
 	//float fps_;
 	bool flip_;
+	bool nostereo_;
 	cv::VideoCapture *camera_a_;
 	cv::VideoCapture *camera_b_;
 };
diff --git a/cv-node/src/calibrate.cpp b/cv-node/src/calibrate.cpp
index 14e0ca3d5..e7a5f35de 100644
--- a/cv-node/src/calibrate.cpp
+++ b/cv-node/src/calibrate.cpp
@@ -321,15 +321,18 @@ bool Calibrate::_recalibrate(vector<vector<Point2f>> *imagePoints,
         //view = _nextImage(cam);
         LOG(INFO) << "Grabbing calibration image...";
         
-        if (view[0].empty() || view[1].empty() || imagePoints[0].size() >= (size_t)settings_.nrFrames) {
+        if (view[0].empty() || (local_->isStereo() && view[1].empty()) || imagePoints[0].size() >= (size_t)settings_.nrFrames) {
         	settings_.outputFileName = FTL_CONFIG_ROOT "/cam0.xml";
         	bool r = runCalibrationAndSave(settings_, imageSize[0],
         					cameraMatrix[0], distCoeffs[0], imagePoints[0],
         					grid_width, release_object);
-        	settings_.outputFileName = FTL_CONFIG_ROOT "/cam1.xml";
-        	r &= runCalibrationAndSave(settings_, imageSize[1],
-        					cameraMatrix[1], distCoeffs[1], imagePoints[1],
-        					grid_width, release_object);
+        					
+        	if (local_->isStereo()) {
+		    	settings_.outputFileName = FTL_CONFIG_ROOT "/cam1.xml";
+		    	r &= runCalibrationAndSave(settings_, imageSize[1],
+		    					cameraMatrix[1], distCoeffs[1], imagePoints[1],
+		    					grid_width, release_object);
+        	}
         					
         	if (!r && view[0].empty()) {
         		LOG(ERROR) << "Not enough frames to calibrate";
@@ -351,7 +354,7 @@ bool Calibrate::_recalibrate(vector<vector<Point2f>> *imagePoints,
         bool found1,found2;
 
 		found1 = findChessboardCorners( view[0], settings_.boardSize, pointBuf[0], chessBoardFlags);
-		found2 = findChessboardCorners( view[1], settings_.boardSize, pointBuf[1], chessBoardFlags);
+		found2 = !local_->isStereo() || findChessboardCorners( view[1], settings_.boardSize, pointBuf[1], chessBoardFlags);
       
         if (found1 && found2)                // If done with success,
         {
@@ -360,23 +363,25 @@ bool Calibrate::_recalibrate(vector<vector<Point2f>> *imagePoints,
                     cvtColor(view[0], viewGray, COLOR_BGR2GRAY);
                     cornerSubPix( viewGray, pointBuf[0], Size(winSize,winSize),
                         Size(-1,-1), TermCriteria( TermCriteria::EPS+TermCriteria::COUNT, 30, 0.0001 ));
-                        
+                imagePoints[0].push_back(pointBuf[0]);
+                  
+                if (local_->isStereo()) {      
                     cvtColor(view[1], viewGray, COLOR_BGR2GRAY);
                     cornerSubPix( viewGray, pointBuf[1], Size(winSize,winSize),
                         Size(-1,-1), TermCriteria( TermCriteria::EPS+TermCriteria::COUNT, 30, 0.0001 ));
-                
-                imagePoints[0].push_back(pointBuf[0]);
-                imagePoints[1].push_back(pointBuf[1]);
+					imagePoints[1].push_back(pointBuf[1]);
+                }
+
 
                 // Draw the corners.
                 drawChessboardCorners( view[0], settings_.boardSize, Mat(pointBuf[0]), found1 );
-                drawChessboardCorners( view[1], settings_.boardSize, Mat(pointBuf[1]), found2 );
+                if (local_->isStereo()) drawChessboardCorners( view[1], settings_.boardSize, Mat(pointBuf[1]), found2 );
         } else {
         	LOG(WARNING) << "No calibration pattern found";
         }
 
         imshow("Left", view[0]);
-        imshow("Right", view[1]);
+        if (local_->isStereo()) imshow("Right", view[1]);
         char key = (char)waitKey(settings_.delay);
 
         if( key  == 27 )
diff --git a/cv-node/src/local.cpp b/cv-node/src/local.cpp
index 075e19f39..9b9c7b329 100644
--- a/cv-node/src/local.cpp
+++ b/cv-node/src/local.cpp
@@ -13,22 +13,23 @@ using cv::Rect;
 using std::string;
 using namespace std::chrono;
 
-LocalSource::LocalSource(bool flip) : timestamp_(0.0), flip_(flip) {
+LocalSource::LocalSource(bool flip, bool nostereo) : timestamp_(0.0), flip_(flip), nostereo_(nostereo) {
 	// Use cameras
 	camera_a_ = new VideoCapture((flip) ? 1 : 0);
-	camera_b_ = new VideoCapture((flip) ? 0 : 1);
+	if (!nostereo) camera_b_ = new VideoCapture((flip) ? 0 : 1);
+	else camera_b_ = nullptr;
 
 	if (!camera_a_->isOpened()) {
 		delete camera_a_;
-		delete camera_b_;
+		if (camera_b_) delete camera_b_;
 		camera_a_ = nullptr;
 		camera_b_ = nullptr;
 		LOG(FATAL) << "No cameras found";
 		return;
 	}
 
-	if (!camera_b_->isOpened()) {
-		delete camera_b_;
+	if (!camera_b_ || !camera_b_->isOpened()) {
+		if (camera_b_) delete camera_b_;
 		camera_b_ = nullptr;
 		stereo_ = false;
 		LOG(WARNING) << "Not able to find second camera for stereo";
@@ -37,7 +38,7 @@ LocalSource::LocalSource(bool flip) : timestamp_(0.0), flip_(flip) {
 	}
 }
 
-LocalSource::LocalSource(const string &vid, bool flip): timestamp_(0.0), flip_(flip) {
+LocalSource::LocalSource(const string &vid, bool flip, bool nostereo): timestamp_(0.0), flip_(flip), nostereo_(nostereo) {
 	if (vid == "") {
 		LOG(FATAL) << "No video file specified";
 		camera_a_ = nullptr;
@@ -191,6 +192,6 @@ double LocalSource::getTimestamp() const {
 }
 	
 bool LocalSource::isStereo() const {
-	return stereo_;
+	return stereo_ && !nostereo_;
 }
 
diff --git a/cv-node/src/main.cpp b/cv-node/src/main.cpp
index 8708bee42..fb535deec 100644
--- a/cv-node/src/main.cpp
+++ b/cv-node/src/main.cpp
@@ -20,6 +20,7 @@ static string OPTION_config;
 static bool OPTION_display = false;
 static bool OPTION_calibrate = false;
 static bool OPTION_flip = false;
+static bool OPTION_nostereo = false;
 
 void handle_options(char ***argv, int *argc) {
 	while (*argc > 0) {
@@ -44,6 +45,8 @@ void handle_options(char ***argv, int *argc) {
 			OPTION_display = true;
 		} else if (cmd.find("--flip") == 0) {
 			OPTION_flip = true;
+		} else if (cmd.find("--no-stereo") == 0) {
+			OPTION_nostereo = true;
 		}
 		
 		(*argc)--;
@@ -64,10 +67,10 @@ int main(int argc, char **argv) {
 	
 	if (argc) {
 		// Load video file
-		lsrc = new LocalSource(argv[0], OPTION_flip);
+		lsrc = new LocalSource(argv[0], OPTION_flip, OPTION_nostereo);
 	} else {
 		// Use cameras
-		lsrc = new LocalSource(OPTION_flip);
+		lsrc = new LocalSource(OPTION_flip, OPTION_nostereo);
 	}
 	
 	auto sync = new SyncSource(); // TODO Pass protocol object
@@ -108,7 +111,7 @@ int main(int argc, char **argv) {
 		cv::imshow("Left",l);
 		if (lsrc->isStereo()) cv::imshow("Right",r);
 		
-		if(cv::waitKey(10000) == 27){
+		if(cv::waitKey(100) == 27){
             //exit if ESC is pressed
             break;
         }
-- 
GitLab