X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=tools%2FclitkDicom2Image.cxx;h=b3bcc902de0699f224e2047f9922f4f0f057bc29;hb=e4970a5c24b53d6842e4f8f02d63b957f03bfbba;hp=a6323da9de5ba6ad89c75ab8372049ef569b3200;hpb=c45f077f35553cdc72396626ac601080093d273b;p=clitk.git diff --git a/tools/clitkDicom2Image.cxx b/tools/clitkDicom2Image.cxx index a6323da..b3bcc90 100644 --- a/tools/clitkDicom2Image.cxx +++ b/tools/clitkDicom2Image.cxx @@ -17,6 +17,7 @@ ===========================================================================**/ // clitk includes +#include "clitkIO.h" #include "clitkDicom2Image_ggo.h" #include "clitkCommon.h" #include "clitkImageCommon.h" @@ -25,7 +26,7 @@ #include #include #include -#if GDCM_MAJOR_VERSION == 2 +#if GDCM_MAJOR_VERSION >= 2 #include #include #include @@ -38,6 +39,8 @@ int main(int argc, char * argv[]) { // init command line GGO(clitkDicom2Image, args_info); + CLITK_INIT; + std::vector input_files; ///if std_input is given, read the input files from stdin if (args_info.std_input_given) { @@ -55,32 +58,54 @@ int main(int argc, char * argv[]) //=========================================== /// Get slices locations ... - int series_number = -1; - std::set series_numbers; - std::map< int, std::vector > theorigin; - std::map< int, std::vector > sliceLocations; - std::map< int, std::vector > seriesFiles; + std::string series_UID = ""; + std::set series_UIDs; + std::map< std::string, std::vector > theorigin; + std::map< std::string, std::vector > theorientation; + std::map< std::string, std::vector > sliceLocations; + std::map< std::string, std::vector > seriesFiles; +#if GDCM_MAJOR_VERSION >= 2 + if (args_info.verbose_flag) + std::cout << "Using GDCM-2.x" << std::endl; +#else + if (args_info.verbose_flag) { + std::cout << "Not using GDCM-2.x" << std::endl; + std::cout<< "The image orientation is not supported with this version of GDCM" <= 2 gdcm::Reader hreader; hreader.SetFileName(input_files[i].c_str()); hreader.Read(); gdcm::DataSet& ds = hreader.GetFile().GetDataSet(); if (args_info.extract_series_flag) { - gdcm::Attribute<0x20,0x11> series_number_att; - series_number_att.SetFromDataSet(hreader.GetFile().GetDataSet()); - series_number = series_number_att.GetValue(); + gdcm::Attribute<0x20,0x000e> series_UID_att; + series_UID_att.SetFromDataSet(ds); + series_UID = series_UID_att.GetValue(); } - - series_numbers.insert(series_number); - theorigin[series_number] = gdcm::ImageHelper::GetOriginValue(hreader.GetFile()); - sliceLocations[series_number].push_back(theorigin[series_number][2]); - seriesFiles[series_number].push_back(input_files[i]); - + + series_UIDs.insert(series_UID); + theorigin[series_UID] = gdcm::ImageHelper::GetOriginValue(hreader.GetFile()); + theorientation[series_UID] = gdcm::ImageHelper::GetDirectionCosinesValue(hreader.GetFile()); + if (args_info.patientSystem_flag) { + double n1 = theorientation[series_UID][1]*theorientation[series_UID][5]- + theorientation[series_UID][2]*theorientation[series_UID][4]; + double n2 = theorientation[series_UID][3]*theorientation[series_UID][2]- + theorientation[series_UID][5]*theorientation[series_UID][0]; + double n3 = theorientation[series_UID][0]*theorientation[series_UID][4]- + theorientation[series_UID][1]*theorientation[series_UID][3]; + double sloc = theorigin[series_UID][0]*n1+ + theorigin[series_UID][1]*n2+ + theorigin[series_UID][2]*n3; + sliceLocations[series_UID].push_back(sloc); + } else + sliceLocations[series_UID].push_back(theorigin[series_UID][2]); + seriesFiles[series_UID].push_back(input_files[i]); + gdcm::Attribute<0x28, 0x100> pixel_size; pixel_size.SetFromDataSet(ds); /* if (pixel_size.GetValue() != 16) @@ -91,24 +116,22 @@ int main(int argc, char * argv[]) } */ #else - if (args_info.verbose_flag) - std::cout << "Not using GDCM-2.x" << std::endl; gdcm::File *header = new gdcm::File(); header->SetFileName(input_files[i]); header->SetMaxSizeLoadEntry(16384); // required ? header->Load(); if (args_info.extract_series_flag) { - series_number = atoi(header->GetEntryValue(0x20,0x11).c_str()); + series_UID = header->GetEntryValue(0x20,0x000e).c_str(); } - - series_numbers.insert(series_number); - theorigin[series_number].resize(3); - theorigin[series_number][0] = header->GetXOrigin(); - theorigin[series_number][1] = header->GetYOrigin(); - theorigin[series_number][2] = header->GetZOrigin(); - sliceLocations[series_number].push_back(theorigin[series_number][2]); - seriesFiles[series_number].push_back(input_files[i]); + + series_UIDs.insert(series_UID); + theorigin[series_UID].resize(3); + theorigin[series_UID][0] = header->GetXOrigin(); + theorigin[series_UID][1] = header->GetYOrigin(); + theorigin[series_UID][2] = header->GetZOrigin(); + sliceLocations[series_UID].push_back(theorigin[series_UID][2]); + seriesFiles[series_UID].push_back(input_files[i]); /*if (header->GetPixelSize() != 2) { std::cerr << "Pixel type 2 bytes ! " << std::endl; std::cerr << "In file " << input_files[i] << std::endl; @@ -120,14 +143,14 @@ int main(int argc, char * argv[]) //=========================================== // Sort slices locations ... - std::set::iterator sn = series_numbers.begin(); - while ( sn != series_numbers.end() ) { + std::set::iterator sn = series_UIDs.begin(); + while ( sn != series_UIDs.end() ) { std::vector locs = sliceLocations[*sn]; std::vector origin = theorigin[*sn]; std::vector files = seriesFiles[*sn]; std::vector sliceIndex; clitk::GetSortedIndex(locs, sliceIndex); - if (args_info.verboseSliceLocation_flag) { + if (args_info.verbose_flag) { std::cout << locs[sliceIndex[0]] << " -> " << sliceIndex[0] << " / " << 0 << " => " << "0 mm " @@ -178,12 +201,13 @@ int main(int argc, char * argv[]) // Read write serie vvImageReader::Pointer reader = vvImageReader::New(); reader->SetInputFilenames(sorted_files); + reader->SetPatientCoordinateSystem(args_info.patientSystem_flag); reader->Update(vvImageReader::DICOM); if (reader->GetLastError().size() != 0) { std::cerr << reader->GetLastError() << std::endl; return 1; } - + vvImage::Pointer image = reader->GetOutput(); vtkImageData* vtk_image = image->GetFirstVTKImageData(); vtkImageChangeInformation* modifier = vtkImageChangeInformation::New(); @@ -205,22 +229,28 @@ int main(int argc, char * argv[]) } std::string outfile; - if (series_numbers.size() == 1) + if (series_UIDs.size() == 1) outfile = args_info.output_arg; else { std::ostringstream name; - name << *sn << "_" << args_info.output_arg; + std::vector directory = clitk::SplitFilename(args_info.output_arg); + if (directory.size() == 2) + name << directory[0] << "/" << *sn << "_" << directory[1]; + else + name << *sn << "_" << args_info.output_arg; outfile = name.str(); } vvImageWriter::Pointer writer = vvImageWriter::New(); writer->SetInput(image); + if (args_info.patientSystem_flag && !image->GetTransform().empty()) + writer->SetSaveTransform(true); writer->SetOutputFileName(outfile); writer->Update(); modifier->Delete(); - + sn++; } - + return 0; }