Skip to content

Commit

Permalink
ENH: allow Nifti with wrong scales in sform
Browse files Browse the repository at this point in the history
Allow loading of Nifti files incorrectly written by by VINCI with wrong scales in sform,
s. #3514
  • Loading branch information
issakomi authored and dzenanz committed Sep 19, 2022
1 parent 1b285b5 commit 927eb26
Showing 1 changed file with 13 additions and 10 deletions.
23 changes: 13 additions & 10 deletions Modules/IO/NIFTI/src/itkNiftiImageIO.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -1913,22 +1913,25 @@ NiftiImageIO::SetImageIOOrientationFromNIfTI(unsigned short dims)
vnl_matrix_fixed<float, 3, 3> rotation = sto_xyz.extract(3, 3, 0, 0);
{
// Ensure that the scales are approximately the same for spacing directions
vnl_vector_fixed<float, 3> scale;
scale[0] = rotation.get_column(0).magnitude();
bool sform_scales_ok{ true };
constexpr float large_value_tolerance = 1e-3; // Numerical precision of sform is not very good
if (itk::Math::abs(this->m_NiftiImage->dx - scale[0]) > large_value_tolerance)
if (itk::Math::abs(this->m_NiftiImage->dx - rotation.get_column(0).magnitude()) > large_value_tolerance)
{
return false;
sform_scales_ok = false;
}
scale[1] = rotation.get_column(1).magnitude();
if (itk::Math::abs(this->m_NiftiImage->dy - scale[1]) > large_value_tolerance)
else if (itk::Math::abs(this->m_NiftiImage->dy - rotation.get_column(1).magnitude()) >
large_value_tolerance)
{
return false;
sform_scales_ok = false;
}
scale[2] = rotation.get_column(2).magnitude();
if (itk::Math::abs(this->m_NiftiImage->dz - scale[2]) > large_value_tolerance)
else if (itk::Math::abs(this->m_NiftiImage->dz - rotation.get_column(2).magnitude()) >
large_value_tolerance)
{
return false;
sform_scales_ok = false;
}
if (!sform_scales_ok)
{
itkWarningMacro(<< this->GetFileName() << " has unexpected scales in sform");
}
}
// Remove scale from columns
Expand Down

0 comments on commit 927eb26

Please sign in to comment.