I am reading dicom series and manipulate some tags from it. Finally write the new series. I can change any dicom tag without any problems. They are correcly changed. But the new dataset is visualized wrongly. The axial slice is not ordered correctly and many of the slices are missing. For sagittal and coronal views, I get only white lines and dots. I would be grateful for any suggestion, where the issue could be.
const unsigned int Dimension = 3;
const unsigned int OutputDimension = 2;
typedef signed short PixelType;
typedef itk::Image< PixelType, Dimension > InputImageType;
typedef itk::Image< PixelType, OutputDimension > OutputImageType;
typedef itk::GDCMSeriesFileNames InputNamesGeneratorType;
typedef itk::NumericSeriesFileNames OutputNamesGeneratorType;
typedef itk::ImageSeriesReader< InputImageType > ReaderType;
typedef itk::ImageSeriesWriter< InputImageType, OutputImageType > SeriesWriterType;
typedef itk::GDCMImageIO ImageIOType;
ImageIOType::Pointer gdcmIO = ImageIOType::New();
InputNamesGeneratorType::Pointer inputNames = InputNamesGeneratorType::New();
inputNames->SetInputDirectory("C:/dicomDir/");
const ReaderType::FileNamesContainer& filenames = inputNames->GetInputFileNames();
// Read
ReaderType::Pointer seriesReader = ReaderType::New();
seriesReader->SetImageIO(gdcmIO);
seriesReader->SetFileNames(filenames);
seriesReader->Update();
//const InputImageType::SpacingType& inputSpacing = seriesReader->GetOutput()->GetSpacing();
const InputImageType::RegionType& inputRegion = seriesReader->GetOutput()->GetLargestPossibleRegion();
const InputImageType::SizeType& inputSize = inputRegion.GetSize();
InputImageType::SizeType outputSize;
typedef InputImageType::SizeType::SizeValueType SizeValueType;
outputSize[0] = inputSize[0];
outputSize[1] = inputSize[1];
outputSize[2] = inputSize[2];
ReaderType::DictionaryRawPointer inputDict = (*(seriesReader->GetMetaDataDictionaryArray()))[0];
ReaderType::DictionaryArrayType outputArray;
gdcm::UIDGenerator sUid;
std::string seriesUid = sUid.Generate();
gdcm::UIDGenerator frameUid;
std::string frameOfReferenceUid = frameUid.Generate();
std::string studyUid;
std::string sopClassUid;
itk::ExposeMetaData<std::string>(*inputDict, "0020|000d", studyUid);
itk::ExposeMetaData<std::string>(*inputDict, "0008|0016", sopClassUid);
gdcmIO->KeepOriginalUIDOn();
for (unsigned int i = 0; i < outputSize[2]; i++)
{
// Create a new dictionary for each slice
ReaderType::DictionaryRawPointer dict = new ReaderType::DictionaryType;
// Copy the dictionary from the first slice
copyDictionary(*inputDict, *dict);
// Set the UID's for the study, series, SOP and frame of reference
itk::EncapsulateMetaData<std::string>(*dict, "0020|000d", studyUid); // Study Instance UID
itk::EncapsulateMetaData<std::string>(*dict, "0020|000e", seriesUid); // Series Instance UID
itk::EncapsulateMetaData<std::string>(*dict, "0020|0052", frameOfReferenceUid); // Frame of Reference UID
gdcm::UIDGenerator sopUid;
std::string sopInstanceUid = sopUid.Generate();
itk::EncapsulateMetaData<std::string>(*dict, "0008|0018", sopInstanceUid); // SOP Instance UID
itk::EncapsulateMetaData<std::string>(*dict, "0002|0003", sopInstanceUid); // Media Stored SOP Instance UID
// Change patient name
itk::EncapsulateMetaData<std::string>(*dict, "0010|0010", "Patient Name Anonymized"); // Patient Name
// Change fields that are slice specific
itksys_ios::ostringstream value;
value.str("");
value << i + 1;
// Image Number
itk::EncapsulateMetaData<std::string>(*dict, "0020|0013", value.str());
outputArray.push_back(dict);
}
// Write
itksys::SystemTools::MakeDirectory("anonymizedPatientDataset");
OutputNamesGeneratorType::Pointer outputNames = OutputNamesGeneratorType::New();
std::string seriesFormat("anonymizedPatientDataset");
seriesFormat = seriesFormat + "/" + "000%d.dcm";
outputNames->SetSeriesFormat(seriesFormat.c_str());
outputNames->SetStartIndex(1);
outputNames->SetEndIndex(outputSize[2]);
SeriesWriterType::Pointer seriesWriter = SeriesWriterType::New();
seriesWriter->SetInput(seriesReader->GetOutput());
seriesWriter->SetImageIO(gdcmIO);
seriesWriter->SetFileNames(outputNames->GetFileNames());
seriesWriter->SetMetaDataDictionaryArray(&outputArray);
seriesWriter->Update();