-
-
Notifications
You must be signed in to change notification settings - Fork 663
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
BUG: Set NIFTI header xyzt_units field correctly
Previously, the correct xyzt code was assigned to the 'xyz_units' variable, but time gets masked out because it's only for space units, resulting in xyzt_units=2. Fix by setting 'time_units' to NIFTI_UNITS_SEC. This produces images with xyzt_units == 10, which is correct for ITK output (NIFTI_UNITS_MM | NIFTI_UNITS_SEC)
- Loading branch information
Showing
3 changed files
with
124 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/*========================================================================= | ||
* | ||
* Copyright NumFOCUS | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0.txt | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*=========================================================================*/ | ||
|
||
#include "itkNiftiImageIOTest.h" | ||
#include "itkTestingMacros.h" | ||
|
||
// Test writing NIFTI xyzt_units field - should be 10 (mm, sec) | ||
int | ||
itkNiftiImageIOTest14(int argc, char * argv[]) | ||
{ | ||
|
||
if (argc > 1) | ||
{ | ||
char * testdir = argv[1]; | ||
itksys::SystemTools::ChangeDirectory(testdir); | ||
} | ||
else | ||
{ | ||
return EXIT_FAILURE; | ||
} | ||
|
||
constexpr unsigned int Dimension = 3; | ||
|
||
using PixelType = float; | ||
using ImageType = itk::Image<PixelType, Dimension>; | ||
|
||
ImageType::Pointer image; | ||
|
||
// fill out an image and write it as nifti. | ||
const char * filename = "xyzt_unit_check.nii"; | ||
using OutputImageType = itk::Image<PixelType, Dimension>; | ||
typename OutputImageType::RegionType region; | ||
typename OutputImageType::IndexType start; | ||
typename OutputImageType::SizeType size; | ||
size[0] = 8; | ||
size[1] = 8; | ||
size[2] = 4; | ||
region.SetSize(size); | ||
region.SetIndex(start); | ||
auto outputimage = OutputImageType::New(); | ||
outputimage->SetRegions(region); | ||
outputimage->Allocate(); | ||
|
||
auto writer = itk::ImageFileWriter<OutputImageType>::New(); | ||
auto niftiImageIO = itk::NiftiImageIO::New(); | ||
writer->SetImageIO(niftiImageIO); | ||
writer->SetFileName(filename); | ||
writer->SetInput(outputimage); | ||
try | ||
{ | ||
writer->Update(); | ||
} | ||
catch (const itk::ExceptionObject & err) | ||
{ | ||
std::cerr << "Exception Object caught: " << std::endl << err << std::endl; | ||
throw; | ||
} | ||
|
||
bool fileHasCorrectXYZTTUnits = false; | ||
|
||
try | ||
{ | ||
// read the image back in | ||
ImageType::Pointer image_from_disk = itk::IOTestHelper::ReadImage<ImageType>(std::string(filename)); | ||
// check the metadata for xyzt_units | ||
itk::MetaDataDictionary & dictionary = image_from_disk->GetMetaDataDictionary(); | ||
std::string xyzt_units; | ||
if (!itk::ExposeMetaData<std::string>(dictionary, "xyzt_units", xyzt_units)) | ||
{ | ||
std::cerr << "xyzt_units not found in metadata" << std::endl; | ||
} | ||
if (xyzt_units == "10") | ||
{ | ||
fileHasCorrectXYZTTUnits = true; | ||
} | ||
else | ||
{ | ||
std::cerr << "xyzt_units not set correctly in metadata" << std::endl; | ||
fileHasCorrectXYZTTUnits = false; | ||
} | ||
} | ||
catch (...) | ||
{ | ||
std::cerr << "Exception caught while reading image back in" << std::endl; | ||
} | ||
|
||
itk::IOTestHelper::Remove(filename); | ||
|
||
if (fileHasCorrectXYZTTUnits) | ||
{ | ||
return EXIT_SUCCESS; | ||
} | ||
return EXIT_FAILURE; | ||
} |