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"
42 using namespace DGtal;
76 typedef ImageContainerBySTLVector < Z3i::Domain, unsigned char > Image3DChar;
79 template<
typename TImage,
typename TImageMask>
81 applyMaskImage( TImage &imageInput,
const TImageMask &maskImage,
82 typename TImageMask::Value valRemove)
84 for(
const auto &p : imageInput.domain())
86 if (maskImage(p) == valRemove)
88 imageInput.setValue(p,0);
94 template<
typename TImage,
typename TRescale>
96 exportImageUCHAR(TImage img,
const std::string resName, TRescale rScale)
98 Image3DChar res ( img.domain() );
100 for (
const auto p: img.domain() ) {
101 res.setValue(p, rScale(img(p)));
103 DGtal::GenericWriter<Image3DChar, 3, unsigned char>::exportFile(resName, res);
108 int main(
int argc,
char** argv )
110 typedef ImageContainerBySTLVector < Z3i::Domain, double > Image3D_D;
111 typedef ImageContainerBySTLVector < Z3i::Domain, int > Image3D_I;
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 {
""};
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, ...)." )
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"}));
136 app.get_formatter()->column_width(40);
137 CLI11_PARSE(app, argc, argv);
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 !=
"")
149 Image3D_I maskImage = ITKReader< Image3D_I >::importITK(inputMask);
150 applyMaskImage(inputImage, maskImage, maskRemoveLabel);
152 RescalFCT rescaleCustom(inputMin, inputMax, 0, 255);
153 exportImageUCHAR(inputImage,outputFileName, rescaleCustom );
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 );
164 trace.info() <<
" [done] " << std::endl ;