ITK 6.0.0
Insight Toolkit
 
Loading...
Searching...
No Matches
rtkGgoFunctions.h
Go to the documentation of this file.
1/*=========================================================================
2 *
3 * Copyright RTK Consortium
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * https://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18
19// #ifndef RTKGGOFUNCTIONS_H
20// #define RTKGGOFUNCTIONS_H
21
22#ifndef rtkGgoFunctions_h
23#define rtkGgoFunctions_h
24
25#include "rtkMacro.h"
27#include "rtkIOFactories.h"
30#include <itksys/RegularExpression.hxx>
32#include <itkExpImageFilter.h>
35#include <itkLogImageFilter.h>
37
38namespace rtk
39{
40
54template <class TConstantImageSourceType, class TArgsInfo>
55void
56SetConstantImageSourceFromGgo(TConstantImageSourceType * source, const TArgsInfo & args_info)
57{
58 using ImageType = typename TConstantImageSourceType::OutputImageType;
59
60 const unsigned int Dimension = ImageType::GetImageDimension();
61
62 typename ImageType::SizeType imageSize;
63 if (args_info.dimension_given)
64 {
65 itkGenericOutputMacro(
66 "Warning: --dimension is deprecated and will be removed in a future release. Use --size instead.");
67 imageSize.Fill(args_info.dimension_arg[0]);
68 for (unsigned int i = 0; i < std::min(args_info.dimension_given, Dimension); i++)
69 {
70 imageSize[i] = args_info.dimension_arg[i];
71 }
72 }
73 else
74 {
75 imageSize.Fill(args_info.size_arg[0]);
76 for (unsigned int i = 0; i < std::min(args_info.size_given, Dimension); i++)
77 imageSize[i] = args_info.size_arg[i];
78 }
79
80 typename ImageType::SpacingType imageSpacing;
81 imageSpacing.Fill(args_info.spacing_arg[0]);
82 for (unsigned int i = 0; i < std::min(args_info.spacing_given, Dimension); i++)
83 imageSpacing[i] = args_info.spacing_arg[i];
84
85 typename ImageType::PointType imageOrigin;
86 for (unsigned int i = 0; i < Dimension; i++)
87 imageOrigin[i] = imageSpacing[i] * (imageSize[i] - 1) * -0.5;
88 for (unsigned int i = 0; i < std::min(args_info.origin_given, Dimension); i++)
89 imageOrigin[i] = args_info.origin_arg[i];
90
91 typename ImageType::DirectionType imageDirection;
92 if (args_info.direction_given)
93 for (unsigned int i = 0; i < Dimension; i++)
94 for (unsigned int j = 0; j < Dimension; j++)
95 imageDirection[i][j] = args_info.direction_arg[i * Dimension + j];
96 else
97 imageDirection.SetIdentity();
98
99 source->SetOrigin(imageOrigin);
100 source->SetSpacing(imageSpacing);
101 source->SetDirection(imageDirection);
102 source->SetSize(imageSize);
103 source->SetConstant({});
104
105 // Copy output image information from an existing file, if requested
106 // Overwrites parameters given in command line, if any
107 if (args_info.like_given)
108 {
109 using LikeReaderType = itk::ImageFileReader<ImageType>;
110 auto likeReader = LikeReaderType::New();
111 likeReader->SetFileName(args_info.like_arg);
112 TRY_AND_EXIT_ON_ITK_EXCEPTION(likeReader->UpdateOutputInformation());
113 source->SetInformationFromImage(likeReader->GetOutput());
114 }
115
116 TRY_AND_EXIT_ON_ITK_EXCEPTION(source->UpdateOutputInformation());
117}
118
134template <class TArgsInfo>
135std::vector<std::string>
136GetProjectionsFileNamesFromGgo(const TArgsInfo & args_info)
137{
138 // Generate file names
140 names->SetDirectory(args_info.path_arg);
141 names->SetNumericSort(args_info.nsort_flag);
142 names->SetRegularExpression(args_info.regexp_arg);
143 names->SetSubMatch(args_info.submatch_arg);
144
145 if (!names->GetFileNames().size())
146 itkGenericExceptionMacro(<< "The regular expression matches 0 file");
147
148 if (args_info.verbose_flag)
149 std::cout << "Regular expression matches " << names->GetFileNames().size() << " file(s)..." << std::endl;
150
151 // Check submatch in file names
152 if (args_info.submatch_given)
153 {
154 // Check that the submatch number returns something relevant
155 itksys::RegularExpression reg;
156 if (!reg.compile(args_info.regexp_arg))
157 {
158 itkGenericExceptionMacro(<< "Error compiling regular expression " << args_info.regexp_arg);
159 }
160
161 // Store the full filename and the selected sub expression match
162 for (const std::string & name : names->GetFileNames())
163 {
164 reg.find(name);
165 if (reg.match(args_info.submatch_arg) == std::string(""))
166 {
167 itkGenericExceptionMacro(<< "Cannot find submatch " << args_info.submatch_arg << " in " << name
168 << " from regular expression " << args_info.regexp_arg);
169 }
170 }
171 }
172
173 std::vector<std::string> fileNames = names->GetFileNames();
175 std::vector<size_t> idxtopop;
176 size_t i = 0;
177 for (const auto & fn : fileNames)
178 {
180 itk::ImageIOFactory::CreateImageIO(fn.c_str(), itk::ImageIOFactory::IOFileModeEnum::ReadMode);
181
182 if (imageio.IsNull())
183 {
184 std::cerr << "Ignoring file: " << fn << "\n";
185 idxtopop.push_back(i);
186 }
187 i++;
188 }
189 std::reverse(idxtopop.begin(), idxtopop.end());
190 for (const auto & id : idxtopop)
191 {
192 fileNames.erase(fileNames.begin() + id);
193 }
194
195 if (fileNames.empty())
196 itkGenericExceptionMacro(<< "All file names matched by the regular expression could not be read");
197
198 return fileNames;
199}
200
201template <class TProjectionsReaderType, class TArgsInfo>
202void
203SetProjectionsReaderFromGgo(TProjectionsReaderType * reader, const TArgsInfo & args_info)
204{
205 const std::vector<std::string> fileNames = GetProjectionsFileNamesFromGgo(args_info);
206
207 // Vector component extraction
208 if (args_info.component_given)
209 {
210 reader->SetVectorComponent(args_info.component_arg);
211 }
212
213 // Change image information
214 const unsigned int Dimension = TProjectionsReaderType::OutputImageType::GetImageDimension();
215 typename TProjectionsReaderType::OutputImageDirectionType direction;
216 if (args_info.newdirection_given)
217 {
218 direction.Fill(args_info.newdirection_arg[0]);
219 for (unsigned int i = 0; i < args_info.newdirection_given; i++)
220 direction[i / Dimension][i % Dimension] = args_info.newdirection_arg[i];
221 reader->SetDirection(direction);
222 }
223 typename TProjectionsReaderType::OutputImageSpacingType spacing;
224 if (args_info.newspacing_given)
225 {
226 spacing.Fill(args_info.newspacing_arg[0]);
227 for (unsigned int i = 0; i < args_info.newspacing_given; i++)
228 spacing[i] = args_info.newspacing_arg[i];
229 reader->SetSpacing(spacing);
230 }
231 typename TProjectionsReaderType::OutputImagePointType origin;
232 if (args_info.neworigin_given)
233 {
234 origin.Fill(args_info.neworigin_arg[0]);
235 for (unsigned int i = 0; i < args_info.neworigin_given; i++)
236 origin[i] = args_info.neworigin_arg[i];
237 reader->SetOrigin(origin);
238 }
239
240 // Crop boundaries
242 for (unsigned int i = 0; i < args_info.lowercrop_given; i++)
243 lowerCrop[i] = args_info.lowercrop_arg[i];
244 if (args_info.lowercrop_given)
245 reader->SetLowerBoundaryCropSize(lowerCrop);
246
248 for (unsigned int i = 0; i < args_info.uppercrop_given; i++)
249 upperCrop[i] = args_info.uppercrop_arg[i];
250 if (args_info.uppercrop_given)
251 reader->SetUpperBoundaryCropSize(upperCrop);
252
253 // Conditional median
254 typename TProjectionsReaderType::MedianRadiusType medianRadius{};
255 for (unsigned int i = 0; i < args_info.radius_given; i++)
256 medianRadius[i] = args_info.radius_arg[i];
257 reader->SetMedianRadius(medianRadius);
258 if (args_info.multiplier_given)
259 reader->SetConditionalMedianThresholdMultiplier(args_info.multiplier_arg);
260
261 // Shrink / Binning
262 typename TProjectionsReaderType::ShrinkFactorsType binFactors;
263 binFactors.Fill(1);
264 for (unsigned int i = 0; i < args_info.binning_given; i++)
265 binFactors[i] = args_info.binning_arg[i];
266 reader->SetShrinkFactors(binFactors);
267
268 // Boellaard scatter correction
269 if (args_info.spr_given)
270 reader->SetScatterToPrimaryRatio(args_info.spr_arg);
271 if (args_info.nonneg_given)
272 reader->SetNonNegativityConstraintThreshold(args_info.nonneg_arg);
273 if (args_info.airthres_given)
274 reader->SetAirThreshold(args_info.airthres_arg);
275
276 // I0 and IDark
277 if (args_info.i0_given)
278 reader->SetI0(args_info.i0_arg);
279 reader->SetIDark(args_info.idark_arg);
280
281 // Line integral flag
282 if (args_info.nolineint_flag)
283 reader->ComputeLineIntegralOff();
284
285 // Water precorrection
286 if (args_info.wpc_given)
287 {
288 std::vector<double> coeffs;
289 coeffs.assign(args_info.wpc_arg, args_info.wpc_arg + args_info.wpc_given);
290 reader->SetWaterPrecorrectionCoefficients(coeffs);
291 }
292
293 // Pass list to projections reader
294 reader->SetFileNames(fileNames);
295 TRY_AND_EXIT_ON_ITK_EXCEPTION(reader->UpdateOutputInformation());
296}
297
298
299template <class TImageType, class TArgsInfo>
300typename TImageType::Pointer
301AddNoiseFromGgo(typename TImageType::Pointer projections, const TArgsInfo & args_info)
302{
303 using ImageType = TImageType;
304 typename TImageType::Pointer newproj = projections;
305
306 // Poisson noise
307 if (args_info.poisson_given)
308 {
309 const double I0{ args_info.poisson_arg[0] };
310 double muref{ 0.01879 }; // mm^-1, attenuation coefficient of water at 75 keV
311 if (args_info.poisson_given > 1)
312 muref = args_info.poisson_arg[1];
313
315 multiply->SetInput(newproj);
316 multiply->SetConstant(-muref);
317
319 expf->SetInput(multiply->GetOutput());
320
322 multiply2->SetInput(expf->GetOutput());
323 multiply2->SetConstant(I0);
324
326 poisson->SetInput(multiply2->GetOutput());
327
329 threshold->SetInput(poisson->GetOutput());
330 threshold->SetLower(1.);
331 threshold->SetOutsideValue(1.);
332
334 // Gaussian noise
336 if (args_info.gaussian_given)
337 {
338 gaussian->SetInput(threshold->GetOutput());
339 gaussian->SetStandardDeviation(args_info.gaussian_arg);
340 multiply3->SetInput(gaussian->GetOutput());
341 }
342 else
343 multiply3->SetInput(threshold->GetOutput());
344 multiply3->SetConstant(1. / I0);
345
347 logf->SetInput(multiply3->GetOutput());
348
350 multiply4->SetInput(logf->GetOutput());
351 multiply4->SetConstant(-1. / muref);
352
353 TRY_AND_EXIT_ON_ITK_EXCEPTION(multiply4->Update());
354 newproj = multiply4->GetOutput();
355 }
356 else if (args_info.gaussian_given)
357 {
359 gaussian->SetInput(newproj);
360 gaussian->SetStandardDeviation(args_info.gaussian_arg);
361
362 TRY_AND_EXIT_ON_ITK_EXCEPTION(gaussian->Update());
363 newproj = gaussian->GetOutput();
364 }
365
366 return newproj;
367}
368
377template <class TArgsInfo, class TIterativeReconstructionFilter>
378void
379SetBackProjectionFromGgo(const TArgsInfo & args_info, TIterativeReconstructionFilter * recon)
380{
381 typename TIterativeReconstructionFilter::VolumeType::Pointer attenuationMap;
382 using VolumeType = typename TIterativeReconstructionFilter::VolumeType;
383 if (args_info.attenuationmap_given)
384 {
385 // Read an existing image to initialize the attenuation map
386 attenuationMap = itk::ReadImage<VolumeType>(args_info.attenuationmap_arg);
387 }
388
389 switch (args_info.bp_arg)
390 {
391 case (-1): // bp__NULL, keep default
392 break;
393 case (0): // bp_arg_VoxelBasedBackProjection
394 recon->SetBackProjectionFilter(TIterativeReconstructionFilter::BP_VOXELBASED);
395 break;
396 case (1): // bp_arg_Joseph
397 recon->SetBackProjectionFilter(TIterativeReconstructionFilter::BP_JOSEPH);
398 break;
399 case (2): // bp_arg_CudaVoxelBased
400 recon->SetBackProjectionFilter(TIterativeReconstructionFilter::BP_CUDAVOXELBASED);
401 break;
402 case (3): // bp_arg_CudaRayCast
403 recon->SetBackProjectionFilter(TIterativeReconstructionFilter::BP_CUDARAYCAST);
404 if (args_info.step_given)
405 recon->SetStepSize(args_info.step_arg);
406 break;
407 case (4): // bp_arg_JosephAttenuated
408 recon->SetBackProjectionFilter(TIterativeReconstructionFilter::BP_JOSEPHATTENUATED);
409 if (args_info.attenuationmap_given)
410 recon->SetAttenuationMap(attenuationMap);
411 break;
412 case (5): // bp_arg_RotationBased
413 recon->SetBackProjectionFilter(TIterativeReconstructionFilter::BP_ZENG);
414 if (args_info.sigmazero_given)
415 recon->SetSigmaZero(args_info.sigmazero_arg);
416 if (args_info.alphapsf_given)
417 recon->SetAlphaPSF(args_info.alphapsf_arg);
418 if (args_info.attenuationmap_given)
419 recon->SetAttenuationMap(attenuationMap);
420 break;
421 case (6): // bp_arg_CudaZeng
422 recon->SetBackProjectionFilter(TIterativeReconstructionFilter::BP_CUDAZENG);
423 if (args_info.sigmazero_given)
424 recon->SetSigmaZero(args_info.sigmazero_arg);
425 if (args_info.alphapsf_given)
426 recon->SetAlphaPSF(args_info.alphapsf_arg);
427 if (args_info.attenuationmap_given)
428 recon->SetAttenuationMap(attenuationMap);
429 break;
430 }
431}
432
441template <class TArgsInfo, class TIterativeReconstructionFilter>
442void
443SetForwardProjectionFromGgo(const TArgsInfo & args_info, TIterativeReconstructionFilter * recon)
444{
445 typename TIterativeReconstructionFilter::VolumeType::Pointer attenuationMap;
446 using VolumeType = typename TIterativeReconstructionFilter::VolumeType;
447 if (args_info.attenuationmap_given)
448 {
449 // Read an existing image to initialize the attenuation map
450 attenuationMap = itk::ReadImage<VolumeType>(args_info.attenuationmap_arg);
451 }
453 typename ClipImageType::Pointer inferiorClipImage, superiorClipImage;
454 if (args_info.inferiorclipimage_given)
455 {
456 // Read an existing image to initialize the attenuation map
457 inferiorClipImage = itk::ReadImage<ClipImageType>(args_info.inferiorclipimage_arg);
458 }
459 if (args_info.superiorclipimage_given)
460 {
461 // Read an existing image to initialize the attenuation map
462 superiorClipImage = itk::ReadImage<ClipImageType>(args_info.superiorclipimage_arg);
463 }
464
465 switch (args_info.fp_arg)
466 {
467 case (-1): // fp__NULL, keep default
468 break;
469 case (0): // fp_arg_Joseph
470 recon->SetForwardProjectionFilter(TIterativeReconstructionFilter::FP_JOSEPH);
471 if (args_info.superiorclipimage_given)
472 recon->SetSuperiorClipImage(superiorClipImage);
473 if (args_info.inferiorclipimage_given)
474 recon->SetInferiorClipImage(inferiorClipImage);
475 break;
476 case (1): // fp_arg_CudaRayCast
477 recon->SetForwardProjectionFilter(TIterativeReconstructionFilter::FP_CUDARAYCAST);
478 if (args_info.step_given)
479 recon->SetStepSize(args_info.step_arg);
480 break;
481 case (2): // fp_arg_JosephAttenuated
482 recon->SetForwardProjectionFilter(TIterativeReconstructionFilter::FP_JOSEPHATTENUATED);
483 if (args_info.superiorclipimage_given)
484 recon->SetSuperiorClipImage(superiorClipImage);
485 if (args_info.inferiorclipimage_given)
486 recon->SetInferiorClipImage(inferiorClipImage);
487 if (args_info.attenuationmap_given)
488 recon->SetAttenuationMap(attenuationMap);
489 break;
490 case (3): // fp_arg_RotationBased
491 recon->SetForwardProjectionFilter(TIterativeReconstructionFilter::FP_ZENG);
492 if (args_info.sigmazero_given)
493 recon->SetSigmaZero(args_info.sigmazero_arg);
494 if (args_info.alphapsf_given)
495 recon->SetAlphaPSF(args_info.alphapsf_arg);
496 if (args_info.attenuationmap_given)
497 recon->SetAttenuationMap(attenuationMap);
498 break;
499 case (4): // fp_arg_CudaZeng
500 recon->SetForwardProjectionFilter(TIterativeReconstructionFilter::FP_CUDAZENG);
501 if (args_info.sigmazero_given)
502 recon->SetSigmaZero(args_info.sigmazero_arg);
503 if (args_info.alphapsf_given)
504 recon->SetAlphaPSF(args_info.alphapsf_arg);
505 if (args_info.attenuationmap_given)
506 recon->SetAttenuationMap(attenuationMap);
507 break;
508 }
509}
510
511} // namespace rtk
512
513#endif // rtkGgoFunctions_h
static Pointer New()
Data source that reads image data from a single file.
SmartPointer< Self > Pointer
static ImageIOBasePointer CreateImageIO(const char *path, IOFileModeEnum mode)
Templated n-dimensional image class.
Definition itkImage.h:89
static Pointer New()
static Pointer New()
static Pointer New()
bool IsNull() const noexcept
static Pointer New()
std::vector< std::string > GetProjectionsFileNamesFromGgo(const TArgsInfo &args_info)
Read a stack of 2D projections from gengetopt specifications.
void SetBackProjectionFromGgo(const TArgsInfo &args_info, TIterativeReconstructionFilter *recon)
Set the correct RTK backprojection type from gengetopt Given a gengetopt bp_arg option from the rtkpr...
void SetForwardProjectionFromGgo(const TArgsInfo &args_info, TIterativeReconstructionFilter *recon)
Set the correct RTK forward projection type from gengetopt Given a gengetopt fp_arg option from the r...
void SetConstantImageSourceFromGgo(TConstantImageSourceType *source, const TArgsInfo &args_info)
Create 3D image from gengetopt specifications.
#define TRY_AND_EXIT_ON_ITK_EXCEPTION(execFunc)
Update a filter and catching/displaying exceptions.
Definition rtkMacro.h:88
constexpr TContainer MakeFilled(typename TContainer::const_reference value)
TOutputImage::Pointer ReadImage(const std::string &filename)
void SetProjectionsReaderFromGgo(TProjectionsReaderType *reader, const TArgsInfo &args_info)
void RTK_EXPORT RegisterIOFactories()
TImageType::Pointer AddNoiseFromGgo(typename TImageType::Pointer projections, const TArgsInfo &args_info)