|
| 1 | +/*========================================================================= |
| 2 | + * |
| 3 | + * Copyright NumFOCUS |
| 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 | +#include "itkImage.h" |
| 20 | +#include "itkImageFileWriter.h" |
| 21 | +#include "itkGDCMImageIO.h" |
| 22 | +#include "itkMetaDataObject.h" |
| 23 | +#include "itkGTest.h" |
| 24 | +#include "itksys/SystemTools.hxx" |
| 25 | +#include "itkImageSeriesReader.h" |
| 26 | +#include <string> |
| 27 | +#include <vector> |
| 28 | + |
| 29 | + |
| 30 | +#define _STRING(s) #s |
| 31 | +#define TOSTRING(s) std::string(_STRING(s)) |
| 32 | + |
| 33 | +namespace |
| 34 | +{ |
| 35 | + |
| 36 | +struct ITKGDCMImageIO : public ::testing::Test |
| 37 | +{ |
| 38 | + void |
| 39 | + SetUp() override |
| 40 | + { |
| 41 | + itksys::SystemTools::MakeDirectory(m_TempDir); |
| 42 | + } |
| 43 | + |
| 44 | + void |
| 45 | + TearDown() override |
| 46 | + { |
| 47 | + itksys::SystemTools::RemoveADirectory(m_TempDir); |
| 48 | + } |
| 49 | + |
| 50 | + std::string m_TempDir = TOSTRING(ITK_TEST_OUTPUT_DIR) + "/ITKGDCMImageIO"; |
| 51 | +}; |
| 52 | + |
| 53 | +} // namespace |
| 54 | + |
| 55 | +TEST_F(ITKGDCMImageIO, ReadSlicesNegativeSpacing) |
| 56 | +{ |
| 57 | + // Tests the case where the slices are written with negative space between slices, |
| 58 | + // and the reader should be able to read them back correctly with negative in the direction cosine matrix. |
| 59 | + |
| 60 | + |
| 61 | + using PixelType = uint16_t; |
| 62 | + constexpr unsigned int Dimension = 3; |
| 63 | + using ImageType = itk::Image<PixelType, Dimension>; |
| 64 | + using SliceType = itk::Image<PixelType, 2>; |
| 65 | + |
| 66 | + // Create a 3D image of size [2,2,3] and fill with 100 |
| 67 | + ImageType::SizeType size = { { 2, 2, 3 } }; |
| 68 | + ImageType::RegionType region; |
| 69 | + region.SetSize(size); |
| 70 | + auto image = ImageType::New(); |
| 71 | + image->SetRegions(region); |
| 72 | + image->Allocate(); |
| 73 | + image->FillBuffer(100); |
| 74 | + |
| 75 | + // DICOM meta-data values |
| 76 | + const std::vector<std::string> positions = { "-216.500\\-216.500\\70.000", |
| 77 | + "-216.500\\-216.500\\-187.500", |
| 78 | + "-216.500\\-216.500\\-445.000" }; |
| 79 | + const std::string orientation = "1.000000\\0.000000\\0.000000\\0.000000\\1.000000\\0.000000"; |
| 80 | + const std::string pixelSpacing = "1.1\\1.2"; |
| 81 | + |
| 82 | + std::string seriesDir = m_TempDir + "/" + ::testing::UnitTest::GetInstance()->current_test_info()->name(); |
| 83 | + itksys::SystemTools::MakeDirectory(seriesDir); |
| 84 | + |
| 85 | + using WriterType = itk::ImageFileWriter<SliceType>; |
| 86 | + using ImageIOType = itk::GDCMImageIO; |
| 87 | + |
| 88 | + for (unsigned int i = 0; i < size[2]; ++i) |
| 89 | + { |
| 90 | + // Extract 2D slice |
| 91 | + SliceType::RegionType sliceRegion; |
| 92 | + SliceType::SizeType sliceSize = { { size[0], size[1] } }; |
| 93 | + SliceType::IndexType sliceIndex = { { 0, 0 } }; |
| 94 | + sliceRegion.SetSize(sliceSize); |
| 95 | + sliceRegion.SetIndex(sliceIndex); |
| 96 | + |
| 97 | + SliceType::Pointer slice = SliceType::New(); |
| 98 | + slice->SetRegions(sliceRegion); |
| 99 | + slice->Allocate(); |
| 100 | + |
| 101 | + slice->FillBuffer(5 + i); |
| 102 | + |
| 103 | + // Set image spacing |
| 104 | + ImageType::SpacingType spacing{ { 1.1, 1.2, 257.5 } }; |
| 105 | + image->SetSpacing(spacing); |
| 106 | + |
| 107 | + |
| 108 | + // Set required DICOM tags |
| 109 | + itk::MetaDataDictionary & dict = slice->GetMetaDataDictionary(); |
| 110 | + itk::EncapsulateMetaData<std::string>(dict, "0020|0032", positions[i]); |
| 111 | + itk::EncapsulateMetaData<std::string>(dict, "0020|0037", orientation); |
| 112 | + itk::EncapsulateMetaData<std::string>(dict, "0028|0030", pixelSpacing); |
| 113 | + |
| 114 | + // Write the slice |
| 115 | + auto writer = WriterType::New(); |
| 116 | + auto gdcmIO = ImageIOType::New(); |
| 117 | + std::ostringstream filename; |
| 118 | + filename << seriesDir << "/slice_" << (i + 1) << ".dcm"; |
| 119 | + writer->SetFileName(filename.str()); |
| 120 | + writer->SetInput(slice); |
| 121 | + writer->SetImageIO(gdcmIO); |
| 122 | + ASSERT_NO_THROW(writer->Update()); |
| 123 | + } |
| 124 | + |
| 125 | + std::vector<std::string> fileNames; |
| 126 | + for (unsigned int i = 0; i < size[2]; ++i) |
| 127 | + { |
| 128 | + std::ostringstream filename; |
| 129 | + filename << seriesDir << "/slice_" << (i + 1) << ".dcm"; |
| 130 | + fileNames.push_back(filename.str()); |
| 131 | + } |
| 132 | + |
| 133 | + // Reader the slices back |
| 134 | + using SeriesReaderType = itk::ImageSeriesReader<ImageType>; |
| 135 | + auto seriesReader = SeriesReaderType::New(); |
| 136 | + seriesReader->SetFileNames(fileNames); |
| 137 | + auto gdcmIO = ImageIOType::New(); |
| 138 | + seriesReader->SetImageIO(gdcmIO); |
| 139 | + ASSERT_NO_THROW(seriesReader->Update()); |
| 140 | + auto outputImage = seriesReader->GetOutput(); |
| 141 | + |
| 142 | + // Check the spacing, origin, and direction |
| 143 | + ImageType::SpacingType spacing = outputImage->GetSpacing(); |
| 144 | + ImageType::PointType origin = outputImage->GetOrigin(); |
| 145 | + ImageType::DirectionType direction = outputImage->GetDirection(); |
| 146 | + ImageType::DirectionType expectedDirection; |
| 147 | + expectedDirection.SetIdentity(); |
| 148 | + expectedDirection[2][2] = -1.0; // Set the z-direction to -1.0 |
| 149 | + ImageType::PointType expectedOrigin{ { -216.5, -216.5, 70.0 } }; |
| 150 | + ImageType::SpacingType expectedSpacing{ { 1.1, 1.2, 257.5 } }; |
| 151 | + |
| 152 | + std::cout << "Spacing: " << spacing << std::endl; |
| 153 | + |
| 154 | + EXPECT_EQ(spacing, expectedSpacing); |
| 155 | + EXPECT_EQ(origin, expectedOrigin); |
| 156 | + EXPECT_EQ(direction, expectedDirection); |
| 157 | +} |
0 commit comments