VMTK
Loading...
Searching...
No Matches
vtkvmtkITKImageWriter.cxx
Go to the documentation of this file.
1/*=========================================================================
2
3 Copyright Brigham and Women's Hospital (BWH) All Rights Reserved.
4
5 See COPYRIGHT.txt
6 or http://www.slicer.org/copyright/copyright.txt for details.
7
8 Program: vtkITK
9 Module: $HeadURL$
10 Date: $Date$
11 Version: $Revision$
12
13==========================================================================*/
14
15// vtkITK includes
17
18// VTK includes
19#include <vtkFloatArray.h>
20#include <vtkImageExport.h>
21#include <vtkImageFlip.h>
22#include <vtkInformation.h>
23#include <vtkInformationVector.h>
24#include <vtkITKUtility.h>
25#include <vtkNew.h>
26#include <vtkPointData.h>
27#include <vtkStreamingDemandDrivenPipeline.h>
28#include <vtkVersion.h>
29
30// VTKsys includes
31#include <vtksys/SystemTools.hxx>
32
33// ITK includes
34#include <itkDiffusionTensor3D.h>
35#include <itkImageFileWriter.h>
36#include <itkMetaDataDictionary.h>
37#include <itkMetaDataObject.h>
38#include <itkMetaDataObjectBase.h>
39#include <itkVTKImageImport.h>
40
41
43
44// helper function
45template <class TPixelType, int Dimension>
46void ITKWriteVTKImage(vtkvmtkITKImageWriter *self, vtkImageData *inputImage, char *fileName,
47 vtkMatrix4x4* rasToIjkMatrix, vtkMatrix4x4* MeasurementFrameMatrix=NULL) {
48
49 typedef itk::Image<TPixelType, Dimension> ImageType;
50
51 vtkMatrix4x4 *ijkToRasMatrix = vtkMatrix4x4::New();
52
53 if (rasToIjkMatrix == NULL)
54 {
55 std::cerr << "ITKWriteVTKImage: rasToIjkMatrix is null" << std::endl;
56 }
57 else
58 {
59 vtkMatrix4x4::Invert(rasToIjkMatrix, ijkToRasMatrix);
60 }
61 ijkToRasMatrix->Transpose();
62
63 typename ImageType::DirectionType direction;
64 typename ImageType::PointType origin;
65 direction.SetIdentity();
66
67 double mag[3];
68 int i;
69 for (i=0; i<3; i++)
70 {
71 // normalize vectors
72 mag[i] = 0;
73 for (int j=0; j<3; j++)
74 {
75 mag[i] += ijkToRasMatrix->GetElement(i,j)* ijkToRasMatrix->GetElement(i,j);
76 }
77 if (mag[i] == 0.0)
78 {
79 mag[i] = 1;
80 }
81 mag[i] = sqrt(mag[i]);
82 }
83
84 for ( i=0; i<3; i++)
85 {
86 int j;
87 for (j=0; j<3; j++)
88 {
89 ijkToRasMatrix->SetElement(i, j, ijkToRasMatrix->GetElement(i,j)/mag[i]);
90 }
91 }
92
93
94 // ITK image direction are in LPS space
95 // convert from ijkToRas to ijkToLps
96 vtkMatrix4x4* rasToLpsMatrix = vtkMatrix4x4::New();
97 rasToLpsMatrix->Identity();
98 rasToLpsMatrix->SetElement(0,0,-1);
99 rasToLpsMatrix->SetElement(1,1,-1);
100
101 vtkMatrix4x4* ijkToLpsMatrix = vtkMatrix4x4::New();
102 vtkMatrix4x4::Multiply4x4(ijkToRasMatrix, rasToLpsMatrix, ijkToLpsMatrix);
103
104 for ( i=0; i<Dimension; i++)
105 {
106 origin[i] = ijkToRasMatrix->GetElement(3,i);
107 int j;
108 for (j=0; j<Dimension; j++)
109 {
110 if (Dimension == 2)
111 {
112 direction[j][i] = (i == j) ? 1. : 0;
113 }
114 else
115 {
116 direction[j][i] = ijkToLpsMatrix->GetElement(i,j);
117 }
118 }
119 }
120
121 rasToLpsMatrix->Delete();
122 ijkToRasMatrix->Delete();
123 ijkToLpsMatrix->Delete();
124
125 origin[0] *= -1;
126 origin[1] *= -1;
127
128 // itk import for input itk images
129 typedef typename itk::VTKImageImport<ImageType> ImageImportType;
130 typename ImageImportType::Pointer itkImporter = ImageImportType::New();
131
132 // vtk export for vtk image
133 vtkImageExport* vtkExporter = vtkImageExport::New();
134 vtkImageFlip* vtkFlip = vtkImageFlip::New();
135
136 // writer
137 typedef typename itk::ImageFileWriter<ImageType> ImageWriterType;
138 typename ImageWriterType::Pointer itkImageWriter = ImageWriterType::New();
139
140 if ( self->GetUseCompression() )
141 {
142 itkImageWriter->UseCompressionOn();
143 }
144 else
145 {
146 itkImageWriter->UseCompressionOff();
147 }
148
149
150 // set pipeline for the image
151 vtkFlip->SetInputData( inputImage );
152 vtkExporter->SetInputData ( inputImage );
153 vtkFlip->SetFilteredAxis(1);
154 vtkFlip->FlipAboutOriginOn();
155
156 ConnectPipelines(vtkExporter, itkImporter);
157
158 // write image
159 if(self->GetImageIOClassName())
160 {
161 itk::LightObject::Pointer objectType =
162 itk::ObjectFactoryBase::CreateInstance(self->GetImageIOClassName());
163 itk::ImageIOBase* imageIOType = dynamic_cast< itk::ImageIOBase * >(
164 objectType.GetPointer());
165 if(imageIOType){
166 itkImageWriter->SetImageIO(imageIOType);
167 }
168 }
169 itkImageWriter->SetInput(itkImporter->GetOutput());
170
171 if (MeasurementFrameMatrix != NULL)
172 {
173 typedef std::vector<std::vector<double> > DoubleVectorType;
174 typedef itk::MetaDataObject<DoubleVectorType> MetaDataDoubleVectorType;
175 const itk::MetaDataDictionary & dictionary = itkImageWriter->GetMetaDataDictionary();
176
177 itk::MetaDataDictionary::ConstIterator itr = dictionary.Begin();
178 itk::MetaDataDictionary::ConstIterator end = dictionary.End();
179
180 while( itr != end )
181 {
182 // Get Measurement Frame
183 itk::MetaDataObjectBase::Pointer entry = itr->second;
184 MetaDataDoubleVectorType::Pointer entryvalue
185 = dynamic_cast<MetaDataDoubleVectorType *>( entry.GetPointer() );
186 if( entryvalue )
187 {
188 int pos = itr->first.find( "NRRD_measurement frame" );
189 if( pos != -1 )
190 {
191 DoubleVectorType tagvalue;
192 tagvalue.resize( 3 );
193 for( int i = 0; i < 3; i++ )
194 {
195 tagvalue[i].resize( 3 );
196 for( int j = 0; j < 3; j++ )
197 {
198 tagvalue[i][j] = MeasurementFrameMatrix->GetElement(i, j);
199 }
200 }
201 entryvalue->SetMetaDataObjectValue( tagvalue );
202 }
203 }
204 ++itr;
205 }
206 }
207
208 try
209 {
210 itkImporter->GetOutput()->SetDirection(direction);
211 itkImporter->GetOutput()->Update();
212 itkImporter->GetOutput()->SetOrigin(origin);
213 itkImporter->GetOutput()->SetSpacing(mag);
214 itkImageWriter->SetFileName( fileName );
215 itkImageWriter->Update();
216 }
217 catch (itk::ExceptionObject& exception)
218 {
219 exception.Print(std::cerr);
220 throw exception;
221 }
222 // clean up
223 vtkExporter->Delete();
224 vtkFlip->Delete();
225}
226
227//----------------------------------------------------------------------------
228template <class TPixelType>
229void ITKWriteVTKImage(vtkvmtkITKImageWriter *self, vtkImageData *inputImage, char *fileName,
230 vtkMatrix4x4* rasToIjkMatrix, vtkMatrix4x4* measurementFrameMatrix=NULL)
231{
232 std::string fileExtension = vtksys::SystemTools::LowerCase( vtksys::SystemTools::GetFilenameLastExtension(fileName) );
233 bool saveAsJPEG = (fileExtension == ".jpg") || (fileExtension == ".jpeg");
234 if (saveAsJPEG)
235 {
236 ITKWriteVTKImage<TPixelType, 2>(self, inputImage, fileName, rasToIjkMatrix);
237 }
238 else // 3D
239 {
240 ITKWriteVTKImage<TPixelType, 3>(self, inputImage, fileName, rasToIjkMatrix, measurementFrameMatrix);
241 }
242}
243
244//----------------------------------------------------------------------------
246{
247 this->FileName = NULL;
248 this->RasToIJKMatrix = NULL;
249 this->MeasurementFrameMatrix = NULL;
250 this->UseCompression = 0;
251 this->ImageIOClassName = NULL;
252}
253
254
255//----------------------------------------------------------------------------
257{
258 // get rid of memory allocated for file names
259 if (this->FileName)
260 {
261 delete [] this->FileName;
262 this->FileName = NULL;
263 }
264
265 if (this->ImageIOClassName)
266 {
267 delete [] this->ImageIOClassName;
268 this->ImageIOClassName = NULL;
269 }
270}
271
272
273//----------------------------------------------------------------------------
274void vtkvmtkITKImageWriter::PrintSelf(std::ostream& os, vtkIndent indent)
275{
276 this->Superclass::PrintSelf(os,indent);
277
278 os << indent << "FileName: " <<
279 (this->FileName ? this->FileName : "(none)") << "\n";
280 os << indent << "ImageIOClassName: " <<
281 (this->ImageIOClassName ? this->ImageIOClassName : "(none)") << "\n";
282}
283
284
285//----------------------------------------------------------------------------
286// This function sets the name of the file.
288{
289 if ( this->FileName && name && (!strcmp(this->FileName,name)))
290 {
291 return;
292 }
293 if (!name && !this->FileName)
294 {
295 return;
296 }
297 if (this->FileName)
298 {
299 delete [] this->FileName;
300 }
301
302 this->FileName = new char[strlen(name) + 1];
303 strcpy(this->FileName, name);
304 this->Modified();
305}
306
307//----------------------------------------------------------------------------
308// Writes all the data from the input.
310{
311 vtkImageData *inputImage = this->GetImageDataInput(0);
312 vtkPointData* pointData = inputImage->GetPointData();
313
314 if ( inputImage == NULL ||
315 pointData == NULL )
316 {
317 vtkErrorMacro(<<"vtkvmtkITKImageWriter: No image to write");
318 return;
319 }
320 if ( ! this->FileName )
321 {
322 vtkErrorMacro(<<"vtkvmtkITKImageWriter: Please specify a FileName");
323 return;
324 }
325
326 if (this->GetOutputInformation(0))
327 {
328 this->GetOutputInformation(0)->Set(
329 vtkStreamingDemandDrivenPipeline::UPDATE_EXTENT(),
330 this->GetOutputInformation(0)->Get(
331 vtkStreamingDemandDrivenPipeline::WHOLE_EXTENT()), 6);
332 }
333
334 int inputDataType =
335 pointData->GetScalars() ? pointData->GetScalars()->GetDataType() :
336 pointData->GetTensors() ? pointData->GetTensors()->GetDataType() :
337 pointData->GetVectors() ? pointData->GetVectors()->GetDataType() :
338 pointData->GetNormals() ? pointData->GetNormals()->GetDataType() :
339 0;
340 int inputNumberOfScalarComponents =
341 pointData->GetScalars() ? pointData->GetScalars()->GetNumberOfComponents() :
342 pointData->GetTensors() ? pointData->GetTensors()->GetNumberOfComponents() :
343 pointData->GetVectors() ? pointData->GetVectors()->GetNumberOfComponents() :
344 pointData->GetNormals() ? pointData->GetNormals()->GetNumberOfComponents() :
345 0;
346
347 if (inputNumberOfScalarComponents == 1)
348 {
349 // take into consideration the scalar type
350 switch (inputDataType)
351 {
352 case VTK_DOUBLE:
353 {
354 ITKWriteVTKImage<double>(this, inputImage, this->GetFileName(), this->RasToIJKMatrix);
355 }
356 break;
357 case VTK_FLOAT:
358 {
359 ITKWriteVTKImage<float>(this, inputImage, this->GetFileName(), this->RasToIJKMatrix);
360 }
361 break;
362 case VTK_LONG:
363 {
364 ITKWriteVTKImage<long>(this, inputImage, this->GetFileName(), this->RasToIJKMatrix);
365 }
366 break;
367 case VTK_UNSIGNED_LONG:
368 {
369 ITKWriteVTKImage<unsigned long>(this, inputImage, this->GetFileName(), this->RasToIJKMatrix);
370 }
371 break;
372 case VTK_INT:
373 {
374 ITKWriteVTKImage<int>(this, inputImage, this->GetFileName(), this->RasToIJKMatrix);
375 }
376 break;
377 case VTK_UNSIGNED_INT:
378 {
379 ITKWriteVTKImage<unsigned int>(this, inputImage, this->GetFileName(), this->RasToIJKMatrix);
380 }
381 break;
382 case VTK_SHORT:
383 {
384 ITKWriteVTKImage<short>(this, inputImage, this->GetFileName(), this->RasToIJKMatrix);
385 }
386 break;
387 case VTK_UNSIGNED_SHORT:
388 {
389 ITKWriteVTKImage<unsigned short>(this, inputImage, this->GetFileName(), this->RasToIJKMatrix);
390 }
391 break;
392 case VTK_CHAR:
393 {
394 ITKWriteVTKImage<char>(this, inputImage, this->GetFileName(), this->RasToIJKMatrix);
395 }
396 break;
397 case VTK_UNSIGNED_CHAR:
398 {
399 ITKWriteVTKImage<unsigned char>(this, inputImage, this->GetFileName(), this->RasToIJKMatrix);
400 }
401 break;
402 default:
403 vtkErrorMacro(<< "Execute: Unknown output ScalarType");
404 return;
405 }
406 } // scalar
407 else if (inputNumberOfScalarComponents == 3)
408 {
409 // take into consideration the scalar type
410 switch (inputDataType)
411 {
412 case VTK_DOUBLE:
413 {
414 typedef itk::Vector<double, 3> VectorPixelType;
415 ITKWriteVTKImage<VectorPixelType>(this, inputImage, this->GetFileName(), this->RasToIJKMatrix);
416 }
417 break;
418 case VTK_FLOAT:
419 {
420 typedef itk::Vector<float, 3> VectorPixelType;
421 ITKWriteVTKImage<VectorPixelType>(this, inputImage, this->GetFileName(), this->RasToIJKMatrix);
422 }
423 break;
424 case VTK_UNSIGNED_SHORT:
425 {
426 typedef itk::Vector<unsigned short, 3> VectorPixelType;
427 ITKWriteVTKImage<VectorPixelType>(this, inputImage, this->GetFileName(), this->RasToIJKMatrix);
428 }
429 break;
430 case VTK_UNSIGNED_CHAR:
431 {
432 typedef itk::Vector<unsigned char, 3> VectorPixelType;
433 ITKWriteVTKImage<VectorPixelType>(this, inputImage, this->GetFileName(), this->RasToIJKMatrix);
434 }
435 break;
436 default:
437 vtkErrorMacro(<< "Execute: Unknown output ScalarType");
438 return;
439 }
440 } // vector
441 else if (inputNumberOfScalarComponents == 4)
442 {
443 // take into consideration the scalar type
444 switch (inputDataType)
445 {
446 case VTK_DOUBLE:
447 {
448 typedef itk::Vector<double, 4> VectorPixelType;
449 ITKWriteVTKImage<VectorPixelType>(this, inputImage, this->GetFileName(), this->RasToIJKMatrix);
450 }
451 break;
452 case VTK_FLOAT:
453 {
454 typedef itk::Vector<float, 4> VectorPixelType;
455 ITKWriteVTKImage<VectorPixelType>(this, inputImage, this->GetFileName(), this->RasToIJKMatrix);
456 }
457 break;
458 case VTK_UNSIGNED_SHORT:
459 {
460 typedef itk::Vector<unsigned short, 4> VectorPixelType;
461 ITKWriteVTKImage<VectorPixelType>(this, inputImage, this->GetFileName(), this->RasToIJKMatrix);
462 }
463 break;
464 case VTK_UNSIGNED_CHAR:
465 {
466 typedef itk::Vector<unsigned char, 4> VectorPixelType;
467 ITKWriteVTKImage<VectorPixelType>(this, inputImage, this->GetFileName(), this->RasToIJKMatrix);
468 }
469 break;
470 default:
471 vtkErrorMacro(<< "Execute: Unknown output ScalarType");
472 return;
473 }
474 } // 4-vector
475 else if (inputNumberOfScalarComponents == 9)
476 {
477 // take into consideration the scalar type
478 switch (inputDataType)
479 {
480 case VTK_FLOAT:
481 {
482 typedef itk::DiffusionTensor3D<float> TensorPixelType;
483 vtkNew<vtkImageData> outImage;
484 outImage->SetDimensions(inputImage->GetDimensions());
485 outImage->SetOrigin(0, 0, 0);
486 outImage->SetSpacing(1, 1, 1);
487 outImage->AllocateScalars(VTK_FLOAT, 6);
488 vtkFloatArray* out = vtkFloatArray::SafeDownCast(outImage->GetPointData()->GetScalars());
489 vtkFloatArray* in = vtkFloatArray::SafeDownCast(inputImage->GetPointData()->GetTensors());
490 float inValue[9];
491 float outValue[6];
492 for(int i=0; i<out->GetNumberOfTuples(); i++)
493 {
494 in->GetTypedTuple(i, inValue);
495 //ITK expect tensors saved in upper-triangular format
496 outValue[0] = inValue[0];
497 outValue[1] = inValue[1];
498 outValue[2] = inValue[2];
499 outValue[3] = inValue[4];
500 outValue[4] = inValue[7];
501 outValue[5] = inValue[8];
502 out->SetTuple(i, outValue);
503 }
504
505 ITKWriteVTKImage<TensorPixelType>(this, outImage.GetPointer(),
506 this->GetFileName(), this->RasToIJKMatrix, this->MeasurementFrameMatrix);
507 }
508 inputImage->GetPointData()->SetScalars(NULL);
509 break;
510 default:
511 vtkErrorMacro(<< "Execute: Unknown output ScalarType");
512 return;
513 }
514 }
515 else
516 {
517 vtkErrorMacro(<< "Can only export 1 or 3 component images, current image has " << inputNumberOfScalarComponents << " components");
518 return;
519 }
520}
Write vtkImageData to a file using an ITK image IO.
virtual char * GetImageIOClassName()
Set/Get the ImageIO class name.
vtkMatrix4x4 * MeasurementFrameMatrix
virtual int GetUseCompression()
use compression if possible
void Write()
The main interface which triggers the writer to start.
void PrintSelf(std::ostream &os, vtkIndent indent) override
Helper templates and macros for connecting VTK and ITK image pipelines (superseded by vtkvmtkITKUtili...
void ConnectPipelines(ITK_Exporter exporter, VTK_Importer *importer)
vtkStandardNewMacro(vtkvmtkITKImageWriter)
void ITKWriteVTKImage(vtkvmtkITKImageWriter *self, vtkImageData *inputImage, char *fileName, vtkMatrix4x4 *rasToIjkMatrix, vtkMatrix4x4 *MeasurementFrameMatrix=NULL)