2 #include <itkImageRegionIterator.h> 3 #include <itkMultiplyImageFilter.h> 4 #include <itkDivideImageFilter.h> 5 #include <itkMaskImageFilter.h> 6 #include <itkCastImageFilter.h> 8 #include <tclap/CmdLine.h> 10 int main(
int argc,
char **argv)
12 TCLAP::CmdLine cmd(
"INRIA / IRISA - VisAGeS/Empenn Team",
' ',ANIMA_VERSION);
14 TCLAP::ValueArg<std::string> inArg(
"i",
"inputfiles",
"Input image list in text file",
true,
"",
"input image list",cmd);
15 TCLAP::ValueArg<std::string> maskArg(
"m",
"maskfiles",
"Input masks list in text file (mask images should contain only zeros or ones)",
false,
"",
"input masks list",cmd);
16 TCLAP::ValueArg<std::string> weightsArg(
"w",
"weights",
"Weights list in text file",
false,
"",
"input weights list",cmd);
17 TCLAP::ValueArg<std::string> outArg(
"o",
"outputfile",
"Output image",
true,
"",
"output image",cmd);
23 catch (TCLAP::ArgException& e)
25 std::cerr <<
"Error: " << e.error() <<
"for argument " << e.argId() << std::endl;
29 std::ifstream imageIn(inArg.getValue());
32 imageIn.getline(refN,2048);
33 while ((strcmp(refN,
"") == 0)&&(!imageIn.eof()))
34 imageIn.getline(refN,2048);
36 itk::ImageIOBase::Pointer imageIO = itk::ImageIOFactory::CreateImageIO(refN, itk::ImageIOFactory::ReadMode);
40 std::cerr <<
"Unable to read input image " << inArg.getValue() << std::endl;
44 imageIO->SetFileName(refN);
45 imageIO->ReadImageInformation();
49 imageIn.seekg(0, std::ios::beg);
51 bool vectorImage = (imageIO->GetNumberOfComponents() > 1);
53 using ImageType = itk::Image <long double, 3>;
54 using SaveImageType = itk::Image <double, 3>;
55 using VectorImageType = itk::VectorImage <long double, 3>;
56 using SaveVectorImageType = itk::VectorImage <double, 3>;
57 typedef itk::MultiplyImageFilter <ImageType, ImageType, ImageType> MultiplyFilterType;
58 typedef itk::DivideImageFilter <ImageType, ImageType, ImageType> DivideFilterType;
59 typedef itk::MultiplyImageFilter <VectorImageType, ImageType, VectorImageType> MultiplyVectorFilterType;
60 typedef itk::DivideImageFilter <VectorImageType, ImageType, VectorImageType> DivideVectorFilterType;
62 using CastFilterType = itk::CastImageFilter <ImageType, SaveImageType>;
63 using CastVectorFilterType = itk::CastImageFilter <VectorImageType, SaveVectorImageType>;
65 std::ifstream masksIn;
66 if (maskArg.getValue() !=
"")
67 masksIn.open(maskArg.getValue());
69 std::ifstream weightsIn;
70 if (weightsArg.getValue() !=
"")
71 weightsIn.open(weightsArg.getValue());
73 double sumWeights = 0;
78 ImageType::Pointer tmpOutput;
79 ImageType::Pointer tmpSumMasks;
81 while (tmpOutput.IsNull())
83 imageIn.getline(refN,2048);
85 if (masksIn.is_open())
86 masksIn.getline(maskN,2048);
87 if (strcmp(refN,
"") == 0)
90 tmpOutput = anima::readImage <ImageType> (refN);
91 double imageWeight = 1.0;
92 if (weightsIn.is_open())
93 weightsIn >> imageWeight;
95 std::cout <<
"Adding image " << refN <<
" with weight " << imageWeight <<
"..." << std::endl;
97 sumWeights += imageWeight;
99 if (imageWeight != 1.0)
101 MultiplyFilterType::Pointer multiplyFilter = MultiplyFilterType::New();
102 multiplyFilter->SetInput1(tmpOutput);
103 multiplyFilter->SetConstant(imageWeight);
104 multiplyFilter->Update();
106 tmpOutput = multiplyFilter->GetOutput();
107 tmpOutput->DisconnectPipeline();
110 if (masksIn.is_open())
112 tmpSumMasks = anima::readImage <ImageType> (maskN);
114 MultiplyFilterType::Pointer maskFilter = MultiplyFilterType::New();
115 maskFilter->SetInput1(tmpOutput);
116 maskFilter->SetInput2(tmpSumMasks);
117 maskFilter->Update();
119 tmpOutput = maskFilter->GetOutput();
120 tmpOutput->DisconnectPipeline();
122 if (imageWeight != 1.0)
124 MultiplyFilterType::Pointer multiplyMaskFilter = MultiplyFilterType::New();
125 multiplyMaskFilter->SetInput1(tmpSumMasks);
126 multiplyMaskFilter->SetConstant(imageWeight);
127 multiplyMaskFilter->Update();
129 tmpSumMasks = multiplyMaskFilter->GetOutput();
130 tmpSumMasks->DisconnectPipeline();
135 while(!imageIn.eof())
137 imageIn.getline(refN,2048);
139 if (masksIn.is_open())
140 masksIn.getline(maskN,2048);
141 if (strcmp(refN,
"") == 0)
144 double imageWeight = 1.0;
145 if (weightsIn.is_open())
146 weightsIn >> imageWeight;
148 sumWeights += imageWeight;
150 std::cout <<
"Adding image " << refN <<
" with weight " << imageWeight <<
"..." << std::endl;
152 ImageType::Pointer tmpImage = anima::readImage <ImageType> (refN);
153 itk::ImageRegionIterator <ImageType> tmpIt(tmpImage,tmpImage->GetLargestPossibleRegion());
154 itk::ImageRegionIterator <ImageType> resIt(tmpOutput,tmpImage->GetLargestPossibleRegion());
156 if (masksIn.is_open())
158 ImageType::Pointer tmpMask = anima::readImage <ImageType> (maskN);
160 itk::ImageRegionIterator <ImageType> tmpMaskIt(tmpMask,tmpImage->GetLargestPossibleRegion());
161 itk::ImageRegionIterator <ImageType> sumMasksIt(tmpSumMasks,tmpImage->GetLargestPossibleRegion());
163 while (!sumMasksIt.IsAtEnd())
165 if (tmpMaskIt.Get() == 0)
174 sumMasksIt.Set(sumMasksIt.Get() + imageWeight * tmpMaskIt.Get());
175 resIt.Set(resIt.Get() + imageWeight * tmpIt.Get());
185 while (!resIt.IsAtEnd())
187 resIt.Set(resIt.Get() + imageWeight * tmpIt.Get());
194 if (masksIn.is_open())
197 if (weightsIn.is_open())
200 if (tmpSumMasks.IsNotNull())
202 DivideFilterType::Pointer divideFilter = DivideFilterType::New();
203 divideFilter->SetInput1(tmpOutput);
204 divideFilter->SetInput2(tmpSumMasks);
205 divideFilter->Update();
207 typedef itk::MaskImageFilter <ImageType, ImageType, ImageType> MaskFilterType;
208 MaskFilterType::Pointer maskFilter = MaskFilterType::New();
209 maskFilter->SetInput(divideFilter->GetOutput());
210 maskFilter->SetMaskImage(tmpSumMasks);
211 maskFilter->Update();
213 tmpOutput = maskFilter->GetOutput();
214 tmpOutput->DisconnectPipeline();
218 DivideFilterType::Pointer divideFilter = DivideFilterType::New();
219 divideFilter->SetInput1(tmpOutput);
220 divideFilter->SetConstant(sumWeights);
221 divideFilter->Update();
223 tmpOutput = divideFilter->GetOutput();
224 tmpOutput->DisconnectPipeline();
227 CastFilterType::Pointer castFilter = CastFilterType::New();
228 castFilter->SetInput(tmpOutput);
229 castFilter->Update();
231 anima::writeImage <SaveImageType> (outArg.getValue(),castFilter->GetOutput());
235 VectorImageType::Pointer outputData;
236 ImageType::Pointer tmpSumMasks;
238 while (outputData.IsNull())
240 imageIn.getline(refN,2048);
242 if (masksIn.is_open())
243 masksIn.getline(maskN,2048);
244 if (strcmp(refN,
"") == 0)
247 outputData = anima::readImage <VectorImageType> (refN);
248 double imageWeight = 1.0;
249 if (weightsIn.is_open())
250 weightsIn >> imageWeight;
252 std::cout <<
"Adding image " << refN <<
" with weight " << imageWeight <<
"..." << std::endl;
254 sumWeights += imageWeight;
256 if (imageWeight != 1.0)
258 MultiplyVectorFilterType::Pointer multiplyFilter = MultiplyVectorFilterType::New();
259 multiplyFilter->SetInput1(outputData);
260 multiplyFilter->SetConstant(imageWeight);
261 multiplyFilter->Update();
263 outputData = multiplyFilter->GetOutput();
264 outputData->DisconnectPipeline();
267 if (masksIn.is_open())
269 tmpSumMasks = anima::readImage <ImageType> (maskN);
271 MultiplyVectorFilterType::Pointer maskFilter = MultiplyVectorFilterType::New();
272 maskFilter->SetInput1(outputData);
273 maskFilter->SetInput2(tmpSumMasks);
274 maskFilter->Update();
276 outputData = maskFilter->GetOutput();
277 outputData->DisconnectPipeline();
279 if (imageWeight != 1.0)
281 MultiplyFilterType::Pointer multiplyMaskFilter = MultiplyFilterType::New();
282 multiplyMaskFilter->SetInput1(tmpSumMasks);
283 multiplyMaskFilter->SetConstant(imageWeight);
284 multiplyMaskFilter->Update();
286 tmpSumMasks = multiplyMaskFilter->GetOutput();
287 tmpSumMasks->DisconnectPipeline();
292 while(!imageIn.eof())
294 imageIn.getline(refN,2048);
296 if (masksIn.is_open())
297 masksIn.getline(maskN,2048);
298 if (strcmp(refN,
"") == 0)
301 double imageWeight = 1.0;
302 if (weightsIn.is_open())
303 weightsIn >> imageWeight;
305 sumWeights += imageWeight;
307 std::cout <<
"Adding image " << refN <<
" with weight " << imageWeight <<
"..." << std::endl;
309 VectorImageType::Pointer tmpImage = anima::readImage <VectorImageType> (refN);
311 itk::ImageRegionIterator <VectorImageType> tmpIt(tmpImage,tmpImage->GetLargestPossibleRegion());
312 itk::ImageRegionIterator <VectorImageType> resIt(outputData,tmpImage->GetLargestPossibleRegion());
314 if (masksIn.is_open())
316 ImageType::Pointer tmpMask = anima::readImage <ImageType> (maskN);
318 itk::ImageRegionIterator <ImageType> tmpMaskIt(tmpMask,tmpImage->GetLargestPossibleRegion());
319 itk::ImageRegionIterator <ImageType> sumMasksIt(tmpSumMasks,tmpImage->GetLargestPossibleRegion());
321 while (!sumMasksIt.IsAtEnd())
323 if (tmpMaskIt.Get() == 0)
332 sumMasksIt.Set(sumMasksIt.Get() + imageWeight * tmpMaskIt.Get());
333 resIt.Set(resIt.Get() + imageWeight * tmpIt.Get());
343 while (!resIt.IsAtEnd())
345 resIt.Set(resIt.Get() + imageWeight * tmpIt.Get());
352 if (masksIn.is_open())
355 if (weightsIn.is_open())
358 if (tmpSumMasks.IsNotNull())
360 DivideVectorFilterType::Pointer divideFilter = DivideVectorFilterType::New();
361 divideFilter->SetInput1(outputData);
362 divideFilter->SetInput2(tmpSumMasks);
363 divideFilter->Update();
365 typedef itk::MaskImageFilter <VectorImageType, ImageType, VectorImageType> MaskFilterType;
366 MaskFilterType::Pointer maskFilter = MaskFilterType::New();
367 maskFilter->SetInput(divideFilter->GetOutput());
368 maskFilter->SetMaskImage(tmpSumMasks);
369 maskFilter->Update();
371 outputData = maskFilter->GetOutput();
372 outputData->DisconnectPipeline();
376 DivideVectorFilterType::Pointer divideFilter = DivideVectorFilterType::New();
377 divideFilter->SetInput1(outputData);
378 divideFilter->SetConstant(sumWeights);
379 divideFilter->Update();
381 outputData = divideFilter->GetOutput();
382 outputData->DisconnectPipeline();
385 CastVectorFilterType::Pointer castFilter = CastVectorFilterType::New();
386 castFilter->SetInput(outputData);
387 castFilter->Update();
389 anima::writeImage <SaveVectorImageType> (outArg.getValue(),castFilter->GetOutput());
int main(int argc, char **argv)