DGtalTools 2.0.0
Loading...
Searching...
No Matches
vol2slice.cpp
1
30#include <iostream>
31#include "DGtal/base/Common.h"
32#include "DGtal/helpers/StdDefs.h"
33#include <boost/format.hpp>
34
35#include "DGtal/io/readers/GenericReader.h"
36#include "DGtal/io/writers/GenericWriter.h"
37#include "DGtal/images/ConstImageAdapter.h"
38#include "DGtal/kernel/BasicPointFunctors.h"
39
40#include "CLI11.hpp"
41
42using namespace std;
43using namespace DGtal;
44
45
47
96int main( int argc, char** argv )
97{
98 typedef ImageContainerBySTLVector < Z3i::Domain, unsigned char > Image3D;
99 typedef ImageContainerBySTLVector < Z2i::Domain, unsigned char > Image2D;
100 typedef DGtal::ConstImageAdapter<Image3D, Image2D::Domain, DGtal::functors::Projector< DGtal::Z3i::Space>,
101 Image3D::Value, DGtal::functors::Identity > SliceImageAdapter;
102
103 CLI::App app;
104
105
106 std::string inputFileName;
107 std::string outputFileName = "result.pgm";
108 DGtal::int64_t rescaleInputMin {0};
109 DGtal::int64_t rescaleInputMax {255};
110 int userStartSlice {0};
111 int userEndSlice {0};
112 unsigned int sliceOrientation {2};
113
114
115 app.description("Convert a volumetric file (.vol, .longvol, .pgm3d) into a set of 2D slice images. \n Typical use: to extract all slices defined in Y plane (y=cst): \n vol2slice image3d.vol slice.pgm -s 1 \n");
116
117 app.add_option("-i,--input,1", inputFileName, "vol file (.vol, .longvol .p3d, .pgm3d and if DGTAL_WITH_ITK is selected: dicom, dcm, mha, mhd). For longvol, dicom, dcm, mha or mhd formats, the input values are linearly scaled between 0 and 255.")
118 ->required()
119 ->check(CLI::ExistingFile);
120
121 app.add_option("--output,-o,2",outputFileName ,"base_name.extension: extracted 2D slice volumetric files (will result n files base_name_xxx.extension)");
122 app.add_option("--setFirstSlice,-f", userStartSlice, "Set the first slice index to be extracted.")
123 -> check(CLI::Number);
124 app.add_option("--setLastSlice,-l", userEndSlice, "Set the last slice index to be extracted (by default set to maximal value according to the given volume).")
125 -> check(CLI::Number);
126 app.add_option("--sliceOrientation,-s", sliceOrientation, "specify the slice orientation for which the slice are defined (by default =2 (Z direction))")
127 -> check(CLI::IsMember({0, 1, 2}));
128 app.add_option("--rescaleInputMin", rescaleInputMin, "min value used to rescale the input intensity (to avoid basic cast into 8 bits image).");
129 app.add_option("--rescaleInputMax", rescaleInputMax, "max value used to rescale the input intensity (to avoid basic cast into 8 bits image).");
130 app.get_formatter()->column_width(40);
131
132 CLI11_PARSE(app, argc, argv);
133
134 std::string outputExt = outputFileName.substr(outputFileName.find_last_of(".")+1);
135 std::string outputBasename = outputFileName.substr(0, outputFileName.find_last_of("."));
136
137
138 trace.info()<< "Importing volume file base name: " << outputBasename << " extension: " << outputExt << " ..." ;
139 typedef DGtal::functors::Rescaling<DGtal::int64_t ,unsigned char > RescalFCT;
140 Image3D input3dImage = GenericReader< Image3D >::importWithValueFunctor( inputFileName,RescalFCT(rescaleInputMin,
141 rescaleInputMax,
142 0, 255) );
143
144 trace.info()<< "[done]" << endl;
145
146 unsigned int startSlice=0;
147 unsigned int endSlice=input3dImage.domain().upperBound()[sliceOrientation];
148
149 if(userStartSlice !=0){
150 startSlice = userStartSlice;
151 }
152 if(userEndSlice != 0){
153 endSlice = userEndSlice;
154 }
155
156 //Processing each slice
157#pragma omp parallel for schedule(dynamic)
158 for( int i = startSlice; i <= endSlice; i++){
159 trace.info() << "Exporting slice image "<< i ;
160 DGtal::functors::Projector<DGtal::Z2i::Space> invFunctor; invFunctor.initRemoveOneDim(sliceOrientation);
161 DGtal::Z2i::Domain domain2D(invFunctor(input3dImage.domain().lowerBound()),
162 invFunctor(input3dImage.domain().upperBound()));
163 DGtal::functors::Projector<DGtal::Z3i::Space> aSliceFunctor(i); aSliceFunctor.initAddOneDim(sliceOrientation);
164 const DGtal::functors::Identity identityFunctor{};
165 SliceImageAdapter sliceImage( input3dImage, domain2D, aSliceFunctor, identityFunctor );
166 stringstream outName; outName << outputBasename << "_" << boost::format("%|05|")% i <<"."<< outputExt ;
167 trace.info() << ": "<< outName.str() ;
168 GenericWriter<SliceImageAdapter>::exportFile(outName.str(), sliceImage);
169 trace.info() << " [done]"<< endl;
170 }
171
172
173 return 0;
174}
Definition ATu0v1.h:57