DGtalTools 2.1.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
97int main( int argc, char** argv )
98{
99 typedef ImageContainerBySTLVector < Z3i::Domain, unsigned char > Image3D;
100 typedef ImageContainerBySTLVector < Z2i::Domain, unsigned char > Image2D;
101 typedef DGtal::ConstImageAdapter<Image3D, Image2D::Domain, DGtal::functors::Projector< DGtal::Z3i::Space>,
102 Image3D::Value, DGtal::functors::Identity > SliceImageAdapter;
103
104 CLI::App app;
105
106
107 std::string inputFileName;
108 std::string outputFileName = "result.pgm";
109 DGtal::int64_t rescaleInputMin {0};
110 DGtal::int64_t rescaleInputMax {255};
111 int userStartSlice {0};
112 int userEndSlice {0};
113 unsigned int sliceOrientation {2};
114
115
116 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");
117
118 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.")
119 ->required()
120 ->check(CLI::ExistingFile);
121
122 app.add_option("--output,-o,2",outputFileName ,"base_name.extension: extracted 2D slice volumetric files (will result n files base_name_xxx.extension)");
123 app.add_option("--setFirstSlice,-f", userStartSlice, "Set the first slice index to be extracted.")
124 -> check(CLI::Number);
125 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).")
126 -> check(CLI::Number);
127 app.add_option("--sliceOrientation,-s", sliceOrientation, "specify the slice orientation for which the slice are defined (by default =2 (Z direction))")
128 -> check(CLI::IsMember({0, 1, 2}));
129 app.add_option("--rescaleInputMin", rescaleInputMin, "min value used to rescale the input intensity (to avoid basic cast into 8 bits image).");
130 app.add_option("--rescaleInputMax", rescaleInputMax, "max value used to rescale the input intensity (to avoid basic cast into 8 bits image).");
131 app.get_formatter()->column_width(40);
132
133 CLI11_PARSE(app, argc, argv);
134
135 std::string outputExt = outputFileName.substr(outputFileName.find_last_of(".")+1);
136 std::string outputBasename = outputFileName.substr(0, outputFileName.find_last_of("."));
137
138
139 trace.info()<< "Importing volume file base name: " << outputBasename << " extension: " << outputExt << " ..." ;
140 typedef DGtal::functors::Rescaling<DGtal::int64_t ,unsigned char > RescalFCT;
141 Image3D input3dImage = GenericReader< Image3D >::importWithValueFunctor( inputFileName,RescalFCT(rescaleInputMin,
142 rescaleInputMax,
143 0, 255) );
144
145 trace.info()<< "[done]" << endl;
146
147 unsigned int startSlice=0;
148 unsigned int endSlice=input3dImage.domain().upperBound()[sliceOrientation];
149
150 if(userStartSlice !=0){
151 startSlice = userStartSlice;
152 }
153 if(userEndSlice != 0){
154 endSlice = userEndSlice;
155 }
156
157 //Processing each slice
158#pragma omp parallel for schedule(dynamic)
159 for( int i = startSlice; i <= endSlice; i++){
160 trace.info() << "Exporting slice image "<< i ;
161 DGtal::functors::Projector<DGtal::Z2i::Space> invFunctor; invFunctor.initRemoveOneDim(sliceOrientation);
162 DGtal::Z2i::Domain domain2D(invFunctor(input3dImage.domain().lowerBound()),
163 invFunctor(input3dImage.domain().upperBound()));
164 DGtal::functors::Projector<DGtal::Z3i::Space> aSliceFunctor(i); aSliceFunctor.initAddOneDim(sliceOrientation);
165 const DGtal::functors::Identity identityFunctor{};
166 SliceImageAdapter sliceImage( input3dImage, domain2D, aSliceFunctor, identityFunctor );
167 stringstream outName; outName << outputBasename << "_" << boost::format("%|05|")% i <<"."<< outputExt ;
168 trace.info() << ": "<< outName.str() ;
169 GenericWriter<SliceImageAdapter>::exportFile(outName.str(), sliceImage);
170 trace.info() << " [done]"<< endl;
171 }
172
173
174 return 0;
175}
Definition ATu0v1.h:57