1 // This file is part of OpenMVG, an Open Multiple View Geometry C++ library. 2 3 // Copyright (c) 2018 Yan Qingsong, Pierre Moulon. 4 5 // This Source Code Form is subject to the terms of the Mozilla Public 6 // License, v. 2.0. If a copy of the MPL was not distributed with this 7 // file, You can obtain one at http://mozilla.org/MPL/2.0/. 8 9 #ifndef IO_READ_GT_MIDDLEBURY_HPP 10 #define IO_READ_GT_MIDDLEBURY_HPP 11 12 #include "io_readGTInterface.hpp" 13 #include "io_loadImages.hpp" 14 15 #include "openMVG/cameras/PinholeCamera.hpp" 16 17 // The feature of the MiddleBury's Data: 18 // 1. all the gt information are store in one file 19 // 2. the image file's name is stored in the gt file 20 class SfM_Data_GT_Loader_MiddleBury : public SfM_Data_GT_Loader_Interface 21 { 22 private: 23 std::vector<cameras::PinholeCamera> cameras_data_; // Store all the camera information 24 public: loadGT()25 bool loadGT() override 26 { 27 // Check all the files under the path 28 std::vector<std::string> gt_files = stlplus::folder_files( this->gt_dir_ ); 29 30 // Because the MiddleBury's Data store all the data in one file 31 // So make sure there is only one file under the gt_dir 32 if (gt_files.size()!=1) 33 { 34 std::cerr << "Error: Maybe give wrong gt_dir!"<<std::endl 35 <<"Make sure there in only one file under the gt_dir!"<<std::endl; 36 return false; 37 } 38 39 // Load the gt_data from the file 40 std::ifstream gt_file( stlplus::create_filespec(this->gt_dir_,gt_files[0]).c_str(), std::ifstream::in); 41 if (!gt_file) 42 { 43 std::cerr << "Error: Failed to open file '" << gt_files[0] << "' for reading" << std::endl; 44 return false; 45 } 46 47 // Image_number 48 int image_count = 0; 49 gt_file >> image_count; 50 51 cameras_data_.reserve(image_count); 52 53 for(int i=0;i<image_count;i++) 54 { 55 std::string image_name; 56 Mat3 K, R; 57 Vec3 t; 58 59 gt_file >> image_name; 60 gt_file >> K(0,0) >> K(0,1) >> K(0,2) 61 >> K(1,0) >> K(1,1) >> K(1,2) 62 >> K(2,0) >> K(2,1) >> K(2,2); 63 gt_file >> R(0,0) >> R(0,1) >> R(0,2) 64 >> R(1,0) >> R(1,1) >> R(1,2) 65 >> R(2,0) >> R(2,1) >> R(2,2); 66 gt_file >> t(0,0) >> t(1,0) >> t(2,0); 67 68 Mat34 P; 69 P_From_KRt(K, R, t, &P); 70 cameras_data_.emplace_back(P); 71 72 // Parse image name 73 images_.push_back( image_name ); 74 75 } 76 gt_file.close(); 77 78 return true; 79 } 80 loadImages()81 bool loadImages() override 82 { 83 return LoadImages(this->image_dir_, this->images_, this->cameras_data_, this->sfm_data_); 84 } 85 }; 86 87 #endif // IO_READ_GT_MIDDLEBURY_HPP 88