17 #ifndef __mitkDiffusionImageNiftiWriterService__cpp
18 #define __mitkDiffusionImageNiftiWriterService__cpp
21 #include "itkMetaDataDictionary.h"
22 #include "itkMetaDataObject.h"
23 #include "itkNiftiImageIO.h"
24 #include "itkImageFileWriter.h"
25 #include "itksys/SystemTools.hxx"
57 MITK_ERROR <<
"Sorry, input to DiffusionImageNiftiWriterService is NULL!";
60 if ( this->GetOutputLocation().empty() )
62 MITK_ERROR <<
"Sorry, filename has not been set!";
73 if (measurementFrame(0,0) || measurementFrame(0,1) || measurementFrame(0,2) ||
74 measurementFrame(1,0) || measurementFrame(1,1) || measurementFrame(1,2) ||
75 measurementFrame(2,0) || measurementFrame(2,1) || measurementFrame(2,2))
77 sprintf( valbuffer,
" (%lf,%lf,%lf) (%lf,%lf,%lf) (%lf,%lf,%lf)", measurementFrame(0,0), measurementFrame(0,1), measurementFrame(0,2), measurementFrame(1,0), measurementFrame(1,1), measurementFrame(1,2), measurementFrame(2,0), measurementFrame(2,1), measurementFrame(2,2));
78 itk::EncapsulateMetaData<std::string>(itkImg->GetMetaDataDictionary(),std::string(
"measurement frame"),std::string(valbuffer));
81 sprintf( valbuffer,
"DWMRI");
82 itk::EncapsulateMetaData<std::string>(itkImg->GetMetaDataDictionary(),std::string(
"modality"),std::string(valbuffer));
87 itk::EncapsulateMetaData<std::string>(itkImg->GetMetaDataDictionary(),std::string(
"DWMRI_b-value"),std::string(valbuffer));
92 sprintf( keybuffer,
"DWMRI_gradient_%04d", i );
101 itk::EncapsulateMetaData<std::string>(itkImg->GetMetaDataDictionary(),std::string(keybuffer),std::string(valbuffer));
104 typedef itk::VectorImage<short,3>
ImageType;
106 std::string ext = this->GetMimeType()->GetExtension(this->GetOutputLocation());
107 ext = itksys::SystemTools::LowerCase(ext);
113 this->SetOutputLocation(this->GetOutputLocation() + ext);
115 if (ext ==
".fsl" || ext ==
".fslgz")
117 MITK_INFO <<
"Writing Nifti-Image for FSL";
119 typedef itk::Image<short,4> ImageType4D;
122 ImageType::SpacingType spacing = itkImg->GetSpacing();
123 ImageType4D::SpacingType spacing4;
124 for(
int i=0; i<3; i++)
125 spacing4[i] = spacing[i];
127 img4->SetSpacing( spacing4 );
131 for(
int i=0; i<3; i++)
132 origin4[i] = origin[i];
134 img4->SetOrigin( origin4 );
136 ImageType::DirectionType direction = itkImg->GetDirection();
137 ImageType4D::DirectionType direction4;
138 for(
int i=0; i<3; i++)
139 for(
int j=0; j<3; j++)
140 direction4[i][j] = direction[i][j];
141 for(
int i=0; i<4; i++)
142 direction4[i][3] = 0;
143 for(
int i=0; i<4; i++)
144 direction4[3][i] = 0;
145 direction4[3][3] = 1;
146 img4->SetDirection( direction4 );
148 ImageType::RegionType region = itkImg->GetLargestPossibleRegion();
149 ImageType4D::RegionType region4;
151 ImageType::RegionType::SizeType size = region.GetSize();
152 ImageType4D::RegionType::SizeType size4;
154 for(
int i=0; i<3; i++)
156 size4[3] = itkImg->GetVectorLength();
158 ImageType::RegionType::IndexType index = region.GetIndex();
159 ImageType4D::RegionType::IndexType index4;
160 for(
int i=0; i<3; i++)
161 index4[i] = index[i];
164 region4.SetSize(size4);
165 region4.SetIndex(index4);
166 img4->SetRegions( region4 );
170 itk::ImageRegionIterator<ImageType> it (itkImg, itkImg->GetLargestPossibleRegion() );
173 for (it.GoToBegin(); !it.IsAtEnd(); ++it)
175 VecPixType vec = it.Get();
176 ImageType::IndexType currentIndex = it.GetIndex();
177 for(
unsigned int ind=0; ind<vec.Size(); ind++)
180 for(
int i=0; i<3; i++)
181 index4[i] = currentIndex[i];
183 img4->SetPixel(index4, vec[ind]);
188 std::string fname3 = this->GetOutputLocation();
189 std::string::iterator itend = fname3.end();
191 fname3.replace( itend-3, itend,
"nii");
193 fname3.replace( itend-5, itend,
"nii.gz");
197 typedef itk::ImageFileWriter<ImageType4D> WriterType4;
199 nrrdWriter4->UseInputMetaDataDictionaryOn();
200 nrrdWriter4->SetInput( img4 );
201 nrrdWriter4->SetFileName(fname3);
202 nrrdWriter4->UseCompressionOn();
203 nrrdWriter4->SetImageIO(io4);
206 nrrdWriter4->Update();
208 catch (itk::ExceptionObject e)
210 std::cout << e << std::endl;
214 itksys::SystemTools::CopyAFile(fname3.c_str(), this->GetOutputLocation().c_str());
218 std::ofstream myfile;
219 std::string fname = this->GetOutputLocation();
221 myfile.open (fname.c_str());
229 std::ofstream myfile2;
230 std::string fname2 = this->GetOutputLocation();
232 myfile2.open (fname2.c_str());
233 for(
int j=0; j<3; j++)
240 direction.normalize();
241 myfile2 << direction.get(j) <<
" ";
244 myfile2 << std::endl;
247 std::ofstream myfile3;
248 std::string fname4 = this->GetOutputLocation();
250 myfile3.open (fname4.c_str());
253 for(
int j=0; j<3; j++)
257 myfile3 << std::endl;
261 else if (ext ==
".nii" || ext ==
".nii.gz")
265 typedef itk::Image<short,4> ImageType4D;
268 ImageType::SpacingType spacing = itkImg->GetSpacing();
269 ImageType4D::SpacingType spacing4;
270 for(
int i=0; i<3; i++)
271 spacing4[i] = spacing[i];
273 img4->SetSpacing( spacing4 );
277 for(
int i=0; i<3; i++)
278 origin4[i] = origin[i];
280 img4->SetOrigin( origin4 );
282 ImageType::DirectionType direction = itkImg->GetDirection();
283 ImageType4D::DirectionType direction4;
284 for(
int i=0; i<3; i++)
285 for(
int j=0; j<3; j++)
286 direction4[i][j] = direction[i][j];
287 for(
int i=0; i<4; i++)
288 direction4[i][3] = 0;
289 for(
int i=0; i<4; i++)
290 direction4[3][i] = 0;
291 direction4[3][3] = 1;
292 img4->SetDirection( direction4 );
294 ImageType::RegionType region = itkImg->GetLargestPossibleRegion();
295 ImageType4D::RegionType region4;
297 ImageType::RegionType::SizeType size = region.GetSize();
298 ImageType4D::RegionType::SizeType size4;
300 for(
int i=0; i<3; i++)
302 size4[3] = itkImg->GetVectorLength();
304 ImageType::RegionType::IndexType index = region.GetIndex();
305 ImageType4D::RegionType::IndexType index4;
306 for(
int i=0; i<3; i++)
307 index4[i] = index[i];
310 region4.SetSize(size4);
311 region4.SetIndex(index4);
312 img4->SetRegions( region4 );
316 itk::ImageRegionIterator<ImageType> it (itkImg, itkImg->GetLargestPossibleRegion() );
319 for (it.GoToBegin(); !it.IsAtEnd(); ++it)
321 VecPixType vec = it.Get();
322 ImageType::IndexType currentIndex = it.GetIndex();
323 for(
unsigned int ind=0; ind<vec.Size(); ind++)
326 for(
int i=0; i<3; i++)
327 index4[i] = currentIndex[i];
329 img4->SetPixel(index4, vec[ind]);
335 typedef itk::ImageFileWriter<ImageType4D> WriterType4;
337 nrrdWriter4->UseInputMetaDataDictionaryOn();
338 nrrdWriter4->SetInput( img4 );
339 nrrdWriter4->SetFileName(this->GetOutputLocation());
340 nrrdWriter4->UseCompressionOn();
341 nrrdWriter4->SetImageIO(io4);
344 nrrdWriter4->Update();
346 catch (itk::ExceptionObject e)
348 std::cout << e << std::endl;
355 std::ofstream myfile;
356 std::string fname = itksys::SystemTools::GetFilenamePath( this->GetOutputLocation() ) +
"/"
357 + this->GetMimeType()->GetFilenameWithoutExtension( this->GetOutputLocation() );
359 myfile.open (fname.c_str());
367 std::ofstream myfile2;
368 std::string fname2 = itksys::SystemTools::GetFilenamePath( this->GetOutputLocation() ) +
"/"
369 + this->GetMimeType()->GetFilenameWithoutExtension( this->GetOutputLocation() );
371 myfile2.open (fname2.c_str());
372 for(
int j=0; j<3; j++)
379 direction.normalize();
380 myfile2 << direction.get(j) <<
" ";
383 myfile2 << std::endl;
386 std::ofstream myfile3;
387 std::string fname4 = itksys::SystemTools::GetFilenamePath( this->GetOutputLocation() ) +
"/"
388 + this->GetMimeType()->GetFilenameWithoutExtension( this->GetOutputLocation() );
390 myfile3.open (fname4.c_str());
393 for(
int j=0; j<3; j++)
397 myfile3 << std::endl;
421 #endif //__mitkDiffusionImageNiftiWriterService__cpp
bool IsDiffusionWeightedImage() const
const MeasurementFrameType & GetMeasurementFrame() const
itk::SmartPointer< Self > Pointer
virtual void Write() override
Write the base data to the specified location or output stream.
mitk::DiffusionPropertyHelper::GradientDirectionType GradientDirectionType
virtual ~DiffusionImageNiftiWriterService()
virtual ConfidenceLevel GetConfidenceLevel() const override
The confidence level of the reader or writer implementation.
DataCollection - Class to facilitate loading/accessing structured data.
The CustomMimeType class represents a custom mime-type which may be registered as a service object...
map::core::discrete::Elements< 3 >::InternalImageType ImageType
Convenience class to temporarily change the current locale.
virtual mitk::DiffusionImageNiftiWriterService * Clone() const override
Image class for storing images.
static const char * GetStaticNameOfClass()
float GetReferenceBValue() const
DiffusionImageNiftiWriterService()
void MITKCORE_EXPORT CastToItkImage(const mitk::Image *mitkImage, itk::SmartPointer< ItkOutputImageType > &itkOutputImage)
Cast an mitk::Image to an itk::Image with a specific type.
us::ServiceRegistration< IFileWriter > RegisterService(us::ModuleContext *context=us::GetModuleContext())
GradientDirectionsContainerType::Pointer GetGradientContainer() const
ConfidenceLevel
A confidence level describing the confidence of the reader or writer in handling the given data...
Base class for writing mitk::BaseData objects to files or streams.
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.