1 /*=========================================================================
5 Author : Joel Schaerer (joel.schaerer@insa-lyon.fr)
8 Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
9 CREATIS-LRMN http://www.creatis.insa-lyon.fr
11 This program is free software: you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation, version 3 of the License.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 =========================================================================*/
25 #ifndef VVIMAGEFROMITK_H
26 #define VVIMAGEFROMITK_H
29 //#include <itkImage.h>
30 #include <itkExtractImageFilter.h>
31 #include <itkImageToVTKImageFilter.h>
33 /**Converts the itk image to vv, handling the 4D problem
34 * The time_sequence boolean specifies that the image is to be interpreted as a time sequence,
35 * even if its dim is < 4. */
36 template<unsigned int Dim, class PixelType> vvImage::Pointer vvImageFromITK(typename itk::Image<PixelType,Dim>::Pointer input, bool time_sequence=false)
38 assert(Dim < 5 && Dim > 0); // We don't handle anything higher than 4-dimensional (for the moment :-p)
39 vvImage::Pointer vv_image=vvImage::New();
40 vv_image->Init(); //Delete any existing images
41 typedef itk::Image< PixelType, Dim > InputImageType;
43 if (Dim == 4 || time_sequence) //The time sequence case: create a series of VTK images
45 typedef itk::Image< PixelType, Dim - 1 > ConnectorImageType;
46 typedef itk::ImageToVTKImageFilter <ConnectorImageType> ConnectorType;
47 typedef itk::ExtractImageFilter<InputImageType,ConnectorImageType> FilterType;
49 typename FilterType::Pointer filter = FilterType::New();
50 typename ConnectorType::Pointer connector = ConnectorType::New();
52 //extract the 3D slices and put them in a std::vector<vtkImageData*>
53 typename InputImageType::RegionType inputRegion = input->GetLargestPossibleRegion();
54 typename InputImageType::SizeType inputSize = inputRegion.GetSize();
56 typename InputImageType::SizeType extractedRegionSize = inputSize;
57 typename InputImageType::RegionType extractedRegion;
58 extractedRegionSize[Dim - 1] = 0;
59 extractedRegion.SetSize(extractedRegionSize);
61 filter->SetInput(input);
62 connector->SetInput(filter->GetOutput());
64 typename InputImageType::IndexType start = inputRegion.GetIndex();
66 for (unsigned int i = 0; i < inputSize[Dim - 1]; i++) {
68 extractedRegion.SetIndex(start);
69 filter->SetExtractionRegion(extractedRegion);
73 catch ( itk::ExceptionObject & err ) {
74 std::cerr << "Error while setting vvImage from ITK (Dim==4) [Extract phase]"
75 << " " << err << std::endl;
81 catch ( itk::ExceptionObject & err ) {
82 std::cerr << "Error while setting vvImage from ITK (Dim==4) [Connect phase]"
83 << " " << err << std::endl;
86 vtkImageData *image = vtkImageData::New();
87 image->DeepCopy(connector->GetOutput());
88 vv_image->AddImage(image);
91 else //Dim == 1,2,3 and not time_sequence
93 typedef itk::Image< PixelType, Dim > ConnectorImageType;
94 typedef itk::ImageToVTKImageFilter <ConnectorImageType> ConnectorType;
95 typename ConnectorType::Pointer connector = ConnectorType::New();
96 connector->SetInput(input);
101 catch ( itk::ExceptionObject & err ) {
102 std::cerr << "Error while setting vvImage from ITK (Dim==3)"
103 << " " << err << std::endl;
106 vtkImageData *image = vtkImageData::New();
107 image->DeepCopy(connector->GetOutput());
108 vv_image->AddImage(image);
113 #endif //vvImageFromITK