Orfeo Toolbox  3.16
itkMetaImageConverter.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Insight Segmentation & Registration Toolkit
4  Module: $RCSfile: itkMetaImageConverter.txx,v $
5  Language: C++
6  Date: $Date: 2007-08-08 18:56:26 $
7  Version: $Revision: 1.5 $
8 
9  Copyright (c) Insight Software Consortium. All rights reserved.
10  See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
11 
12  This software is distributed WITHOUT ANY WARRANTY; without even
13  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14  PURPOSE. See the above copyright notices for more information.
15 
16 =========================================================================*/
17 #ifndef __itkMetaImageConverter_txx
18 #define __itkMetaImageConverter_txx
19 
20 #include "itkMetaImageConverter.h"
21 #include "itkImageRegionIterator.h"
23 
24 namespace itk
25 {
26 
28 template <unsigned int NDimensions, class PixelType>
31 {
32 
33 }
34 
36 template <unsigned int NDimensions, class PixelType>
40 {
41  MaskSpatialObjectPointer spatialObject = MaskSpatialObjectType::New();
42  typedef itk::Image<unsigned char,NDimensions> ImageType;
43 
44  typedef typename ImageType::Pointer ImagePointer;
45  typedef typename ImageType::SizeType SizeType;
46  typedef typename ImageType::RegionType RegionType;
47 
48  ImagePointer myImage = ImageType::New();
49 
50  SizeType size;
51 
52  double spacing[NDimensions];
53 
54  for(unsigned int i=0;i<NDimensions;i++)
55  {
56  size[i] = image->DimSize()[i];
57  spacing[i] = image->ElementSpacing()[i];
58  if(spacing[i] == 0) {spacing[i] = 1;}
59  }
60 
61  RegionType region;
62  region.SetSize(size);
63  itk::Index<NDimensions> zeroIndex;
64  zeroIndex.Fill(0);
65  region.SetIndex( zeroIndex );
66  myImage->SetLargestPossibleRegion(region);
67  myImage->SetBufferedRegion(region);
68  myImage->SetRequestedRegion(region);
69  myImage->SetSpacing(spacing);
70  myImage->Allocate();
71 
73  for(unsigned int i = 0; !it.IsAtEnd(); i++, ++it)
74  {
75  it.Set(
76  static_cast< typename ImageType::PixelType >( image->ElementData(i) ));
77  }
78 
79  spatialObject->SetImage(myImage);
80  spatialObject->SetId(image->ID());
81  spatialObject->SetParentId(image->ParentID());
82  spatialObject->GetProperty()->SetName(image->Name());
83 
84  return spatialObject;
85 }
86 
88 template <unsigned int NDimensions, class PixelType>
92 {
93  SpatialObjectPointer spatialObject = SpatialObjectType::New();
94  typedef itk::Image<PixelType,NDimensions> ImageType;
95 
96  typedef typename ImageType::Pointer ImagePointer;
97  typedef typename ImageType::SizeType SizeType;
98  typedef typename ImageType::RegionType RegionType;
99 
100  ImagePointer myImage = ImageType::New();
101 
102  SizeType size;
103 
104  double spacing[NDimensions];
105 
106  for(unsigned int i=0;i<NDimensions;i++)
107  {
108  size[i] = image->DimSize()[i];
109  spacing[i] = image->ElementSpacing()[i];
110  if(spacing[i] == 0) {spacing[i] = 1;}
111  }
112 
113  RegionType region;
114  region.SetSize(size);
115  itk::Index<NDimensions> zeroIndex;
116  zeroIndex.Fill(0);
117  region.SetIndex( zeroIndex );
118  myImage->SetLargestPossibleRegion(region);
119  myImage->SetBufferedRegion(region);
120  myImage->SetRequestedRegion(region);
121  myImage->SetSpacing(spacing);
122  myImage->Allocate();
123 
125  for(unsigned int i = 0; !it.IsAtEnd(); i++, ++it)
126  {
127  it.Set(
128  static_cast< typename ImageType::PixelType >( image->ElementData(i) ));
129  }
130 
131  spatialObject->SetImage(myImage);
132  spatialObject->SetId(image->ID());
133  spatialObject->SetParentId(image->ParentID());
134  spatialObject->GetProperty()->SetName(image->Name());
135 
136  return spatialObject;
137 }
138 
140 template <unsigned int NDimensions, class PixelType>
141 MetaImage*
144 {
145  typedef itk::Image<PixelType,NDimensions> ImageType;
146  typedef typename ImageType::ConstPointer ImageConstPointer;
147  typedef typename ImageType::SizeType SizeType;
148  typedef typename ImageType::RegionType RegionType;
149 
150  ImageConstPointer SOImage = spatialObject->GetImage();
151 
152  float spacing[NDimensions];
153  int size[NDimensions];
154 
155  for(unsigned int i=0;i<NDimensions;i++)
156  {
157  size[i] = SOImage->GetLargestPossibleRegion().GetSize()[i];
158  spacing[i] = SOImage->GetSpacing()[i];
159  }
160 
161  MetaImage* Image = new MetaImage(NDimensions,size,
162  spacing,MET_GetPixelType(typeid(PixelType)));
163 
165  SOImage->GetLargestPossibleRegion());
166  for(unsigned int i = 0; !it.IsAtEnd(); i++, ++it)
167  {
168  Image->ElementData(i,it.Get());
169  }
170 
171  Image->ID(spatialObject->GetId());
172  if(spatialObject->GetParent())
173  {
174  Image->ParentID(spatialObject->GetParent()->GetId());
175  }
176  return Image;
177 }
178 
179 
181 template <unsigned int NDimensions, class PixelType>
184 ::ReadMeta(const char* name)
185 {
186  SpatialObjectPointer spatialObject;
187  MetaImage* Image = new MetaImage();
188  Image->Read(name);
189  Image->PrintInfo();
190 
191  spatialObject = MetaImageToImageSpatialObject(Image);
192 
193  return spatialObject;
194 }
195 
196 
198 template <unsigned int NDimensions, class PixelType>
199 bool
201 ::WriteMeta(SpatialObjectType* spatialObject,const char* name)
202 {
203  MetaImage* Image = ImageSpatialObjectToMetaImage(spatialObject);
204  Image->Write(name);
205  return true;
206 }
207 
208 } // end namespace itk
209 
210 
211 #endif

Generated at Sat Feb 2 2013 23:52:45 for Orfeo Toolbox with doxygen 1.8.1.1