DGtalTools  1.2.0
itk2vol.cpp
1 
30 #include <iostream>
31 #include <fstream>
32 #include "DGtal/base/Common.h"
33 #include "DGtal/helpers/StdDefs.h"
34 #include "DGtal/base/BasicFunctors.h"
35 #include "DGtal/images/ImageContainerBySTLVector.h"
36 #include "DGtal/io/writers/GenericWriter.h"
37 #include "DGtal/io/readers/ITKReader.h"
38 
39 #include "CLI11.hpp"
40 
41 using namespace std;
42 using namespace DGtal;
43 
76 typedef ImageContainerBySTLVector < Z3i::Domain, unsigned char > Image3DChar;
77 
78 
79 template<typename TImage, typename TImageMask>
80 void
81 applyMaskImage( TImage &imageInput, const TImageMask &maskImage,
82  typename TImageMask::Value valRemove)
83 {
84  for(const auto &p : imageInput.domain())
85  {
86  if (maskImage(p) == valRemove)
87  {
88  imageInput.setValue(p,0);
89  }
90  }
91 
92 }
93 
94 template<typename TImage, typename TRescale>
95 void
96 exportImageUCHAR(TImage img, const std::string resName, TRescale rScale)
97 {
98  Image3DChar res ( img.domain() );
99 
100  for (const auto p: img.domain() ) {
101  res.setValue(p, rScale(img(p)));
102  }
103  DGtal::GenericWriter<Image3DChar, 3, unsigned char>::exportFile(resName, res);
104 }
105 
106 
107 
108 int main( int argc, char** argv )
109 {
110  typedef ImageContainerBySTLVector < Z3i::Domain, double > Image3D_D;
111  typedef ImageContainerBySTLVector < Z3i::Domain, int > Image3D_I;
112 
113 
114  // parse command line using CLI ----------------------------------------------
115  CLI::App app;
116  std::string inputFileName;
117  std::string outputFileName {"result.vol"};
118  DGtal::int64_t inputMin {-1000};
119  DGtal::int64_t inputMax {3000};
120  int maskRemoveLabel {0};
121  string inputMask {""};
122  string inputType {""};
123 
124  app.description("Converts itk file into a volumetric file (.vol, .pgm3d). \n Example:\n itk2vol image.mhd sample.vol --inputMin -500 --inputMax -100 \n");
125  app.add_option("-i,--input,1", inputFileName, "Any file format in the ITK library (mhd, mha, ...)." )
126  ->required()
127  ->check(CLI::ExistingFile);
128  app.add_option("-o,--output,2", outputFileName, "volumetric file (.vol, .pgm3d).");
129  app.add_option("-m,--maskImage", inputMask, "Use a mask image to remove image part (where mask is 0). The default mask value can be changed using mask default value.");
130  app.add_option("-r,--maskRemoveLabel", maskRemoveLabel,"Change the label value that defines the part of input image to be removed by the option --maskImage." );
131  app.add_option("--inputMin", inputMin, "set minimum density threshold on Hounsfield scale.");
132  app.add_option("--inputMax", inputMax, "set maximum density threshold on Hounsfield scale.");
133  app.add_option("-t,--inputType", inputType, "to specify the input image type (int or double).")
134  -> check(CLI::IsMember({"int", "double"}));
135 
136  app.get_formatter()->column_width(40);
137  CLI11_PARSE(app, argc, argv);
138  // END parse command line using CLI ----------------------------------------------
139 
140 
141  if (inputType == "double") {
142  typedef DGtal::functors::Rescaling<double ,unsigned char > RescalFCT;
143  trace.info() << "Reading input file (of type double)" << inputFileName ;
144  Image3D_D inputImage = ITKReader< Image3D_D >::importITK(inputFileName, true);
145  trace.info() << " [done] " << std::endl ;
146  trace.info() << " converting into vol file... " ;
147  if ( inputMask != "")
148  {
149  Image3D_I maskImage = ITKReader< Image3D_I >::importITK(inputMask);
150  applyMaskImage(inputImage, maskImage, maskRemoveLabel);
151  }
152  RescalFCT rescaleCustom(inputMin, inputMax, 0, 255);
153  exportImageUCHAR(inputImage,outputFileName, rescaleCustom );
154  }else {
155  typedef DGtal::functors::Rescaling<int ,unsigned char > RescalFCT;
156  trace.info() << "Reading input file (of type int) " << inputFileName ;
157  Image3D_I inputImage = ITKReader< Image3D_I >::importITK(inputFileName, true);
158  trace.info() << " [done] " << std::endl ;
159  trace.info() << " converting into vol file... " ;
160  RescalFCT rescaleCustom(inputMin, inputMax, 0, 255);
161  exportImageUCHAR(inputImage,outputFileName, rescaleCustom );
162 
163  }
164  trace.info() << " [done] " << std::endl ;
165  return EXIT_SUCCESS;
166 }
Definition: ATu0v1.h:57