diff --git a/Modules/DiffusionImaging/DiffusionCore/DicomImport/mitkDiffusionDICOMFileReaderHelper.h b/Modules/DiffusionImaging/DiffusionCore/DicomImport/mitkDiffusionDICOMFileReaderHelper.h index 19a333b436..bf774d0571 100644 --- a/Modules/DiffusionImaging/DiffusionCore/DicomImport/mitkDiffusionDICOMFileReaderHelper.h +++ b/Modules/DiffusionImaging/DiffusionCore/DicomImport/mitkDiffusionDICOMFileReaderHelper.h @@ -1,243 +1,243 @@ #ifndef MITKDIFFUSIONDICOMFILEREADERHELPER_H #define MITKDIFFUSIONDICOMFILEREADERHELPER_H #include "mitkDiffusionImage.h" #include "itkImageSeriesReader.h" #include "itkVectorImage.h" #include "itkImageRegionIteratorWithIndex.h" namespace mitk { /** * @brief The MosaicDescriptor struct is a help struct holding the necessary information for * loading a mosaic DICOM file into an MITK file with correct geometry information */ struct MosaicDescriptor { unsigned int nimages; bool slicenormalup; itk::ImageBase<3>::SpacingType spacing; itk::ImageBase<3>::DirectionType direction; float origin[3]; }; class DiffusionDICOMFileReaderHelper { public: typedef std::vector< std::string > StringContainer; typedef std::vector< StringContainer > VolumeFileNamesContainer; template< typename PixelType, unsigned int VecImageDimension> typename itk::VectorImage< PixelType, VecImageDimension >::Pointer LoadToVector( const VolumeFileNamesContainer& filenames //const itk::ImageBase<3>::RegionType requestedRegion ) { typedef itk::Image< PixelType, 3> InputImageType; typedef itk::ImageSeriesReader< InputImageType > SeriesReaderType; typename SeriesReaderType::Pointer probe_reader = SeriesReaderType::New(); probe_reader->SetFileNames( filenames.at(0) ); probe_reader->GenerateOutputInformation(); const itk::ImageBase<3>::RegionType requestedRegion = probe_reader->GetOutput()->GetLargestPossibleRegion(); MITK_INFO << " --- Probe reader : \n" << " Retrieved LPR " << requestedRegion; typedef itk::VectorImage< PixelType, 3 > VectorImageType; typename VectorImageType::Pointer output_image = VectorImageType::New(); output_image->SetNumberOfComponentsPerPixel( filenames.size() ); output_image->SetSpacing( probe_reader->GetOutput()->GetSpacing() ); output_image->SetOrigin( probe_reader->GetOutput()->GetOrigin() ); output_image->SetDirection( probe_reader->GetOutput()->GetDirection() ); output_image->SetLargestPossibleRegion( probe_reader->GetOutput()->GetLargestPossibleRegion() ); output_image->SetBufferedRegion( requestedRegion ); output_image->Allocate(); itk::ImageRegionIterator< VectorImageType > vecIter( output_image, requestedRegion ); VolumeFileNamesContainer::const_iterator volumesFileNamesIter = filenames.begin(); // iterate over the given volumes unsigned int component = 0; while( volumesFileNamesIter != filenames.end() ) { MITK_INFO << " ======== Loading volume " << component+1 << " of " << filenames.size(); typename SeriesReaderType::Pointer volume_reader = SeriesReaderType::New(); volume_reader->SetFileNames( *volumesFileNamesIter ); try { volume_reader->UpdateLargestPossibleRegion(); } catch( const itk::ExceptionObject &e) { mitkThrow() << " ITK Series reader failed : "<< e.what(); } itk::ImageRegionConstIterator< InputImageType > iRCIter ( volume_reader->GetOutput(), volume_reader->GetOutput()->GetLargestPossibleRegion() ); // transfer to vector image iRCIter.GoToBegin(); vecIter.GoToBegin(); while( !iRCIter.IsAtEnd() ) { typename VectorImageType::PixelType vector_pixel = vecIter.Get(); vector_pixel.SetElement( component, iRCIter.Get() ); vecIter.Set( vector_pixel ); ++vecIter; ++iRCIter; } ++volumesFileNamesIter; component++; } return output_image; } /** * Create the vector image for the resulting diffusion image from a mosaic DICOM image set, * The method needs to be provided with the MosaicDescriptor struct to be able to compute the * correct index and to set the geometry information of the image itself. */ template< typename PixelType, unsigned int VecImageDimension> typename itk::VectorImage< PixelType, VecImageDimension >::Pointer LoadMosaicToVector( const VolumeFileNamesContainer& filenames, MosaicDescriptor mosaicInfo ) { typedef itk::Image< PixelType, 3> MosaicImageType; typedef itk::ImageFileReader< MosaicImageType > SingleImageReaderType; // generate output typedef itk::VectorImage< PixelType, 3 > VectorImageType; VolumeFileNamesContainer::const_iterator volumesFileNamesIter = filenames.begin(); // probe the first file to retrieve the size of the 2d image // we need this information to compute the index relation between mosaic and resulting 3d position // but we need it only once typename SingleImageReaderType::Pointer mosaic_probe = SingleImageReaderType::New(); mosaic_probe->SetFileName( (*volumesFileNamesIter).at(0) ); try { mosaic_probe->UpdateLargestPossibleRegion(); } catch( const itk::ExceptionObject &e) { mitkThrow() << " ITK Image file reader failed : "<< e.what(); } typename MosaicImageType::RegionType mosaic_lpr = mosaic_probe->GetOutput()->GetLargestPossibleRegion(); MITK_INFO << " == MOSAIC: " << mosaic_lpr; itk::ImageBase<3>::SizeValueType images_per_row = ceil( sqrt( mosaicInfo.nimages ) ); itk::ImageBase<3>::RegionType requestedRegion; requestedRegion.SetSize( 0, mosaic_lpr.GetSize()[0]/images_per_row); requestedRegion.SetSize( 1, mosaic_lpr.GetSize()[1]/images_per_row); requestedRegion.SetSize( 2, mosaicInfo.nimages); typename VectorImageType::Pointer output_image = VectorImageType::New(); output_image->SetNumberOfComponentsPerPixel( filenames.size() ); /* FIXME!!! The struct currently does not provide the geometry information the loading works as required*/ output_image->SetSpacing( mosaicInfo.spacing ); - /*output_image->SetOrigin( mosaicInfo.origin ); - output_image->SetDirection( mosaicInfo.direction );*/ + output_image->SetOrigin( mosaicInfo.origin ); + //output_image->SetDirection( mosaicInfo.direction );*/ output_image->SetLargestPossibleRegion( requestedRegion ); output_image->SetBufferedRegion( requestedRegion ); output_image->Allocate(); itk::ImageRegionIteratorWithIndex< VectorImageType > vecIter( output_image, requestedRegion ); // hold the image sizes in an extra variable ( used very often ) typename MosaicImageType::SizeValueType dx = requestedRegion.GetSize()[0]; typename MosaicImageType::SizeValueType dy = requestedRegion.GetSize()[1]; // iterate over the given volumes unsigned int component = 0; while( volumesFileNamesIter != filenames.end() ) { MITK_INFO << " ======== Loading volume " << component+1 << " of " << filenames.size(); typename SingleImageReaderType::Pointer mosaic_reader = SingleImageReaderType::New(); mosaic_reader->SetFileName( (*volumesFileNamesIter).at(0) ); try { mosaic_reader->UpdateLargestPossibleRegion(); } catch( const itk::ExceptionObject &e) { mitkThrow() << " ITK Image file reader failed : "<< e.what(); } typename MosaicImageType::Pointer current_mosaic = mosaic_reader->GetOutput(); vecIter.GoToBegin(); while( !vecIter.IsAtEnd() ) { typename VectorImageType::PixelType vector_pixel = vecIter.Get(); typename VectorImageType::IndexType threeD_index = vecIter.GetIndex(); typename MosaicImageType::IndexType mosaic_index; mosaic_index[2] = 1; // first find the corresponding tile in the mosaic // this is defined by the z-position of the vector (3D) image iterator // in x : z_index % #images_in_grid // in y : z_index / #images_in_grid // // the remaining is just computing the correct position in the mosaic, done by // --------- index of (0,0,z) ----- + --- current 2d position --- mosaic_index[0] = (threeD_index[2] % images_per_row) * dx + threeD_index[0] + images_per_row; mosaic_index[1] = (threeD_index[2] / images_per_row) * dy + threeD_index[1]; typename MosaicImageType::PixelType mosaic_pixel = current_mosaic->GetPixel( mosaic_index ); vector_pixel.SetElement( component, mosaic_pixel ); vecIter.Set( vector_pixel ); ++vecIter; } ++volumesFileNamesIter; component++; } return output_image; } }; } #endif // MITKDIFFUSIONDICOMFILEREADERHELPER_H diff --git a/Modules/DiffusionImaging/DiffusionCore/DicomImport/mitkDiffusionHeaderSiemensMosaicDICOMFileReader.cpp b/Modules/DiffusionImaging/DiffusionCore/DicomImport/mitkDiffusionHeaderSiemensMosaicDICOMFileReader.cpp index 9779892b5e..fc3ff94c53 100644 --- a/Modules/DiffusionImaging/DiffusionCore/DicomImport/mitkDiffusionHeaderSiemensMosaicDICOMFileReader.cpp +++ b/Modules/DiffusionImaging/DiffusionCore/DicomImport/mitkDiffusionHeaderSiemensMosaicDICOMFileReader.cpp @@ -1,120 +1,134 @@ #include "mitkDiffusionHeaderSiemensDICOMFileHelper.h" #include "mitkDiffusionHeaderSiemensMosaicDICOMFileReader.h" #include mitk::DiffusionHeaderSiemensMosaicDICOMFileReader::DiffusionHeaderSiemensMosaicDICOMFileReader() : DiffusionHeaderSiemensDICOMFileReader() { } mitk::DiffusionHeaderSiemensMosaicDICOMFileReader ::~DiffusionHeaderSiemensMosaicDICOMFileReader() { } void mitk::DiffusionHeaderSiemensMosaicDICOMFileReader ::RetrieveMosaicInformation(std::string filename) { gdcm::Reader gdcmReader; gdcmReader.SetFileName( filename.c_str() ); gdcmReader.Read(); const gdcm::DataSet& dataset = gdcmReader.GetFile().GetDataSet(); // (0018,0088) DS [2.5] # 4, 1 SpacingBetweenSlices // important for mosaic data std::string spacing_between_slices; if( RevealBinaryTag( gdcm::Tag(0x0018, 0x0088 ), dataset, spacing_between_slices ) ) { std::istringstream ifs_spacing(spacing_between_slices ); ifs_spacing >> this->m_MosaicDescriptor.spacing[2]; } //(0028,0030) DS [2.5\2.5] # 8, 2 PixelSpacing std::string inplace_spacing; if( RevealBinaryTag( gdcm::Tag(0x0028, 0x0030 ), dataset, inplace_spacing ) ) { std::stringstream iplss( inplace_spacing ); std::string buffer; unsigned int idx=0; while( std::getline( iplss, buffer, '\\' ) ) { std::istringstream substream(buffer); substream >> this->m_MosaicDescriptor.spacing[idx++]; } } + // (0020,0032) DS [-802.51815986633\-801.18644070625\-30.680992126465] # 50, 3 ImagePositionPatient + std::string position_patient; + if( RevealBinaryTag( gdcm::Tag( 0x0028, 0x0032), dataset, position_patient) ) + { + std::stringstream ppass( inplace_spacing ); + std::string buffer; + unsigned int idx=0; + while( std::getline( ppass, buffer, '\\' ) ) + { + std::istringstream substream(buffer); + substream >> this->m_MosaicDescriptor.origin[idx++]; + } + } + } bool mitk::DiffusionHeaderSiemensMosaicDICOMFileReader ::ReadDiffusionHeader(std::string filename) { gdcm::Reader gdcmReader; gdcmReader.SetFileName( filename.c_str() ); gdcmReader.Read(); MITK_INFO << " -- Analyzing: " << filename; const gdcm::DataSet& dataset = gdcmReader.GetFile().GetDataSet(); const gdcm::Tag t_sie_diffusion( 0x0029,0x1010 ); std::string siemens_diffusionheader_str; if( RevealBinaryTag( t_sie_diffusion, dataset, siemens_diffusionheader_str ) ) { mitk::DiffusionImageMosaicDICOMHeaderInformation header_values; this->ExtractSiemensDiffusionTagInformation( siemens_diffusionheader_str, header_values ); mitk::SiemensDiffusionHeaderType hformat = GetHeaderType( siemens_diffusionheader_str ); Siemens_Header_Format specs = this->m_SiemensFormatsCollection.at( hformat ); std::string::size_type tag_position = siemens_diffusionheader_str.find( "NumberOfImagesInMosaic", 0 ); if( tag_position != std::string::npos ) { std::vector value_array; ParseInputString( siemens_diffusionheader_str.substr( tag_position, siemens_diffusionheader_str.size() - tag_position + 1 ), value_array, specs ); header_values.n_images = value_array[0]; this->m_MosaicDescriptor.nimages = value_array[0]; } tag_position = siemens_diffusionheader_str.find("SliceNormalVector", 0); if( tag_position != std::string::npos ) { std::vector value_array; ParseInputString( siemens_diffusionheader_str.substr( tag_position, siemens_diffusionheader_str.size() - tag_position + 1 ), value_array, specs ); MITK_DEBUG << "SliceNormal"; for( unsigned int i=0; i 2 ) { header_values.slicenormalup = (value_array[2] > 0); this->m_MosaicDescriptor.slicenormalup = ( value_array[2] > 0); } } m_HeaderInformationList.push_back( header_values ); } return (m_HeaderInformationList.size() > 0); }