Last active
November 23, 2016 14:39
-
-
Save ipa/669d5756a83f503336d0ee1d654efaa3 to your computer and use it in GitHub Desktop.
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
cmake_minimum_required(VERSION 3.2) | |
project(niftytodicom) | |
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} ") #-std=c++11 -fpermissive | |
find_package(ITK REQUIRED) | |
include(${ITK_USE_FILE}) | |
set(SOURCE_FILES main.cpp) | |
add_executable(niftytodicom ${SOURCE_FILES}) | |
target_link_libraries(niftytodicom ${ITK_LIBRARIES}) |
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
// | |
// Created by ipa on 07/11/16. | |
// | |
#include <iostream> | |
#include <itkImage.h> | |
#include <gdcmImageWriter.h> | |
#include <itkImageSeriesWriter.h> | |
#include <itkGDCMImageIO.h> | |
#include <itkNumericSeriesFileNames.h> | |
typedef itk::Image< unsigned char, 3 > UC3ImageType; | |
typedef itk::Image< unsigned char, 2 > UC2ImageType; | |
typedef itk::ImageSeriesWriter< UC3ImageType, UC2ImageType > WriterType; | |
const std::string output_dir = "series/series%03d.dcm"; | |
void CopyDictionary(itk::MetaDataDictionary &fromDict, itk::MetaDataDictionary &toDict); | |
void PrintDictionary(itk::MetaDataDictionary &dict); | |
const std::string PATIENT_NAME = "0010|0010"; | |
const std::string PATIENT_ID = "0010|0020"; | |
const std::string STUDY_INSTANCE_UID = "0020|000D"; | |
const std::string STUDY_ID = "0020|0010"; | |
const std::string SERIES_INSTANCE_UID = "0020|000E"; | |
const std::string SERIES_DESCRIPTION = "0008|103E"; | |
const std::string SERIES_NUMBER = "0020|0011"; | |
const std::string INSTANCE_NUMBER = "0020|0013"; | |
const std::string IMAGE_POSITION_PATIENT = "0020|0032"; | |
const std::string IMAGE_ORIENTATION_PATIENT = "0020|0037"; | |
const std::string PATIENT_POSITION = "0018|5100"; | |
const std::string SLICE_SPACING = "0018|0088"; | |
const std::string SLICE_THICKNESS = "0018|0050"; | |
const std::string PIXEL_SPACING = "0028|0030"; | |
const std::string SLICE_LOCATION = "0020|1041"; | |
int main(){ | |
UC3ImageType::Pointer image = UC3ImageType::New(); | |
UC3ImageType::SizeType image_size; | |
image_size[0] = 256; | |
image_size[1] = 256; | |
image_size[2] = 256; | |
UC3ImageType::RegionType image_region; | |
image_region.SetSize(image_size); | |
image->SetRegions(image_region); | |
image->Allocate(); | |
WriterType::DictionaryArrayType dictionaryArray; | |
gdcm::UIDGenerator studyuid; | |
std::string v_StudyUID = studyuid.Generate(); | |
std::cout << v_StudyUID << std::endl; | |
for(int f = 0; f < 256; f++){ | |
itk::MetaDataDictionary inputDict = image->GetMetaDataDictionary(); | |
WriterType::DictionaryRawPointer dictionary = new WriterType::DictionaryType; | |
CopyDictionary(inputDict, *dictionary); | |
itk::EncapsulateMetaData<std::string>(*dictionary, PATIENT_NAME, "Patient"); | |
itk::EncapsulateMetaData<std::string>(*dictionary, PATIENT_ID, "12345"); | |
itk::EncapsulateMetaData<std::string>(*dictionary, STUDY_INSTANCE_UID, v_StudyUID); | |
std::ostringstream value; | |
value.str(""); | |
value << f; | |
itk::EncapsulateMetaData<std::string>(*dictionary, INSTANCE_NUMBER, value.str()); | |
itk::EncapsulateMetaData<std::string>(*dictionary, SLICE_THICKNESS, "0.2"); | |
itk::EncapsulateMetaData<std::string>(*dictionary, SLICE_SPACING, "0.2"); | |
itk::EncapsulateMetaData<std::string>(*dictionary, PIXEL_SPACING, "0.2\\0.2"); | |
itk::EncapsulateMetaData<std::string>(*dictionary, SERIES_DESCRIPTION, "Some Series"); | |
UC3ImageType::PointType position; | |
UC3ImageType::IndexType index; | |
index[0] = 0; | |
index[1] = 0; | |
index[2] = f; | |
image->TransformIndexToPhysicalPoint(index, position); | |
value.str(""); | |
value << position[0] << "\\" << position[1] << "\\" << position[2]; | |
itk::EncapsulateMetaData<std::string>(*dictionary, IMAGE_POSITION_PATIENT, value.str()); | |
value.str(""); | |
value << position[2]; | |
itk::EncapsulateMetaData<std::string>(*dictionary, SLICE_LOCATION, value.str()); | |
itk::EncapsulateMetaData<std::string>(*dictionary, IMAGE_ORIENTATION_PATIENT, "1\\0\\0\\0\\1\\0"); | |
dictionaryArray.push_back(dictionary); | |
} | |
WriterType::DictionaryRawPointer dict = dictionaryArray[0]; | |
typedef itk::NumericSeriesFileNames NamesGeneratorType; | |
NamesGeneratorType::Pointer nameGenerator = NamesGeneratorType::New(); | |
nameGenerator->SetStartIndex(1); | |
nameGenerator->SetEndIndex(256); | |
nameGenerator->SetIncrementIndex(1); | |
nameGenerator->SetSeriesFormat(output_dir); | |
typedef std::vector<std::string> FileNamesContainer; | |
FileNamesContainer fileNames = nameGenerator->GetFileNames(); | |
typedef itk::GDCMImageIO ImageIOType; | |
ImageIOType::Pointer gdcmImageIO = ImageIOType::New(); | |
WriterType::Pointer writer = WriterType::New(); | |
writer->SetFileNames(nameGenerator->GetFileNames()); | |
writer->SetInput(image); | |
writer->SetImageIO(gdcmImageIO); | |
writer->SetMetaDataDictionaryArray(&dictionaryArray); | |
try | |
{ | |
writer->Update(); | |
} | |
catch (itk::ExceptionObject & e) | |
{ | |
std::cerr << "exception in file writer " << std::endl; | |
std::cerr << e << std::endl; | |
} | |
std::cout << "finished" << std::endl; | |
return EXIT_SUCCESS; | |
} | |
void CopyDictionary (itk::MetaDataDictionary &fromDict, itk::MetaDataDictionary &toDict) | |
{ | |
typedef itk::MetaDataDictionary DictionaryType; | |
DictionaryType::ConstIterator itr = fromDict.Begin(); | |
DictionaryType::ConstIterator end = fromDict.End(); | |
typedef itk::MetaDataObject< std::string > MetaDataStringType; | |
while( itr != end ) | |
{ | |
itk::MetaDataObjectBase::Pointer entry = itr->second; | |
MetaDataStringType::Pointer entryvalue = | |
dynamic_cast<MetaDataStringType *>( entry.GetPointer() ) ; | |
if( entryvalue ) | |
{ | |
std::string tagkey = itr->first; | |
std::string tagvalue = entryvalue->GetMetaDataObjectValue(); | |
itk::EncapsulateMetaData<std::string>(toDict, tagkey, tagvalue); | |
} | |
++itr; | |
} | |
} | |
void PrintDictionary(itk::MetaDataDictionary &dict){ | |
typedef itk::MetaDataDictionary DictionaryType; | |
DictionaryType::ConstIterator itr = dict.Begin(); | |
DictionaryType::ConstIterator end = dict.End(); | |
typedef itk::MetaDataObject< std::string > MetaDataStringType; | |
while( itr != end ) | |
{ | |
itk::MetaDataObjectBase::Pointer entry = itr->second; | |
MetaDataStringType::Pointer entryvalue = | |
dynamic_cast<MetaDataStringType *>( entry.GetPointer() ) ; | |
if( entryvalue ) | |
{ | |
std::cout << itr->first << "\t" << entryvalue->GetMetaDataObjectValue() << std::endl; | |
} | |
++itr; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment