1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: itkGDCMSeriesStreamReadImageWrite.cxx
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif
#include "itkImageSeriesReader.h"
#include "itkImageFileWriter.h"
#include "itkGDCMImageIO.h"
#include "itkGDCMSeriesFileNames.h"
#include "itkPipelineMonitorImageFilter.h"
#include "itkStreamingImageFilter.h"
/// \brief is comparison with a percentage tollerance
///
/// returns true of the current value is with in tol percentage of m);
///
/// | u - v | <= e * |u| or | u - v | <= e * |v|
/// defines a "close with tolerance e" relationship between u and v
/// this is a symetric comparison
///
/// Also it is check to see if the u an v are both less then
/// epsilon for the type
static bool IsEqualTolerant(const float lm, const float rm, double tol) {
tol = vcl_fabs(tol);
float temp = vcl_fabs(lm - rm);
return temp <= tol*vcl_fabs(lm) ||
temp <= tol*vcl_fabs(rm) ||
(vcl_fabs(lm) < vcl_numeric_limits<float>::epsilon() &&
vcl_fabs(rm) < vcl_numeric_limits<float>::epsilon());
}
int main( int argc, char* argv[] )
{
if( argc < 6 )
{
std::cerr << "Usage: " << argv[0];
std::cerr << " DicomDirectory outputFile ";
std::cerr << " spacingX spacingY spacingZ [ force-no-streaming 1|0]" << std::endl;
return EXIT_FAILURE;
}
typedef itk::Image<unsigned short,3> ImageType;
typedef itk::ImageSeriesReader< ImageType > ReaderType;
typedef itk::GDCMImageIO ImageIOType;
typedef itk::GDCMSeriesFileNames SeriesFileNames;
unsigned int numberOfDataPieces = 4;
ImageType::SpacingType expectedSpacing;
expectedSpacing[0] = atof(argv[3]);
expectedSpacing[1] = atof(argv[4]);
expectedSpacing[2] = atof(argv[5]);
bool forceNoStreaming = true;
if( argc > 6 )
{
if( atoi(argv[6]) != 1 )
{
forceNoStreaming = false;
}
}
bool expectedToStream = !forceNoStreaming;
ImageIOType::Pointer gdcmIO = ImageIOType::New();
SeriesFileNames::Pointer filenameGenerator = SeriesFileNames::New();
filenameGenerator->SetInputDirectory( argv[1] );
ReaderType::Pointer reader = ReaderType::New();
const ReaderType::FileNamesContainer & filenames =
filenameGenerator->GetInputFileNames();
reader->SetFileNames( filenames );
reader->SetImageIO( gdcmIO );
typedef itk::PipelineMonitorImageFilter<ImageType> MonitorFilter;
MonitorFilter::Pointer monitor = MonitorFilter::New();
monitor->SetInput( reader->GetOutput() );
typedef itk::StreamingImageFilter<ImageType, ImageType> StreamingFilter;
StreamingFilter::Pointer streamer = StreamingFilter::New();
streamer->SetInput( monitor->GetOutput() );
streamer->SetNumberOfStreamDivisions( numberOfDataPieces );
try
{
if( forceNoStreaming )
{
// no streaming
reader->UseStreamingOff();
streamer->Update();
}
else
{
// stream based on the number of z-slices
reader->UseStreamingOn();
reader->UpdateOutputInformation();
numberOfDataPieces = reader->GetOutput()->GetLargestPossibleRegion().GetSize()[2];
streamer->SetNumberOfStreamDivisions( numberOfDataPieces );
streamer->Update();
}
}
catch( itk::ExceptionObject & err )
{
std::cerr << "ExceptionObject caught !" << std::endl;
std::cerr << err << std::endl;
std::cerr << monitor;
return EXIT_FAILURE;
}
// verify things executed as expected
bool passed = true;
if( expectedToStream )
{
if( !monitor->VerifyAllInputCanStream(numberOfDataPieces) )
{
passed = false;
}
}
else
{
if( !monitor->VerifyAllIputCanNotStream() )
{
passed = false;
}
}
if( !passed )
{
std::cerr << monitor << std::endl;
std::cerr << "pipeline did not execute as expected!" << std::endl;
return EXIT_FAILURE;
}
std::cout << "Origin: " << reader->GetOutput()->GetOrigin() << std::endl;
std::cout << "direction: " << reader->GetOutput()->GetDirection() << std::endl;
std::cout << "Spacing: " << reader->GetOutput()->GetSpacing() << std::endl;
std::cout << "Expected Spacing: " << expectedSpacing << std::endl;
ImageType::SpacingType spacing = reader->GetOutput()->GetSpacing();
// we only give 4 bits of tolerence, IEEE float a 24-bit mantissa
const double percentTolerance = 1.0 / double ( (unsigned int)(1) << 18);
if (!IsEqualTolerant(spacing[0], expectedSpacing[0], percentTolerance) ||
!IsEqualTolerant(spacing[1], expectedSpacing[1], percentTolerance) ||
!IsEqualTolerant(spacing[2], expectedSpacing[2], percentTolerance))
{
std::cerr << "Spacing does not match expected" << std::endl;
return EXIT_FAILURE;
}
typedef itk::ImageFileWriter< ImageType > WriterType;
WriterType::Pointer writer = WriterType::New();
writer->SetFileName( argv[2] );
writer->SetInput( reader->GetOutput() );
try
{
writer->Update();
}
catch (itk::ExceptionObject &excp)
{
std::cerr << "Exception thrown while writing the image" << std::endl;
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
|