1 /*=========================================================================
4 Module: $RCSfile: vvFromITK.h,v $
6 Date: $Date: 2010/01/06 13:32:01 $
7 Version: $Revision: 1.1 $
8 Author : Joel Schaerer (joel.schaerer@insa-lyon.fr)
11 Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
12 CREATIS-LRMN http://www.creatis.insa-lyon.fr
14 This program is free software: you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation, version 3 of the License.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 =========================================================================*/
28 #ifndef vvImageFromITK_h
29 #define vvImageFromITK_h
33 #include <itkExtractImageFilter.h>
34 #include "itkImageToVTKImageFilter.h"
36 /**Converts the itk image to vv, handling the 4D problem
37 * The time_sequence boolean specifies that the image is to be interpreted as a time sequence,
38 * even if its dim is < 4. */
39 template<unsigned int Dim, class PixelType> vvImage::Pointer vvImageFromITK(typename itk::Image<PixelType,Dim>::Pointer input, bool time_sequence=false)
41 assert(Dim < 5 && Dim > 0); // We don't handle anything higher than 4-dimensional (for the moment :-p)
42 vvImage::Pointer vv_image=vvImage::New();
43 vv_image->Init(); //Delete any existing images
44 typedef itk::Image< PixelType, Dim > InputImageType;
46 if (Dim == 4 || time_sequence) //The time sequence case: create a series of VTK images
48 typedef itk::Image< PixelType, Dim - 1 > ConnectorImageType;
49 typedef itk::ImageToVTKImageFilter <ConnectorImageType> ConnectorType;
50 typedef itk::ExtractImageFilter<InputImageType,ConnectorImageType> FilterType;
52 typename FilterType::Pointer filter = FilterType::New();
53 typename ConnectorType::Pointer connector = ConnectorType::New();
55 //extract the 3D slices and put them in a std::vector<vtkImageData*>
56 typename InputImageType::RegionType inputRegion = input->GetLargestPossibleRegion();
57 typename InputImageType::SizeType inputSize = inputRegion.GetSize();
59 typename InputImageType::SizeType extractedRegionSize = inputSize;
60 typename InputImageType::RegionType extractedRegion;
61 extractedRegionSize[Dim - 1] = 0;
62 extractedRegion.SetSize(extractedRegionSize);
64 filter->SetInput(input);
65 connector->SetInput(filter->GetOutput());
67 typename InputImageType::IndexType start = inputRegion.GetIndex();
69 for (unsigned int i = 0; i < inputSize[Dim - 1]; i++) {
71 extractedRegion.SetIndex(start);
72 filter->SetExtractionRegion(extractedRegion);
76 catch ( itk::ExceptionObject & err ) {
77 std::cerr << "Error while setting vvImage from ITK (Dim==4) [Extract phase]"
78 << " " << err << std::endl;
84 catch ( itk::ExceptionObject & err ) {
85 std::cerr << "Error while setting vvImage from ITK (Dim==4) [Connect phase]"
86 << " " << err << std::endl;
89 vtkImageData *image = vtkImageData::New();
90 image->DeepCopy(connector->GetOutput());
91 vv_image->AddImage(image);
94 else //Dim == 1,2,3 and not time_sequence
96 typedef itk::Image< PixelType, Dim > ConnectorImageType;
97 typedef itk::ImageToVTKImageFilter <ConnectorImageType> ConnectorType;
98 typename ConnectorType::Pointer connector = ConnectorType::New();
99 connector->SetInput(input);
104 catch ( itk::ExceptionObject & err ) {
105 std::cerr << "Error while setting vvImage from ITK (Dim==3)"
106 << " " << err << std::endl;
109 vtkImageData *image = vtkImageData::New();
110 image->DeepCopy(connector->GetOutput());
111 vv_image->AddImage(image);
116 #endif //vvImageFromITK