diff --git a/Modules/DiffusionImaging/IODataStructures/TbssImages/mitkNrrdTbssImageReader.cpp b/Modules/DiffusionImaging/IODataStructures/TbssImages/mitkNrrdTbssImageReader.cpp index b48b2211c8..07d5b336c1 100644 --- a/Modules/DiffusionImaging/IODataStructures/TbssImages/mitkNrrdTbssImageReader.cpp +++ b/Modules/DiffusionImaging/IODataStructures/TbssImages/mitkNrrdTbssImageReader.cpp @@ -1,310 +1,316 @@ /*========================================================================= Program: Medical Imaging & Interaction Toolkit Language: C++ Date: $Date: 2009-07-14 19:11:20 +0200 (Tue, 14 Jul 2009) $ Version: $Revision: 18127 $ Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. See MITKCopyright.txt or http://www.mitk.org/copyright.html for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notices for more information. =========================================================================*/ #ifndef __mitkNrrdTbssImageReader_cpp #define __mitkNrrdTbssImageReader_cpp #include "mitkNrrdTbssImageReader.h" #include "itkImageFileReader.h" #include "itkMetaDataObject.h" #include "itkNrrdImageIO.h" #include "itkNiftiImageIO.h" #include #include #include #include "itksys/SystemTools.hxx" namespace mitk { void NrrdTbssImageReader ::GenerateData() { // Since everything is completely read in GenerateOutputInformation() it is stored // in a cache variable. A timestamp is associated. // If the timestamp of the cache variable is newer than the MTime, we only need to // assign the cache variable to the DataObject. // Otherwise, the tree must be read again from the file and OuputInformation must // be updated! if ( ( ! m_OutputCache ) || ( this->GetMTime( ) > m_CacheTime.GetMTime( ) ) ) { this->GenerateOutputInformation(); itkWarningMacro("Cache regenerated!"); } if (!m_OutputCache) { itkWarningMacro("Tree cache is empty!") } int vecsize = m_OutputCache->GetImage()->GetVectorLength(); static_cast(this->GetOutput()) ->SetImage(m_OutputCache->GetImage()); static_cast(this->GetOutput()) ->SetGroupInfo(m_OutputCache->GetGroupInfo()); static_cast(this->GetOutput()) ->InitializeFromVectorImage(); - } void NrrdTbssImageReader ::GenerateOutputInformation() { OutputType::Pointer outputForCache = OutputType::New(); if ( m_FileName == "") { throw itk::ImageFileReaderException(__FILE__, __LINE__, "Sorry, the filename to be read is empty!"); } else { try { const std::string& locale = "C"; const std::string& currLocale = setlocale( LC_ALL, NULL ); if ( locale.compare(currLocale)!=0 ) { try { MITK_INFO << " ** Changing locale from " << setlocale(LC_ALL, NULL) << " to '" << locale << "'"; setlocale(LC_ALL, locale.c_str()); } catch(...) { MITK_INFO << "Could not set locale " << locale; } } MITK_INFO << "NrrdTbssImageReader READING IMAGE INFORMATION"; ImageType::Pointer img; std::string ext = itksys::SystemTools::GetFilenameLastExtension(m_FileName); ext = itksys::SystemTools::LowerCase(ext); if (ext == ".tbss") { typedef itk::ImageFileReader FileReaderType; FileReaderType::Pointer reader = FileReaderType::New(); reader->SetFileName(this->m_FileName); itk::NrrdImageIO::Pointer io = itk::NrrdImageIO::New(); reader->SetImageIO(io); + reader->Update(); img = reader->GetOutput(); } MITK_INFO << "NrrdTbssImageReader READING HEADER INFORMATION"; if (ext == ".tbss") { itk::MetaDataDictionary imgMetaDictionary = img->GetMetaDataDictionary(); std::vector imgMetaKeys = imgMetaDictionary.GetKeys(); std::vector::const_iterator itKey = imgMetaKeys.begin(); std::string metaString; //int numberOfGradientImages = 0; - std::string name; - int n; + std::string measurementInfo; + std::vector< std::pair > groups; for (; itKey != imgMetaKeys.end(); itKey ++) { itk::ExposeMetaData (imgMetaDictionary, *itKey, metaString); + + MITK_INFO << *itKey << " ---> " << metaString; + if (itKey->find("Group_index") != std::string::npos) { - MITK_INFO << *itKey << " ---> " << metaString; - std::vector tokens; this->Tokenize(metaString, tokens, " "); if(tokens.size()==2) { std::cout << tokens.at(0) << " " << tokens.at(1) << std::endl; std::pair< std::string, int > p; p.first = tokens.at(0); std::string s = tokens.at(1); p.second = atoi(tokens.at(1).c_str()); groups.push_back(p); } } + else if(itKey->find("Measurement info")) + { + measurementInfo = metaString; + } + } outputForCache->SetGroupInfo(groups); - + outputForCache->SetMeasurementInfo(measurementInfo); } // This call updates the output information of the associated VesselTreeData outputForCache->SetImage(img); // outputForCache->SetB_Value(m_B_Value); //outputForCache->SetDirections(m_DiffusionVectors); // outputForCache->SetOriginalDirections(m_OriginalDiffusionVectors); // outputForCache->SetMeasurementFrame(m_MeasurementFrame); // Since we have already read the tree, we can store it in a cache variable // so that it can be assigned to the DataObject in GenerateData(); m_OutputCache = outputForCache; m_CacheTime.Modified(); try { MITK_INFO << " ** Changing locale back from " << setlocale(LC_ALL, NULL) << " to '" << currLocale << "'"; setlocale(LC_ALL, currLocale.c_str()); } catch(...) { MITK_INFO << "Could not reset locale " << currLocale; } } catch(std::exception& e) { MITK_INFO << "Std::Exception while reading file!!"; MITK_INFO << e.what(); throw itk::ImageFileReaderException(__FILE__, __LINE__, e.what()); } catch(...) { MITK_INFO << "Exception while reading file!!"; throw itk::ImageFileReaderException(__FILE__, __LINE__, "Sorry, an error occurred while reading the requested vessel tree file!"); } } } const char* NrrdTbssImageReader ::GetFileName() const { return m_FileName.c_str(); } void NrrdTbssImageReader ::SetFileName(const char* aFileName) { m_FileName = aFileName; } const char* NrrdTbssImageReader ::GetFilePrefix() const { return m_FilePrefix.c_str(); } void NrrdTbssImageReader ::SetFilePrefix(const char* aFilePrefix) { m_FilePrefix = aFilePrefix; } const char* NrrdTbssImageReader ::GetFilePattern() const { return m_FilePattern.c_str(); } void NrrdTbssImageReader ::SetFilePattern(const char* aFilePattern) { m_FilePattern = aFilePattern; } bool NrrdTbssImageReader ::CanReadFile(const std::string filename, const std::string filePrefix, const std::string filePattern) { // First check the extension if( filename == "" ) return false; // check if image is serie if( filePattern != "" && filePrefix != "" ) return false; std::string ext = itksys::SystemTools::GetFilenameLastExtension(filename); ext = itksys::SystemTools::LowerCase(ext); if (ext == ".tbss") { itk::NrrdImageIO::Pointer io = itk::NrrdImageIO::New(); typedef itk::ImageFileReader FileReaderType; FileReaderType::Pointer reader = FileReaderType::New(); reader->SetImageIO(io); reader->SetFileName(filename); try { reader->Update(); } catch(itk::ExceptionObject e) { MITK_INFO << e.GetDescription(); return false; } /* typename ImageType::Pointer img = reader->GetOutput(); itk::MetaDataDictionary imgMetaDictionary = img->GetMetaDataDictionary(); std::vector imgMetaKeys = imgMetaDictionary.GetKeys(); std::vector::const_iterator itKey = imgMetaKeys.begin(); std::string metaString; for (; itKey != imgMetaKeys.end(); itKey ++) { itk::ExposeMetaData (imgMetaDictionary, *itKey, metaString); if (itKey->find("tbss") != std::string::npos) { if (metaString.find("ROI") != std::string::npos) { return true; } } } } */ // return false; return true; } return false; } } //namespace MITK #endif diff --git a/Modules/DiffusionImaging/IODataStructures/TbssImages/mitkNrrdTbssRoiImageReader.cpp b/Modules/DiffusionImaging/IODataStructures/TbssImages/mitkNrrdTbssRoiImageReader.cpp index 6e4d545051..f9bc87b82d 100644 --- a/Modules/DiffusionImaging/IODataStructures/TbssImages/mitkNrrdTbssRoiImageReader.cpp +++ b/Modules/DiffusionImaging/IODataStructures/TbssImages/mitkNrrdTbssRoiImageReader.cpp @@ -1,353 +1,357 @@ /*========================================================================= Program: Medical Imaging & Interaction Toolkit Language: C++ Date: $Date: 2009-07-14 19:11:20 +0200 (Tue, 14 Jul 2009) $ Version: $Revision: 18127 $ Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. See MITKCopyright.txt or http://www.mitk.org/copyright.html for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notices for more information. =========================================================================*/ #ifndef __mitkNrrdTbssRoiReader_cpp #define __mitkNrrdTbssRoiReader_cpp #include "mitkNrrdTbssRoiImageReader.h" #include "itkImageFileReader.h" #include "itkMetaDataObject.h" #include "itkNrrdImageIO.h" #include "itkNiftiImageIO.h" #include #include #include #include "itksys/SystemTools.hxx" namespace mitk { void NrrdTbssRoiImageReader ::GenerateData() { try { // Change locale if needed const std::string& locale = "C"; const std::string& currLocale = setlocale( LC_ALL, NULL ); if ( locale.compare(currLocale)!=0 ) { try { MITK_INFO << " ** Changing locale from " << setlocale(LC_ALL, NULL) << " to '" << locale << "'"; setlocale(LC_ALL, locale.c_str()); } catch(...) { MITK_INFO << "Could not set locale " << locale; } } // READ IMAGE INFORMATION const unsigned int MINDIM = 3; const unsigned int MAXDIM = 4; MITK_INFO << "loading " << m_FileName << " via mitk::NrrdTbssImageReader... " << std::endl; // Check to see if we can read the file given the name or prefix if ( m_FileName == "" ) { itkWarningMacro( << "Filename is empty!" ) - return ; + return; } itk::NrrdImageIO::Pointer imageIO = itk::NrrdImageIO::New(); imageIO->SetFileName( m_FileName.c_str() ); imageIO->ReadImageInformation(); unsigned int ndim = imageIO->GetNumberOfDimensions(); if ( ndim < MINDIM || ndim > MAXDIM ) { itkWarningMacro( << "Sorry, only dimensions 3 is supported. The given file has " << ndim << " dimensions!" ) return; } itk::ImageIORegion ioRegion( ndim ); itk::ImageIORegion::SizeType ioSize = ioRegion.GetSize(); itk::ImageIORegion::IndexType ioStart = ioRegion.GetIndex(); unsigned int dimensions[ MAXDIM ]; dimensions[ 0 ] = 0; dimensions[ 1 ] = 0; dimensions[ 2 ] = 0; dimensions[ 3 ] = 0; float spacing[ MAXDIM ]; spacing[ 0 ] = 1.0f; spacing[ 1 ] = 1.0f; spacing[ 2 ] = 1.0f; spacing[ 3 ] = 1.0f; Point3D origin; origin.Fill(0); unsigned int i; for ( i = 0; i < ndim ; ++i ) { ioStart[ i ] = 0; ioSize[ i ] = imageIO->GetDimensions( i ); if(iGetDimensions( i ); spacing[ i ] = imageIO->GetSpacing( i ); if(spacing[ i ] <= 0) spacing[ i ] = 1.0f; } if(i<3) { origin[ i ] = imageIO->GetOrigin( i ); } } ioRegion.SetSize( ioSize ); ioRegion.SetIndex( ioStart ); MITK_INFO << "ioRegion: " << ioRegion << std::endl; imageIO->SetIORegion( ioRegion ); void* buffer = new unsigned char[imageIO->GetImageSizeInBytes()]; imageIO->Read( buffer ); //mitk::Image::Pointer static_cast(this->GetOutput())image = mitk::Image::New(); if((ndim==4) && (dimensions[3]<=1)) ndim = 3; if((ndim==3) && (dimensions[2]<=1)) ndim = 2; mitk::PixelType pixelType( imageIO->GetComponentTypeInfo(), imageIO->GetNumberOfComponents(), imageIO->GetPixelType() ); static_cast(this->GetOutput())->Initialize( pixelType, ndim, dimensions ); static_cast(this->GetOutput())->SetImportChannel( buffer, 0, Image::ManageMemory ); // access direction of itk::Image and include spacing mitk::Matrix3D matrix; matrix.SetIdentity(); unsigned int j, itkDimMax3 = (ndim >= 3? 3 : ndim); for ( i=0; i < itkDimMax3; ++i) for( j=0; j < itkDimMax3; ++j ) matrix[i][j] = imageIO->GetDirection(j)[i]; // re-initialize PlaneGeometry with origin and direction PlaneGeometry* planeGeometry = static_cast (static_cast (this->GetOutput())->GetSlicedGeometry(0)->GetGeometry2D(0)); planeGeometry->SetOrigin(origin); planeGeometry->GetIndexToWorldTransform()->SetMatrix(matrix); // re-initialize SlicedGeometry3D SlicedGeometry3D* slicedGeometry = static_cast(this->GetOutput())->GetSlicedGeometry(0); slicedGeometry->InitializeEvenlySpaced(planeGeometry, static_cast(this->GetOutput())->GetDimension(2)); slicedGeometry->SetSpacing(spacing); // re-initialize TimeSlicedGeometry static_cast(this->GetOutput())->GetTimeSlicedGeometry()->InitializeEvenlyTimed(slicedGeometry, static_cast(this->GetOutput())->GetDimension(3)); buffer = NULL; MITK_INFO << "number of image components: "<< static_cast(this->GetOutput())->GetPixelType().GetNumberOfComponents() << std::endl; // READ TBSS HEADER INFORMATION ImageType::Pointer img; std::string ext = itksys::SystemTools::GetFilenameLastExtension(m_FileName); ext = itksys::SystemTools::LowerCase(ext); if (ext == ".roi") { typedef itk::ImageFileReader FileReaderType; FileReaderType::Pointer reader = FileReaderType::New(); reader->SetFileName(this->m_FileName); reader->SetImageIO(imageIO); reader->Update(); img = reader->GetOutput(); + static_cast(this->GetOutput())->SetImage(img); + itk::MetaDataDictionary imgMetaDictionary = img->GetMetaDataDictionary(); ReadRoiInfo(imgMetaDictionary); + + } // RESET LOCALE try { MITK_INFO << " ** Changing locale back from " << setlocale(LC_ALL, NULL) << " to '" << currLocale << "'"; setlocale(LC_ALL, currLocale.c_str()); } catch(...) { MITK_INFO << "Could not reset locale " << currLocale; } MITK_INFO << "...finished!" << std::endl; } catch(std::exception& e) { MITK_INFO << "Std::Exception while reading file!!"; MITK_INFO << e.what(); throw itk::ImageFileReaderException(__FILE__, __LINE__, e.what()); } catch(...) {http://www.wetter.com/deutschland/heidelberg/DE0004329.html MITK_INFO << "Exception while reading file!!"; throw itk::ImageFileReaderException(__FILE__, __LINE__, "Sorry, an error occurred while reading the requested vessel tree file!"); } } void NrrdTbssRoiImageReader ::ReadRoiInfo(itk::MetaDataDictionary dict) { std::vector imgMetaKeys = dict.GetKeys(); std::vector::const_iterator itKey = imgMetaKeys.begin(); std::string metaString; std::vector< itk::Index<3> > roi; for (; itKey != imgMetaKeys.end(); itKey ++) { double x,y,z; itk::Index<3> ix; itk::ExposeMetaData (dict, *itKey, metaString); if (itKey->find("ROI_index") != std::string::npos) { MITK_INFO << *itKey << " ---> " << metaString; sscanf(metaString.c_str(), "%lf %lf %lf\n", &x, &y, &z); ix[0] = x; ix[1] = y; ix[2] = z; roi.push_back(ix); } else if(itKey->find("preprocessed FA") != std::string::npos) { MITK_INFO << *itKey << " ---> " << metaString; static_cast(this->GetOutput())->SetPreprocessedFA(true); static_cast(this->GetOutput())->SetPreprocessedFAFile(metaString); } // Name of structure if (itKey->find("structure") != std::string::npos) { MITK_INFO << *itKey << " ---> " << metaString; static_cast(this->GetOutput())->SetStructure(metaString); } } static_cast(this->GetOutput())->SetRoi(roi); } const char* NrrdTbssRoiImageReader ::GetFileName() const { return m_FileName.c_str(); } void NrrdTbssRoiImageReader ::SetFileName(const char* aFileName) { m_FileName = aFileName; } const char* NrrdTbssRoiImageReader ::GetFilePrefix() const { return m_FilePrefix.c_str(); } void NrrdTbssRoiImageReader ::SetFilePrefix(const char* aFilePrefix) { m_FilePrefix = aFilePrefix; } const char* NrrdTbssRoiImageReader ::GetFilePattern() const { return m_FilePattern.c_str(); } void NrrdTbssRoiImageReader ::SetFilePattern(const char* aFilePattern) { m_FilePattern = aFilePattern; } bool NrrdTbssRoiImageReader ::CanReadFile(const std::string filename, const std::string filePrefix, const std::string filePattern) { // First check the extension if( filename == "" ) return false; // check if image is serie if( filePattern != "" && filePrefix != "" ) return false; std::string ext = itksys::SystemTools::GetFilenameLastExtension(filename); ext = itksys::SystemTools::LowerCase(ext); if (ext == ".roi") { itk::NrrdImageIO::Pointer io = itk::NrrdImageIO::New(); typedef itk::ImageFileReader FileReaderType; FileReaderType::Pointer reader = FileReaderType::New(); reader->SetImageIO(io); reader->SetFileName(filename); try { reader->Update(); } catch(itk::ExceptionObject e) { MITK_INFO << e.GetDescription(); return false; } return true; } return false; } } //namespace MITK #endif diff --git a/Modules/DiffusionImaging/IODataStructures/TbssImages/mitkNrrdTbssRoiImageWriter.cpp b/Modules/DiffusionImaging/IODataStructures/TbssImages/mitkNrrdTbssRoiImageWriter.cpp index 120db54b85..b7cd82ee2a 100644 --- a/Modules/DiffusionImaging/IODataStructures/TbssImages/mitkNrrdTbssRoiImageWriter.cpp +++ b/Modules/DiffusionImaging/IODataStructures/TbssImages/mitkNrrdTbssRoiImageWriter.cpp @@ -1,140 +1,143 @@ /*========================================================================= Program: Medical Imaging & Interaction Toolkit Language: C++ Date: $Date: 2008-12-10 18:05:13 +0100 (Mi, 10 Dez 2008) $ Version: $Revision: 15922 $ Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. See MITKCopyright.txt or http://www.mitk.org/copyright.html for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notices for more information. =========================================================================*/ #ifndef __mitkNrrdTbssRoiImageWriter__cpp #define __mitkNrrdTbssRoiImageWriter__cpp #include "mitkNrrdTbssRoiImageWriter.h" #include "itkMetaDataDictionary.h" #include "itkMetaDataObject.h" #include "itkNrrdImageIO.h" //#include "itkNiftiImageIO.h" #include "itkImageFileWriter.h" #include "itksys/SystemTools.hxx" #include #include mitk::NrrdTbssRoiImageWriter::NrrdTbssRoiImageWriter() : m_FileName(""), m_FilePrefix(""), m_FilePattern(""), m_Success(false) { this->SetNumberOfRequiredInputs( 1 ); } mitk::NrrdTbssRoiImageWriter::~NrrdTbssRoiImageWriter() {} void mitk::NrrdTbssRoiImageWriter::GenerateData() { m_Success = false; InputType* input = this->GetInput(); + if (input == NULL) { itkWarningMacro(<<"Sorry, input to NrrdTbssImageWriter is NULL!") return; } if ( m_FileName == "" ) { itkWarningMacro( << "Sorry, filename has not been set!" ) return ; } - - itk::Image::Pointer img = input->GetImage(); - - char keybuffer[512]; char valbuffer[512]; std::vector< itk::Index<3> > roi = input->GetRoi(); std::vector< itk::Index<3> >::iterator it = roi.begin(); int i=0; while(it != roi.end()) { itk::Index<3> ix = *it; sprintf( keybuffer, "ROI_index_%04d", i ); sprintf( valbuffer, "%1d %1d %1d", ix[0],ix[1],ix[2]); std::cout << valbuffer << std::endl; - itk::EncapsulateMetaData< std::string >(input->GetImage()->GetMetaDataDictionary(),std::string(keybuffer),std::string(valbuffer)); + //input->GetImage()->GetMetaDataDictionary(); + + itk::EncapsulateMetaData< std::string >(input->GetImage()->GetMetaDataDictionary(), std::string(keybuffer), std::string(valbuffer)); + it++; ++i; } - typedef itk::Image ImageType; + std::string structure = input->GetStructure(); + itk::EncapsulateMetaData< std::string >(input->GetImage()->GetMetaDataDictionary(), "structure", structure); + typedef itk::Image ImageType; + ImageType::Pointer img = input->GetImage(); itk::NrrdImageIO::Pointer io = itk::NrrdImageIO::New(); io->SetFileType( itk::ImageIOBase::Binary ); io->UseCompressionOn(); typedef itk::ImageFileWriter WriterType; WriterType::Pointer nrrdWriter = WriterType::New(); nrrdWriter->UseInputMetaDataDictionaryOn(); nrrdWriter->SetInput( img ); nrrdWriter->SetImageIO(io); nrrdWriter->SetFileName(m_FileName); // nrrdWriter->UseCompressionOn(); nrrdWriter->SetImageIO(io); try { nrrdWriter->Update(); } catch (itk::ExceptionObject e) { std::cout << e << std::endl; } m_Success = true; } void mitk::NrrdTbssRoiImageWriter::SetInput( InputType* tbssVol ) { this->ProcessObject::SetNthInput( 0, tbssVol ); } mitk::TbssRoiImage* mitk::NrrdTbssRoiImageWriter::GetInput() { if ( this->GetNumberOfInputs() < 1 ) { return NULL; } else { return dynamic_cast ( this->ProcessObject::GetInput( 0 ) ); } } std::vector mitk::NrrdTbssRoiImageWriter::GetPossibleFileExtensions() { std::vector possibleFileExtensions; possibleFileExtensions.push_back(".roi"); return possibleFileExtensions; } #endif //__mitkNrrdTbssRoiImageWriter__cpp diff --git a/Modules/DiffusionImaging/IODataStructures/TbssImages/mitkNrrdTbssRoiImageWriter.h b/Modules/DiffusionImaging/IODataStructures/TbssImages/mitkNrrdTbssRoiImageWriter.h index 8da997c75e..55ad4270e3 100644 --- a/Modules/DiffusionImaging/IODataStructures/TbssImages/mitkNrrdTbssRoiImageWriter.h +++ b/Modules/DiffusionImaging/IODataStructures/TbssImages/mitkNrrdTbssRoiImageWriter.h @@ -1,139 +1,141 @@ /*========================================================================= Program: Medical Imaging & Interaction Toolkit Language: C++ Date: $Date: 2008-08-27 17:18:46 +0200 (Mi, 27 Aug 2008) $ Version: $Revision: 15096 $ Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. See MITKCopyright.txt or http://www.mitk.org/copyright.html for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notices for more information. =========================================================================*/ #ifndef _MITK_NRRDTBSSROIVOL_WRITER__H_ #define _MITK_NRRDTBSSROIVOL_WRITER__H_ #include #include #include #include #include "MitkDiffusionImagingExports.h" namespace mitk { /** * Writes diffusion volumes to a file * @ingroup Process */ class MitkDiffusionImaging_EXPORT NrrdTbssRoiImageWriter : public mitk::FileWriterWithInformation { public: + typedef itk::Image ImageType; + mitkClassMacro( NrrdTbssRoiImageWriter, mitk::FileWriterWithInformation ) mitkWriterMacro itkNewMacro( Self ) typedef mitk::TbssRoiImage InputType; /** * Sets the filename of the file to write. * @param FileName the nameInputType of the file to write. */ itkSetStringMacro( FileName ); /** * @returns the name of the file to be written to disk. */ itkGetStringMacro( FileName ); /** * @warning multiple write not (yet) supported */ itkSetStringMacro( FilePrefix ); /** * @warning multiple write not (yet) supported */ itkGetStringMacro( FilePrefix ); /** * @warning multiple write not (yet) supported */ itkSetStringMacro( FilePattern ); /** * @warning multiple write not (yet) supported */ itkGetStringMacro( FilePattern ); /**image * Sets the input object for the filter. * @param input the diffusion volumes to write to file. */ void SetInput( InputType* input ); /** * @returns the 0'th input object of the filter. */ InputType* GetInput(); /** * Returns false if an error happened during writing */ itkGetMacro( Success, bool ); /** * @return possible file extensions for the data type associated with the writer */ virtual std::vector GetPossibleFileExtensions(); // FileWriterWithInformation methods virtual const char * GetDefaultFilename() { return "TbssRoiImage.roi"; } virtual const char * GetFileDialogPattern() { return "Tbss Roi Images (*.roi)"; } virtual const char * GetDefaultExtension() { return ".roi"; } virtual bool CanWriteBaseDataType(BaseData::Pointer data) { return (dynamic_cast(data.GetPointer()) != NULL); } virtual void DoWrite(BaseData::Pointer data) { if (CanWriteBaseDataType(data)) { this->SetInput(dynamic_cast(data.GetPointer())); this->Update(); } } protected: NrrdTbssRoiImageWriter(); virtual ~NrrdTbssRoiImageWriter(); virtual void GenerateData(); std::string m_FileName; std::string m_FilePrefix; std::string m_FilePattern; bool m_Success; }; } // end of namespace mitk //#include "mitkNrrdTbssRoiImageWriter.cpp" #endif diff --git a/Modules/DiffusionImaging/IODataStructures/TbssImages/mitkTbssImporter.cpp b/Modules/DiffusionImaging/IODataStructures/TbssImages/mitkTbssImporter.cpp index 0b72c2ff5f..0c0d255150 100644 --- a/Modules/DiffusionImaging/IODataStructures/TbssImages/mitkTbssImporter.cpp +++ b/Modules/DiffusionImaging/IODataStructures/TbssImages/mitkTbssImporter.cpp @@ -1,203 +1,158 @@ /*========================================================================= Program: Medical Imaging & Interaction Toolkit Language: C++ Date: $Date: 2009-07-14 19:11:20 +0200 (Tue, 14 Jul 2009) $ Version: $Revision: 18127 $ Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. See MITKCopyright.txt or http://www.mitk.org/copyright.html for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notices for more information. =========================================================================*/ #ifndef __mitkTbssImporter_cpp #define __mitkTbssImporter_cpp #include "mitkTbssImporter.h" #include #include namespace mitk { mitk::TbssImporter::TbssImporter() { } - mitk::TbssImporter::TbssImporter(std::string path) : m_InputPath(path) + mitk::TbssImporter::TbssImporter(std::string path) : m_File(path) { } mitk::TbssImage::Pointer mitk::TbssImporter::Import() { // read all images with all_*.nii.gz mitk::TbssImage::Pointer tbssImg = mitk::TbssImage::New(); + FileReaderType4D::Pointer reader = FileReaderType4D::New(); + reader->SetFileName(m_File); + reader->Update(); - QDir currentDir = QDir(QString(m_InputPath.c_str())); - currentDir.setFilter( QDir::Files ); - QStringList entries = currentDir.entryList(); + FloatImage4DType::Pointer img = FloatImage4DType::New(); + img = reader->GetOutput(); - for(int e=0; eGetLargestPossibleRegion().GetSize(); + FloatImage4DType::SpacingType spacing = img->GetSpacing(); - if (std::string::npos != filename.find("all_FA_skeletonised.nii.gz")) - { - // Found a 4d skeletonized image - if(m_InputPath.find_last_of("/") != m_InputPath.size()-1) - { - m_InputPath += "/"; - } + DataImageType::SizeType dataSize; + dataSize[0] = size[0]; + dataSize[1] = size[1]; + dataSize[2] = size[2]; - std::string file = m_InputPath + filename; - FileReaderType4D::Pointer reader = FileReaderType4D::New(); - reader->SetFileName(file); - reader->Update(); + m_Data->SetRegions(dataSize); - FloatImage4DType::Pointer img = FloatImage4DType::New(); - img = reader->GetOutput(); - //Create new m_Data (only once) - if(m_Data.IsNull()) - { - m_Data = DataImageType::New(); + DataImageType::SpacingType dataSpacing; + dataSpacing[0] = spacing[0]; + dataSpacing[1] = spacing[1]; + dataSpacing[2] = spacing[2]; + m_Data->SetSpacing(dataSpacing); - FloatImage4DType::SizeType size = img->GetLargestPossibleRegion().GetSize(); - FloatImage4DType::SpacingType spacing = img->GetSpacing(); + FloatImage4DType::PointType origin = img->GetOrigin(); + DataImageType::PointType dataOrigin; + dataOrigin[0] = origin[0]; + dataOrigin[1] = origin[1]; + dataOrigin[2] = origin[2]; - DataImageType::SizeType dataSize; - dataSize[0] = size[0]; - dataSize[1] = size[1]; - dataSize[2] = size[2]; + m_Data->SetOrigin(dataOrigin); - m_Data->SetRegions(dataSize); + FloatImage4DType::DirectionType dir = img->GetDirection(); - DataImageType::SpacingType dataSpacing; - dataSpacing[0] = spacing[0]; - dataSpacing[1] = spacing[1]; - dataSpacing[2] = spacing[2]; - - m_Data->SetSpacing(dataSpacing); + DataImageType::DirectionType dataDir; + for(int i=0; i<=2; i++) + { + for(int j=0; j<=2; j++) + { + dataDir[i][j] = dir[i][j]; + } + } - FloatImage4DType::PointType origin = img->GetOrigin(); + m_Data->SetDirection(dataDir); - DataImageType::PointType dataOrigin; - dataOrigin[0] = origin[0]; - dataOrigin[1] = origin[1]; - dataOrigin[2] = origin[2]; - m_Data->SetOrigin(dataOrigin); + // Set the length to one because otherwise allocate fails. Should be changed when groups/measurements are added + m_Data->SetVectorLength(size[3]); + m_Data->Allocate(); - FloatImage4DType::DirectionType dir = img->GetDirection(); - DataImageType::DirectionType dataDir; - for(int i=0; i<=2; i++) + for(int i=0; i ix; + ix[0] = i; + ix[1] = j; + ix[2] = k; + itk::VariableLengthVector pixel = m_Data->GetPixel(ix); + int vecSize = pixel.Size(); + + for(int z=0; zSetDirection(dataDir); - - //VariableLengthVector vec; - //vec.SetElement(i, 0.0); - //m_Data->FillBuffer(0.0); - + itk::Index<4> ix4; + ix4[0] = i; + ix4[1] = j; + ix4[2] = k; + ix4[3] = z; + float value = img->GetPixel(ix4); - // Set the length to one because otherwise allocate fails. Should be changed when groups/measurements are added - m_Data->SetVectorLength(size[3]); - m_Data->Allocate(); - - - } - - - // m_Data should be allocated here so move on filling it with values from the 4D image - FloatImage4DType::SizeType size = img->GetLargestPossibleRegion().GetSize(); - - for(int i=0; i ix; - ix[0] = i; - ix[1] = j; - ix[2] = k; - itk::VariableLengthVector pixel = m_Data->GetPixel(ix); - int vecSize = pixel.Size(); - - for(int z=0; z ix4; - ix4[0] = i; - ix4[1] = j; - ix4[2] = k; - ix4[3] = z; - float value = img->GetPixel(ix4); - - - pixel.SetElement(z, value); - } - m_Data->SetPixel(ix, pixel); - } + pixel.SetElement(z, value); } + m_Data->SetPixel(ix, pixel); } - - - std::cout << "found!" << std::endl; } - - - } // mitk::CastToTbssImage(m_Data.GetPointer(), tbssImg); + tbssImg->SetGroupInfo(m_Groups); + tbssImg->SetMeasurementInfo(m_MeasurementInfo); + tbssImg->SetImage(m_Data); + int vecsize = m_Data->GetVectorLength(); + tbssImg->InitializeFromVectorImage(); - tbssImg->SetGroupInfo(m_Groups); - tbssImg->SetMeasurementInfo(m_MeasurementInfo); - tbssImg->SetImage(m_Data); - - int vecsize = m_Data->GetVectorLength(); + return tbssImg; - tbssImg->InitializeFromVectorImage(); - return tbssImg; } - - } #endif // __mitkTbssImporter_cpp diff --git a/Modules/DiffusionImaging/IODataStructures/TbssImages/mitkTbssImporter.h b/Modules/DiffusionImaging/IODataStructures/TbssImages/mitkTbssImporter.h index f4c88cbea3..1f52c9ffff 100644 --- a/Modules/DiffusionImaging/IODataStructures/TbssImages/mitkTbssImporter.h +++ b/Modules/DiffusionImaging/IODataStructures/TbssImages/mitkTbssImporter.h @@ -1,98 +1,98 @@ /*========================================================================= Program: Medical Imaging & Interaction Toolkit Language: C++ Date: $Date: 2009-07-14 19:11:20 +0200 (Tue, 14 Jul 2009) $ Version: $Revision: 18127 $ Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. See MITKCopyright.txt or http://www.mitk.org/copyright.html for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notices for more information. =========================================================================*/ #ifndef __mitkTbssImporter_h #define __mitkTbssImporter_h #include "mitkCommon.h" #include "mitkFileReader.h" #include "itkImage.h" #include "itkVectorImage.h" #include "itkImageFileReader.h" #include "mitkTbssImage.h" namespace mitk { //template class TbssImporter : public itk::Object { public: // typedef TPixelType PixelType; typedef itk::VectorImage DataImageType; // type of the 3d vector image containing the skeletonized images typedef itk::Image FloatImage4DType; typedef itk::ImageFileReader FileReaderType4D; TbssImporter(); mitkClassMacro( TbssImporter, Object ) itkNewMacro(Self) TbssImporter(std::string path); - void SetInputPath(std::string p) + void SetInputFile(std::string f) { - m_InputPath = p; + m_File = f; } mitk::TbssImage::Pointer Import(); void SetGroupInfo(std::vector< std::pair > groups) { m_Groups = groups; } std::vector< std::pair > GetGroupInfo() { return m_Groups; } void SetMeasurementInfo(std::string s) { m_MeasurementInfo = s; } std::string GetMeasurementInfo() { return m_MeasurementInfo; } protected: virtual ~TbssImporter(){} - std::string m_InputPath; + std::string m_File; DataImageType::Pointer m_Data; std::vector< std::pair > m_Groups; std::string m_MeasurementInfo; }; } #include "mitkTbssImporter.cpp" #endif // __mitkTbssImporter_h diff --git a/Modules/DiffusionImaging/IODataStructures/TbssImages/mitkTbssRoiImage.h b/Modules/DiffusionImaging/IODataStructures/TbssImages/mitkTbssRoiImage.h index 926ab907d4..6f3cc6a306 100644 --- a/Modules/DiffusionImaging/IODataStructures/TbssImages/mitkTbssRoiImage.h +++ b/Modules/DiffusionImaging/IODataStructures/TbssImages/mitkTbssRoiImage.h @@ -1,126 +1,129 @@ /*========================================================================= Program: Medical Imaging & Interaction Toolkit Language: C++ Date: $Date: 2008-02-07 17:17:57 +0100 (Do, 07 Feb 2008) $ Version: $Revision: 11989 $ Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. See MITKCopyright.txt or http://www.mitk.org/copyright.html for details. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the above copyright notices for more information. =========================================================================*/ #ifndef __mitkTbssRoiImage__h #define __mitkTbssRoiImage__h #include "mitkImage.h" #include "itkImage.h" #include "mitkImageCast.h" +#include "MitkDiffusionImagingExports.h" + namespace mitk { /** * \brief this class encapsulates diffusion volumes (vectorimages not * yet supported by mitkImage) */ - class TbssRoiImage : public Image + class MitkDiffusionImaging_EXPORT TbssRoiImage : public Image { public: typedef itk::Image ImageType; typedef itk::Index<3> IndexType; //typedef typename std::vector > RoiType; mitkClassMacro( TbssRoiImage, Image ) itkNewMacro(Self) //void SetRequestedRegionToLargestPossibleRegion(); //bool RequestedRegionIsOutsideOfTheBufferedRegion(); //virtual bool VerifyRequestedRegion(); //void SetRequestedRegion(itk::DataObject *data); ImageType::Pointer GetImage() { return m_Image; } void SetImage(ImageType::Pointer image ) { this->m_Image = image; } itkGetMacro(PreprocessedFA, bool) itkSetMacro(PreprocessedFA, bool) itkGetMacro(PreprocessedFAFile, std::string) itkSetMacro(PreprocessedFAFile, std::string) itkGetMacro(Structure, std::string) itkSetMacro(Structure, std::string) void SetRoi(std::vector< itk::Index<3> > roi) { m_Roi = roi; } // depricated std::vector< itk::Index<3> > GetRoi() { return m_Roi; } + void InitializeFromImage() { MITK_INFO << "make an mitk image that can be shown by mitk"; this->InitializeByItk(m_Image.GetPointer(),1,1); } TbssRoiImage(); protected: virtual ~TbssRoiImage(){} ImageType::Pointer m_Image; std::vector< itk::Index<3> > m_Roi; bool m_PreprocessedFA; std::string m_PreprocessedFAFile; std::string m_Structure; }; // Does the same es the normal CastToMitkImage, but needed to reimplemented due to the templatet pixeltype template void CastToTbssImage(const ItkOutputImageType* itkimage, itk::SmartPointer< mitk::TbssRoiImage >& tbssoutputimage) { if(tbssoutputimage.IsNull()) { tbssoutputimage = mitk::TbssRoiImage::New(); } tbssoutputimage->InitializeByItk(itkimage); tbssoutputimage->SetChannel(itkimage->GetBufferPointer()); } } // namespace mitk //#include "mitkTbssRoiImage.cpp" #endif /* __mitkTbssRoiImage__h */