DGtalTools  0.9.2
sdp2vol.cpp
1 
29 #include <iostream>
31 #include <fstream>
32 #include "DGtal/base/Common.h"
33 #include "DGtal/helpers/StdDefs.h"
34 #include "DGtal/images/ImageContainerBySTLVector.h"
35 #include "DGtal/io/writers/GenericWriter.h"
36 
37 #include <boost/program_options/options_description.hpp>
38 #include <boost/program_options/parsers.hpp>
39 #include <boost/program_options/variables_map.hpp>
40 
41 using namespace std;
42 using namespace DGtal;
43 
77 namespace po = boost::program_options;
79 
80 int main( int argc, char** argv )
81 {
82  typedef ImageContainerBySTLVector < Z3i::Domain, unsigned char > Image3D;
83 
84  // parse command line ----------------------------------------------
85  po::options_description general_opt("Allowed options are: ");
86  general_opt.add_options()
87  ("help,h", "display this message")
88  ("input,i", po::value<std::string>(), "Sequence of 3d Discrete points (.sdp) " )
89  ("output,o", po::value<std::string>(), "Vol file (.vol, .longvol, .pgm3d) " )
90  ("foregroundVal,f", po::value<int>()->default_value(128), "value which will represent the foreground object in the resulting image (default 128)")
91  ("invertY", "Invert the Y axis (image flip in the y direction)")
92  ("backgroundVal,b", po::value<int>()->default_value(0), "value which will represent the background outside the object in the resulting image (default 0)")
93  ("domain,d", po::value<std::vector <int> >()->multitoken(), "The domain of the resulting image xmin ymin zmin xmax ymax zmax ");
94 
95  bool parseOK=true;
96  po::variables_map vm;
97  try{
98  po::store(po::parse_command_line(argc, argv, general_opt), vm);
99  }catch(const std::exception& ex){
100  parseOK=false;
101  trace.info()<< "Error checking program options: "<< ex.what()<< endl;
102  }
103  po::notify(vm);
104  if( !parseOK || vm.count("help"))
105  {
106  std::cout << "Convert digital set of points into a volumic file.\n";
107  std::cout << "Usage: " << argv[0] << " [input] [output]\n"
108  << general_opt << "\n";
109  std::cout << "Example:\n"
110  << "sdp2vol -i volumePoints.sdp -o volume.vol -d 0 0 0 10 10 10 \n";
111  return 0;
112  }
113  if(! vm.count("input") ||! vm.count("output") || !vm.count("domain") )
114  {
115  trace.error() << " Input/ output filename and domain are needed to be defined" << endl;
116  return 0;
117  }
118 
119  std::vector<int> domainCoords= vm["domain"].as<std::vector <int> >();
120  Z3i::Point ptLower(domainCoords[0],domainCoords[1], domainCoords[2]);
121  Z3i::Point ptUpper(domainCoords[3],domainCoords[4], domainCoords[5]);
122  Image3D::Domain imageDomain(ptLower, ptUpper);
123 
124  string inputSDP = vm["input"].as<std::string>();
125  string outputFilename = vm["output"].as<std::string>();
126  int foregroundVal = vm["foregroundVal"].as<int>();
127  int backgroundVal = vm["backgroundVal"].as<int>();
128 
129 
130  vector<unsigned int> vPos;
131  vPos.push_back(0);
132  vPos.push_back(1);
133  vPos.push_back(2);
134  trace.info() << "Reading input SDP file: " << inputSDP ;
135  std::vector<Z3i::Point> vectPoints= PointListReader<Z3i::Point>::getPointsFromFile(inputSDP, vPos);
136  trace.info() << " [done] " << std::endl ;
137 
138  Image3D imageResult(imageDomain);
139  for(Image3D::Domain::ConstIterator iter = imageResult.domain().begin(); iter!= imageResult.domain().end();
140  iter++){
141  imageResult.setValue(*iter, backgroundVal);
142  }
143 
144  for(unsigned int i=0; i<vectPoints.size(); i++){
145  if(vm.count("invertY")){
146  vectPoints[i][1]=ptUpper[1]-vectPoints[i][1];
147  }
148  if(imageResult.domain().isInside(vectPoints[i])){
149  imageResult.setValue(vectPoints[i], foregroundVal);
150  }else{
151  trace.warning() << "point " << vectPoints[i] << " outside the domain (ignored in the resulting volumic image)" << std::endl;
152  }
153  }
154  trace.info()<< "Exporting resulting volumic image ... ";
155  GenericWriter<Image3D>::exportFile(outputFilename, imageResult);
156  trace.info() << " [done]"<<std::endl;
157  return 0;
158 
159 }
160 
161 
162 
163 
STL namespace.