DGtalTools  0.9.2
3dVolViewer.cpp
1 
29 #include <iostream>
31 
32 #include "DGtal/base/Common.h"
33 #include "DGtal/base/BasicFunctors.h"
34 #include "DGtal/helpers/StdDefs.h"
35 #include "DGtal/io/readers/GenericReader.h"
36 #ifdef WITH_ITK
37 #include "DGtal/io/readers/DicomReader.h"
38 #endif
39 #include "DGtal/io/viewers/Viewer3D.h"
40 #include "DGtal/io/DrawWithDisplay3DModifier.h"
41 #include "DGtal/io/readers/PointListReader.h"
42 
43 #include "DGtal/io/Color.h"
44 #include "DGtal/io/colormaps/GradientColorMap.h"
45 #include "DGtal/images/ImageSelector.h"
46 
47 #include <boost/program_options/options_description.hpp>
48 #include <boost/program_options/parsers.hpp>
49 #include <boost/program_options/variables_map.hpp>
50 
51 using namespace std;
52 using namespace DGtal;
53 using namespace Z3i;
54 
55 
104 namespace po = boost::program_options;
106 
107 int main( int argc, char** argv )
108 {
109  // parse command line ----------------------------------------------
110  po::options_description general_opt("Allowed options are: ");
111  general_opt.add_options()
112  ("help,h", "display this message")
113  ("input,i", po::value<std::string>(), "vol file (.vol) , pgm3d (.p3d or .pgm3d, pgm (with 3 dims)) file or sdp (sequence of discrete points)" )
114  ("thresholdMin,m", po::value<int>()->default_value(0), "threshold min to define binary shape" )
115  ("thresholdMax,M", po::value<int>()->default_value(255), "threshold max to define binary shape" )
116  ("numMaxVoxel,n", po::value<int>()->default_value(500000), "set the maximal voxel number to be displayed." )
117 #ifdef WITH_ITK
118  ("dicomMin", po::value<int>()->default_value(-1000), "set minimum density threshold on Hounsfield scale")
119  ("dicomMax", po::value<int>()->default_value(3000), "set maximum density threshold on Hounsfield scale")
120 #endif
121  ("transparency,t", po::value<uint>()->default_value(255), "transparency") ;
122 
123  bool parseOK=true;
124  po::variables_map vm;
125  try{
126  po::store(po::parse_command_line(argc, argv, general_opt), vm);
127  }catch(const std::exception& ex){
128  parseOK=false;
129  trace.info()<< "Error checking program options: "<< ex.what()<< endl;
130  }
131  po::notify(vm);
132  if( !parseOK || vm.count("help")||argc<=1)
133  {
134  std::cout << "Usage: " << argv[0] << " [input]\n"
135  << "Display volume file as a voxel set by using QGLviewer"<< endl
136  << general_opt << "\n"
137  << "Example: "<< std::endl
138  << " \t 3dVolViewer -i $DGtal/examples/samples/lobster.vol -m 60 -t 10" << endl;
139  return 0;
140  }
141 
142  if(! vm.count("input"))
143  {
144  trace.error() << " The file name was defined" << endl;
145  return 0;
146  }
147  string inputFilename = vm["input"].as<std::string>();
148  int thresholdMin = vm["thresholdMin"].as<int>();
149  int thresholdMax = vm["thresholdMax"].as<int>();
150  unsigned char transp = vm["transparency"].as<uint>();
151 
152  bool limitDisplay=false;
153  if(vm.count("numMaxVoxel")){
154  limitDisplay=true;
155  }
156  unsigned int numDisplayedMax = vm["numMaxVoxel"].as<int>();
157 
158 
159  QApplication application(argc,argv);
160  Viewer3D<> viewer;
161  viewer.setWindowTitle("simple Volume Viewer");
162  viewer.show();
163 
164  typedef ImageSelector<Domain, unsigned char>::Type Image;
165  string extension = inputFilename.substr(inputFilename.find_last_of(".") + 1);
166  if(extension!="vol" && extension != "p3d" && extension != "pgm3D" && extension != "pgm3d" && extension != "sdp" && extension != "pgm"
167  #ifdef WITH_ITK
168  && extension !="dcm"
169 #endif
170 ){
171  trace.info() << "File extension not recognized: "<< extension << std::endl;
172  return 0;
173  }
174 
175  if(extension=="vol" || extension=="pgm3d" || extension=="pgm3D"
176 #ifdef WITH_ITK
177  || extension =="dcm"
178 #endif
179 ){
180  unsigned int numDisplayed=0;
181 
182 #ifdef WITH_ITK
183  int dicomMin = vm["dicomMin"].as<int>();
184  int dicomMax = vm["dicomMax"].as<int>();
185  typedef DGtal::functors::Rescaling<int ,unsigned char > RescalFCT;
186  Image image = extension == "dcm" ? DicomReader< Image, RescalFCT >::importDicom( inputFilename,
187  RescalFCT(dicomMin,
188  dicomMax,
189  0, 255) ) :
190  GenericReader<Image>::import( inputFilename );
191 #else
192  Image image = GenericReader<Image>::import (inputFilename );
193 #endif
194 
195  trace.info() << "Image loaded: "<<image<< std::endl;
196  Domain domain = image.domain();
197  GradientColorMap<long> gradient( thresholdMin, thresholdMax);
198  gradient.addColor(Color::Blue);
199  gradient.addColor(Color::Green);
200  gradient.addColor(Color::Yellow);
201  gradient.addColor(Color::Red);
202  for(Domain::ConstIterator it = domain.begin(), itend=domain.end(); it!=itend; ++it){
203  unsigned char val= image( (*it) );
204  if(limitDisplay && numDisplayed > numDisplayedMax)
205  break;
206  Color c= gradient(val);
207  if(val<=thresholdMax && val >=thresholdMin){
208  viewer << CustomColors3D(Color((float)(c.red()), (float)(c.green()),(float)(c.blue()), transp),
209  Color((float)(c.red()), (float)(c.green()),(float)(c.blue()), transp));
210  viewer << *it;
211  numDisplayed++;
212  }
213  }
214  }else if(extension=="sdp"){
215  vector<Z3i::RealPoint> vectVoxels = PointListReader<Z3i::RealPoint>::getPointsFromFile(inputFilename);
216  for(unsigned int i=0;i< vectVoxels.size(); i++){
217  viewer << vectVoxels.at(i);
218  }
219  }
220  viewer << Viewer3D<>::updateDisplay;
221  return application.exec();
222 }
STL namespace.