From 784b1126bcf4170e91bce98a1a3e60ef50184e78 Mon Sep 17 00:00:00 2001 From: Hans Johnson Date: Thu, 14 Nov 2024 19:23:39 -0600 Subject: [PATCH 1/2] BUG: Reduce tolerance for same slice checks The AlmostEquals function fails for slices that are only 1e-15 different for 1.5mm slices. The DefaultImageCoordinateTolerance has been determined to be 1e-6. Using the global default tolerance provides more meaningful default behaviors. --- Modules/IO/ImageBase/include/itkImageSeriesReader.hxx | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Modules/IO/ImageBase/include/itkImageSeriesReader.hxx b/Modules/IO/ImageBase/include/itkImageSeriesReader.hxx index de34f120d41..e12c3ffd9e1 100644 --- a/Modules/IO/ImageBase/include/itkImageSeriesReader.hxx +++ b/Modules/IO/ImageBase/include/itkImageSeriesReader.hxx @@ -27,6 +27,7 @@ #include "itkMetaDataObject.h" #include // For ptrdiff_t. #include +#include "itkImageBase.h" namespace itk { @@ -432,9 +433,8 @@ ImageSeriesReader::GenerateData() SpacingScalarType dirNnorm = dirN.GetNorm(); if (this->m_SpacingDefined && - !Math::AlmostEquals( - dirNnorm, - outputSpacing[this->m_NumberOfDimensionsInImage])) // either non-uniform sampling or missing slice + std::abs(dirNnorm - outputSpacing[this->m_NumberOfDimensionsInImage]) > + itk::DefaultImageCoordinateTolerance) // either non-uniform sampling or missing slice { nonUniformSampling = true; spacingDeviation = itk::Math::abs(outputSpacing[this->m_NumberOfDimensionsInImage] - dirNnorm); From 96ead7d16c716dc57a797618dcd2fdd55caae1d8 Mon Sep 17 00:00:00 2001 From: Hans Johnson Date: Thu, 14 Nov 2024 19:53:33 -0600 Subject: [PATCH 2/2] BUG: Premature failure when reading dicom slices to 3D When reading individual 2D dicom slices, the slice thickness may not be known at each slice reading. If set to spacing of 0 (the default) an exception is thrown before the inter-slice distance can be computed. Catch this case and set a temporary inter-slice thickness. --- Modules/IO/GDCM/src/itkGDCMImageIO.cxx | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/Modules/IO/GDCM/src/itkGDCMImageIO.cxx b/Modules/IO/GDCM/src/itkGDCMImageIO.cxx index b8f31090fee..53ca1b22c3c 100644 --- a/Modules/IO/GDCM/src/itkGDCMImageIO.cxx +++ b/Modules/IO/GDCM/src/itkGDCMImageIO.cxx @@ -57,6 +57,7 @@ #include "gdcmDirectionCosines.h" #include +#include #include namespace itk @@ -681,7 +682,12 @@ GDCMImageIO::InternalReadImageInformation() const double * sp = image.GetSpacing(); spacing[0] = sp[0]; spacing[1] = sp[1]; - spacing[2] = sp[2]; + // A 2D dicom slice may not have an explicit interslice provided per + // file. interslice distances can be computed after all slices are read. + // set to non-zero value here to avoid prematurely throwing an exception + // before the interslice thickness can be computed. + const auto abs_spacing_2 = std::abs(sp[2]); // Spacing may be negative at this point, will be fixed below + spacing[2] = (abs_spacing_2 < itk::DefaultImageCoordinateTolerance) ? 1.0 : sp[2]; } break; }