1 #include <boost/regex.hpp>
2 #include <opencv2/opencv.hpp>
21 std::string sensorType =
"";
24 boost::filesystem::path metaPath = dir /
"meta.yaml";
25 if(boost::filesystem::exists(metaPath))
27 YAML::Node meta = YAML::LoadFile(metaPath.string());
30 if(meta[
"sensor_type"])
32 sensorType = meta[
"sensor_type"].as<std::string>();
39 const boost::filesystem::path& root,
41 const size_t& positionNumber,
42 const size_t& camNumber,
43 const size_t& imageNumber)
45 std::stringstream posStr;
46 posStr << std::setfill(
'0') << std::setw(8) << positionNumber;
48 std::stringstream camStr;
49 camStr << std::setfill(
'0') << std::setw(8) << camNumber;
51 saveScanImage(root, image, posStr.str(), camStr.str(), imageNumber);
55 const boost::filesystem::path& root,
57 const std::string positionDirectory,
58 const size_t& cameraNumber,
59 const size_t& imageNumber)
61 std::stringstream camStr;
62 camStr << std::setfill(
'0') << std::setw(8) << cameraNumber;
64 saveScanImage(root, image, positionDirectory, camStr.str(), imageNumber);
68 boost::filesystem::path root,
69 const std::string positionDirectory,
70 const std::string cameraDirectory)
72 boost::filesystem::path pos(positionDirectory);
73 boost::filesystem::path cam(cameraDirectory);
74 boost::filesystem::path data(
"data/");
75 return root / pos /cam / data;
79 const boost::filesystem::path& root,
81 const std::string positionDirectory,
82 const std::string cameraDirectory,
83 const size_t& imageNumber)
86 std::stringstream metaFileName;
87 metaFileName << std::setfill(
'0') << std::setw(8) << imageNumber <<
".yaml";
89 std::stringstream imageFileName;
90 imageFileName << std::setfill(
'0') << std::setw(8) << imageNumber <<
".png";
92 boost::filesystem::path imageDirectory =
95 if(!boost::filesystem::exists(imageDirectory))
97 std::cout <<
timestamp <<
"Creating: " << imageDirectory << std::endl;
98 boost::filesystem::create_directory(imageDirectory);
101 boost::filesystem::path imagePath = imageDirectory / imageFileName.str();
102 boost::filesystem::path metaPath = imageDirectory / metaFileName.str();
108 std::ofstream out(metaPath.c_str());
111 std::cout <<
timestamp <<
"Writing " << metaPath << std::endl;
117 <<
"Warning: to write " << metaPath << std::endl;
121 std::cout <<
timestamp <<
"Writing " << imagePath << std::endl;
122 cv::imwrite(imagePath.string(), image.
image);
126 const boost::filesystem::path& root,
128 const size_t& positionNumber,
129 const size_t& cameraNumber,
130 const size_t& imageNumber)
132 std::stringstream posStr;
133 posStr << std::setfill(
'0') << std::setw(8) << positionNumber;
135 std::stringstream camStr;
136 camStr << std::setfill(
'0') << std::setw(8) << cameraNumber;
138 return loadScanImage(root, image, posStr.str(), camStr.str(), imageNumber);
142 const boost::filesystem::path& root,
144 const std::string& positionDirectory,
145 const size_t& cameraNumber,
146 const size_t& imageNumber)
148 std::stringstream camStr;
149 camStr << std::setfill(
'0') << std::setw(8) << cameraNumber;
151 return loadScanImage(root, image, positionDirectory, camStr.str(), imageNumber);
155 const boost::filesystem::path& root,
157 const std::string& positionDirectory,
158 const std::string& cameraDirectory,
159 const size_t& imageNumber)
162 std::stringstream metaFileName;
163 metaFileName << std::setfill(
'0') << std::setw(8) << imageNumber <<
".yaml";
165 std::stringstream imageFileName;
166 imageFileName << std::setfill(
'0') << std::setw(8) << imageNumber <<
".png";
168 boost::filesystem::path imageDirectory =
171 boost::filesystem::path imagePath(imageDirectory / boost::filesystem::path(imageFileName.str()));
172 boost::filesystem::path metaPath(imageDirectory / boost::filesystem::path(metaFileName.str()));
175 std::cout <<
timestamp <<
"Loading " << metaPath << std::endl;
176 YAML::Node meta = YAML::LoadFile(metaPath.string());
180 std::cout <<
timestamp <<
"Loading " << imagePath << std::endl;
182 image.
image = cv::imread(imagePath.string());
188 std::vector<ScanImagePtr>& images,
189 boost::filesystem::path dataPath)
195 std::stringstream metaStr;
196 metaStr << std::setfill(
'0') << std::setw(8) << c <<
".yaml";
198 std::stringstream pngStr;
199 pngStr << std::setfill(
'0') << std::setw(8) << c <<
".png";
201 boost::filesystem::path metaPath = dataPath / metaStr.str();
202 boost::filesystem::path pngPath = dataPath / pngStr.str();
205 if(boost::filesystem::exists(metaPath)
206 && boost::filesystem::exists(pngPath) )
211 std::cout <<
timestamp <<
"Loading " << metaPath << std::endl;
212 YAML::Node meta = YAML::LoadFile(metaPath.string());
215 if(YAML::convert<ScanImage>::decode(meta, *image))
218 std::cout <<
timestamp <<
"Loading " << pngPath << std::endl;
220 image->
image = cv::imread(pngPath.string());
225 std::cout <<
timestamp <<
"Could not convert " << metaPath << std::endl;
233 <<
timestamp <<
"Read " << c <<
" images from " << dataPath << std::endl;
244 boost::filesystem::path root,
245 const std::string positionDirectory,
246 const std::string cameraDirectory)
248 boost::filesystem::path pos(positionDirectory);
249 boost::filesystem::path cam(cameraDirectory);
250 return root / pos /cam;
255 const boost::filesystem::path& root,
257 const std::string positionDirectory,
258 const std::string cameraDirectory)
260 boost::filesystem::path cameraPath =
263 if(!boost::filesystem::exists(cameraPath))
265 std::cout <<
timestamp <<
"Creating: " << cameraPath << std::endl;
266 boost::filesystem::create_directory(cameraPath);
269 boost::filesystem::path metaPath(cameraPath /
"meta.yaml");
275 std::ofstream out(metaPath.c_str());
278 std::cout <<
timestamp <<
"Writing " << metaPath << std::endl;
284 <<
"Warning: Unable to write " << metaPath << std::endl;
288 for(
size_t i = 0; i < camera.
images.size(); i++)
295 const boost::filesystem::path& root,
297 const std::string& positionDirectory,
298 const size_t& cameraNumber)
300 std::stringstream camStr;
301 camStr <<
"cam_" << cameraNumber;
303 return saveScanCamera(root, camera, positionDirectory, camStr.str());
307 const boost::filesystem::path& root,
309 const size_t& positionNumber,
310 const size_t& cameraNumber)
312 std::stringstream posStr;
313 posStr << std::setfill(
'0') << std::setw(8) << positionNumber;
320 const boost::filesystem::path& root,
322 const std::string& positionDirectory,
323 const size_t& cameraNumber)
325 std::stringstream camStr;
326 camStr << std::setfill(
'0') << std::setw(8) << cameraNumber;
328 return loadScanCamera(root, camera, positionDirectory, camStr.str());
332 const boost::filesystem::path& root,
334 const size_t& positionNumber,
335 const size_t& cameraNumber)
337 std::stringstream posStr;
338 posStr << std::setfill(
'0') << std::setw(8) << positionNumber;
340 std::stringstream camStr;
341 camStr << std::setfill(
'0') << std::setw(8) << cameraNumber;
347 const boost::filesystem::path& root,
349 const std::string& positionDirectory,
350 const std::string& cameraDirectory)
352 boost::filesystem::path cameraPath =
358 boost::filesystem::path metaPath = cameraPath /
"meta.yaml";
361 std::cout <<
timestamp <<
"Loading " << metaPath << std::endl;
362 YAML::Node meta = YAML::LoadFile(metaPath.string());
380 const boost::filesystem::path& root,
382 const std::string positionDirectory,
383 const std::string scanDirectory,
384 const std::string scanName)
387 boost::filesystem::path scanPosPath = root / positionDirectory;
389 if(!boost::filesystem::exists(scanPosPath))
391 std::cout <<
timestamp <<
"Creating: " << scanPosPath << std::endl;
392 boost::filesystem::create_directory(scanPosPath);
395 boost::filesystem::path scanPath = scanPosPath / scanDirectory;
396 if(!boost::filesystem::exists(scanPath))
398 std::cout <<
timestamp <<
"Creating: " << scanPath << std::endl;
399 boost::filesystem::create_directory(scanPath);
403 boost::filesystem::path metaPath = scanPath /
"meta.yaml";
404 if(!boost::filesystem::exists(metaPath))
409 std::ofstream out(metaPath.c_str());
412 std::cout <<
timestamp <<
"Writing " << metaPath << std::endl;
418 <<
"Warning: Unable to write " << metaPath << std::endl;
423 boost::filesystem::path scanDataPath = scanPath /
"data";
424 if(!boost::filesystem::exists(scanDataPath))
426 std::cout <<
timestamp <<
"Creating: " << scanDataPath << std::endl;
427 boost::filesystem::create_directory(scanDataPath);
430 boost::filesystem::path scanOut = scanDataPath / (scanName +
".ply");
431 boost::filesystem::path scanMetaOut = scanDataPath / (scanName +
".yaml");
439 std::ofstream out(scanMetaOut.string());
442 std::cout <<
timestamp <<
"Writing " << scanMetaOut << std::endl;
448 <<
"Warning: Unable to write " << scanMetaOut << std::endl;
452 std::cout <<
timestamp <<
"Writing " << scanOut << std::endl;
455 model->m_pointCloud = scan.
points;
460 const boost::filesystem::path& root,
462 const std::string positionDirectory,
463 const std::string scanDirectory,
464 const size_t& scanNumber)
466 std::stringstream scanStr;
467 scanStr << std::setfill(
'0') << std::setw(8) << scanNumber;
469 saveScan(root, scan, positionDirectory, scanDirectory, scanStr.str());
473 const boost::filesystem::path& root,
475 const size_t& positionNumber,
476 const size_t& scanNumber)
478 std::stringstream posStr;
479 posStr << std::setfill(
'0') << std::setw(8) << positionNumber;
481 std::stringstream scanStr;
482 scanStr << std::setfill(
'0') << std::setw(8) << scanNumber <<
".ply";
484 saveScan(root, scan, posStr.str(),
"scans", scanStr.str());
488 const boost::filesystem::path& root,
490 const std::string& positionDirectory,
491 const std::string& scanSubDirectory,
492 const std::string& scanName)
495 boost::filesystem::path scanDirectoryPath = root / positionDirectory / scanSubDirectory;
497 if(!boost::filesystem::exists(scanDirectoryPath))
499 std::cerr <<
timestamp <<
"Could not open " << scanDirectoryPath << std::endl;
507 boost::filesystem::path scanDataPath = scanDirectoryPath /
"data";
510 boost::filesystem::path metaPath = scanDataPath / (scanName +
".yaml");
511 std::cout <<
timestamp <<
"Loading " << metaPath << std::endl;
512 YAML::Node meta = YAML::LoadFile(metaPath.string());
513 scan = meta.as<
Scan>();
516 boost::filesystem::path scanFile = scanDataPath / (scanName +
".ply");
517 std::cout <<
timestamp <<
"Loading " << scanFile << std::endl;
520 if(model->m_pointCloud)
522 scan.
points = model->m_pointCloud;
527 <<
"Warning: Loading " << scanFile <<
" failed." << std::endl;
539 const boost::filesystem::path& root,
541 const std::string& positionDirectory,
542 const size_t& scanNumber)
544 std::stringstream scanStr;
545 scanStr << std::setfill(
'0') << std::setw(8) << scanNumber;
547 return loadScan(root, scan, positionDirectory,
"scans", scanStr.str());
551 const boost::filesystem::path& root,
553 const size_t& positionNumber,
554 const size_t& scanNumber)
556 std::stringstream posStr;
557 posStr << std::setfill(
'0') << std::setw(8) << positionNumber;
559 std::stringstream scanStr;
560 scanStr << std::setfill(
'0') << std::setw(8) << scanNumber;
562 return loadScan(root, scan, posStr.str(),
"scans", scanStr.str());
572 boost::filesystem::path root,
573 const std::string positionDirectory,
574 const std::string panoramaDirectory)
576 boost::filesystem::path pos(positionDirectory);
577 boost::filesystem::path pan(panoramaDirectory);
578 return root / pos /
"spectral" /
"data" / pan;
611 const boost::filesystem::path& root,
613 const std::string positionDirectory,
614 const std::string panoramaDirectory,
615 const size_t& channelNumber)
618 std::stringstream metaFileName;
619 metaFileName << std::setfill(
'0') << std::setw(8) << channelNumber <<
".yaml";
621 std::stringstream channelFileName;
622 channelFileName << std::setfill(
'0') << std::setw(8) << channelNumber <<
".png";
624 boost::filesystem::path channelDirectory =
627 if(!boost::filesystem::exists(channelDirectory))
629 std::cout <<
timestamp <<
"Creating: " << channelDirectory << std::endl;
630 boost::filesystem::create_directory(channelDirectory);
633 boost::filesystem::path imagePath = channelDirectory / channelFileName.str();
634 boost::filesystem::path metaPath = channelDirectory / metaFileName.str();
640 std::ofstream out(metaPath.c_str());
643 std::cout <<
timestamp <<
"Writing " << metaPath << std::endl;
649 <<
"Warning: to write " << metaPath << std::endl;
653 std::cout <<
timestamp <<
"Writing " << imagePath << std::endl;
654 cv::imwrite(imagePath.string(), channel.
channel);
658 std::vector<HyperspectralPanoramaChannelPtr>& channels,
659 boost::filesystem::path dataPath)
665 std::stringstream metaStr;
666 metaStr << std::setfill(
'0') << std::setw(8) << c <<
".yaml";
668 std::stringstream pngStr;
669 pngStr << std::setfill(
'0') << std::setw(8) << c <<
".png";
671 boost::filesystem::path metaPath = dataPath / metaStr.str();
672 boost::filesystem::path pngPath = dataPath / pngStr.str();
675 if(boost::filesystem::exists(metaPath)
676 && boost::filesystem::exists(pngPath) )
682 YAML::Node meta = YAML::LoadFile(metaPath.string());
685 if(YAML::convert<HyperspectralPanoramaChannel>::decode(meta, *channel))
690 channel->
channel = cv::imread(pngPath.string());
695 std::cout <<
timestamp <<
"Could not convert " << metaPath << std::endl;
703 <<
timestamp <<
"Read " << c <<
" channels from " << dataPath << std::endl;
715 boost::filesystem::path root,
716 const std::string positionDirectory,
717 const std::string cameraDirectory)
719 boost::filesystem::path pos(positionDirectory);
720 boost::filesystem::path cam(cameraDirectory);
721 return root / pos /cam;
726 const std::string positionDirectory,
727 const std::string& cameraDirectory)
731 if(!boost::filesystem::exists(spectral_pos_path))
733 std::cout <<
timestamp <<
"Creating: " << spectral_pos_path << std::endl;
734 boost::filesystem::create_directory(spectral_pos_path);
737 boost::filesystem::path spectral_data_path = spectral_pos_path /
"data";
738 if(!boost::filesystem::exists(spectral_data_path))
740 std::cout <<
timestamp <<
"Creating: " << spectral_data_path << std::endl;
741 boost::filesystem::create_directory(spectral_data_path);
745 boost::filesystem::path metaPath = spectral_pos_path /
"meta.yaml";
746 if(!boost::filesystem::exists(metaPath))
751 std::ofstream out(metaPath.c_str());
754 std::cout <<
timestamp <<
"Writing " << metaPath << std::endl;
760 <<
"Warning: Unable to write " << metaPath << std::endl;
765 for(
int panorama_id = 0; panorama_id < camera.
panoramas.size(); panorama_id++)
767 char buffer[
sizeof(int) * 5];
768 sprintf(buffer,
"%08d", panorama_id);
769 string nr_str(buffer);
771 boost::filesystem::path panorama_pos_path = spectral_data_path / nr_str;
772 if(!boost::filesystem::exists(panorama_pos_path))
774 std::cout <<
timestamp <<
"Creating: " << panorama_pos_path << std::endl;
775 boost::filesystem::create_directory(panorama_pos_path);
779 for(
int channel_id = 0; channel_id < camera.
panoramas[panorama_id]->channels.size(); channel_id++)
787 const boost::filesystem::path& root,
789 const std::string& positionDirectory)
795 const boost::filesystem::path& root,
797 const size_t& positionNumber)
799 std::stringstream posStr;
800 posStr << std::setfill(
'0') << std::setw(8) << positionNumber;
806 const boost::filesystem::path& root,
808 const std::string& positionDirectory,
809 const std::string& cameraDirectory)
811 boost::filesystem::path cameraPath =
817 boost::filesystem::path metaPath = cameraPath /
"meta.yaml";
820 std::cout <<
timestamp <<
"Loading " << metaPath << std::endl;
821 YAML::Node meta = YAML::LoadFile(metaPath.string());
827 boost::filesystem::path dataPath = cameraPath /
"data";
833 std::stringstream metaStr;
834 metaStr << std::setfill(
'0') << std::setw(8) << c;
836 boost::filesystem::path channelPath = dataPath / metaStr.str();
838 if(boost::filesystem::exists(channelPath))
840 std::vector<HyperspectralPanoramaChannelPtr> channels;
844 panorama->channels = channels;
852 <<
timestamp <<
"Read " << c <<
" panoramas from " << cameraPath << std::endl;
866 const boost::filesystem::path& root,
868 const std::string& positionDirectory)
874 const boost::filesystem::path& root,
876 const size_t& positionNumber)
878 std::stringstream posStr;
879 posStr << std::setfill(
'0') << std::setw(8) << positionNumber;
889 const boost::filesystem::path& root,
891 const std::string positionDirectory)
893 boost::filesystem::path scan_pos_path = root / positionDirectory;
895 if(!boost::filesystem::exists(root))
897 std::cout <<
timestamp <<
"Creating: " << root << std::endl;
898 boost::filesystem::create_directory(root);
901 if(!boost::filesystem::exists(scan_pos_path))
903 std::cout <<
timestamp <<
"Creating: " << scan_pos_path << std::endl;
904 boost::filesystem::create_directory(scan_pos_path);
908 boost::filesystem::path scanPosMetaOut = scan_pos_path /
"meta.yaml";
912 std::ofstream out(scanPosMetaOut.string());
915 std::cout <<
timestamp <<
"Writing " << scanPosMetaOut << std::endl;
921 <<
"Warning: Unable to write " << scanPosMetaOut << std::endl;
924 for(
size_t scan_id = 0; scan_id < scanPos.
scans.size(); scan_id++)
926 saveScan(root, *scanPos.
scans[scan_id], positionDirectory,
"scans", scan_id);
929 for(
size_t cam_id = 0; cam_id < scanPos.
cams.size(); cam_id++)
941 const boost::filesystem::path& root,
943 const size_t& positionNumber)
945 std::stringstream posStr;
946 posStr << std::setfill(
'0') << std::setw(8) << positionNumber;
952 const boost::filesystem::path& root,
954 const std::string& positionDirectory)
957 boost::filesystem::path scanPosDir = root / positionDirectory;
959 if(!boost::filesystem::exists(scanPosDir))
961 std::cerr <<
timestamp <<
"Could not open " << scanPosDir << std::endl;
965 boost::filesystem::path scanPosMetaPath = scanPosDir /
"meta.yaml";
966 if(!boost::filesystem::is_regular_file(scanPosMetaPath))
968 std::cerr <<
timestamp <<
"Could not open " << scanPosMetaPath << std::endl;
972 YAML::Node meta = YAML::LoadFile(scanPosMetaPath.string());
977 boost::filesystem::directory_iterator it{scanPosDir};
978 while (it != boost::filesystem::directory_iterator{})
984 boost::filesystem::path scanDataDir = it->path() /
"data";
986 boost::filesystem::directory_iterator itScans{scanDataDir};
987 while (itScans != boost::filesystem::directory_iterator{})
989 if(itScans->path().extension() ==
".yaml")
991 std::string scanName = itScans->path().stem().string();
994 if(
loadScan(root, *scan, positionDirectory, it->path().stem().string(), scanName))
996 scanPos.
scans.push_back(scan);
1006 if(
loadScanCamera(root, *cam, positionDirectory, it->path().stem().string()))
1008 scanPos.
cams.push_back(cam);
1010 std::cout <<
timestamp <<
"[ WARNING ] Could not load camera from " << it->path() << std::endl;
1021 std::cout <<
timestamp <<
"[ WARNING ] Could not load hyperspectral camera from " << it->path() << std::endl;
1033 const boost::filesystem::path& root,
1035 const size_t& positionNumber)
1037 std::stringstream posStr;
1038 posStr << std::setfill(
'0') << std::setw(8) << positionNumber;
1047 const boost::filesystem::path& root,
1051 if(!boost::filesystem::exists(root))
1053 boost::filesystem::create_directory(root);
1056 boost::filesystem::path scanProjMetaPath = root /
"meta.yaml";
1061 std::ofstream out(scanProjMetaPath.string());
1064 std::cout <<
timestamp <<
"Writing " << scanProjMetaPath << std::endl;
1070 <<
"Warning: Unable to write " << scanProjMetaPath << std::endl;
1075 for(
size_t i=0; i<scanProj.
positions.size(); i++)
1084 const boost::filesystem::path& root,
1087 if(!boost::filesystem::exists(root))
1089 std::cerr <<
timestamp <<
"Could not open " << root << std::endl;
1093 boost::filesystem::path scanProjMetaPath = root /
"meta.yaml";
1095 if(!boost::filesystem::is_regular_file(scanProjMetaPath))
1097 std::cerr <<
timestamp <<
"Could not load " << scanProjMetaPath << std::endl;
1101 YAML::Node meta = YAML::LoadFile(scanProjMetaPath.string());
1104 std::vector<boost::filesystem::path> paths;
1106 std::copy(boost::filesystem::directory_iterator(root), boost::filesystem::directory_iterator(), back_inserter(paths));
1107 std::sort(paths.begin(), paths.end());
1109 for (std::vector<boost::filesystem::path>::const_iterator it(paths.begin()), it_end(paths.end()); it != it_end; ++it)
1113 std::cout << *it <<
'\n';