2 * This program is free software: you can redistribute it and/or modify
3 * it under the terms of the GNU Lesser General Public License as
4 * published by the Free Software Foundation, either version 3 of the
5 * License, or (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
19 * @author Pierre Gueth (\c pierre.gueth@gmail.com )
20 * Laboratoire d'InfoRmatique en Image et Systèmes d'information - LIRIS (CNRS,
21 * UMR 5205), CNRS, France
24 * @author Bertrand Kerautret (\c bertrand.kerautret@loria.fr )
25 * LORIA (CNRS, UMR 7503), University of Lorraine, France
29 * Header file for module ITKReader.cpp
31 * This file is part of the DGtal library.
34#include "DGtal/images/ConstImageAdapter.h"
36#pragma GCC diagnostic push
37#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
40#pragma clang diagnostic push
41#pragma clang diagnostic ignored "-Wdocumentation"
43#include <itkImageFileReader.h>
45#pragma clang diagnostic pop
48#pragma GCC diagnostic pop
54 I ITKReader<I>::importITK(
55 const std::string & filename, bool shiftDomainUsingOrigin )
57 typename DGtal::ITKIOTrait<typename I::Value>::DefaultReadFunctor def;
58 return DGtal::ITKReader<I>::importITK(filename,def , shiftDomainUsingOrigin);
62 template <typename TFunctor>
63 I ITKReader<I>::importITK(
64 const std::string & filename,
65 const TFunctor & aFunctor, bool shiftDomainUsingOrigin )
67 typedef typename Image::Domain Domain;
68 typedef itk::ImageIOBase::IOComponentType IOComponentType;
69 BOOST_CONCEPT_ASSERT( (concepts::CUnaryFunctor<TFunctor, ValueOut, Value>));
70 const IOComponentType componentType = getITKComponentType( filename );
71 switch ( componentType )
75 case itk::ImageIOBase::UNKNOWNCOMPONENTTYPE:
76 trace.warning() << "[ITKReader] Unknown and unsupported component type! "
77 "File will be loaded with UCHAR component type" << std::endl;
79 case itk::ImageIOBase::UCHAR:
81 typedef ImageContainerByITKImage<Domain, unsigned char> DGtalITKImage;
82 return readDGtalImageFromITKtypes<DGtalITKImage>( filename, aFunctor, shiftDomainUsingOrigin );
84 case itk::ImageIOBase::CHAR:
86 typedef ImageContainerByITKImage<Domain, char> DGtalITKImage;
87 return readDGtalImageFromITKtypes<DGtalITKImage>( filename, aFunctor, shiftDomainUsingOrigin );
89 case itk::ImageIOBase::USHORT:
91 typedef ImageContainerByITKImage<Domain, unsigned short> DGtalITKImage;
92 return readDGtalImageFromITKtypes<DGtalITKImage>( filename, aFunctor, shiftDomainUsingOrigin );
94 case itk::ImageIOBase::SHORT:
96 typedef ImageContainerByITKImage<Domain, short> DGtalITKImage;
97 return readDGtalImageFromITKtypes<DGtalITKImage>( filename, aFunctor, shiftDomainUsingOrigin );
99 case itk::ImageIOBase::UINT:
101 typedef ImageContainerByITKImage<Domain, unsigned int> DGtalITKImage;
102 return readDGtalImageFromITKtypes<DGtalITKImage>( filename, aFunctor, shiftDomainUsingOrigin );
104 case itk::ImageIOBase::INT:
106 typedef ImageContainerByITKImage<Domain, int> DGtalITKImage;
107 return readDGtalImageFromITKtypes<DGtalITKImage>( filename, aFunctor, shiftDomainUsingOrigin );
109 case itk::ImageIOBase::ULONG:
111 typedef ImageContainerByITKImage<Domain, unsigned long> DGtalITKImage;
112 return readDGtalImageFromITKtypes<DGtalITKImage>( filename, aFunctor, shiftDomainUsingOrigin );
114 case itk::ImageIOBase::LONG:
116 typedef ImageContainerByITKImage<Domain, long> DGtalITKImage;
117 return readDGtalImageFromITKtypes<DGtalITKImage>( filename, aFunctor, shiftDomainUsingOrigin );
119#if (ITK_VERSION_MAJOR > 4)\
120 || (ITK_VERSION_MAJOR == 4 && ITK_VERSION_MINOR >= 13)
121 case itk::ImageIOBase::ULONGLONG:
123 typedef ImageContainerByITKImage<Domain, unsigned long long>
125 return readDGtalImageFromITKtypes<DGtalITKImage>( filename, aFunctor, shiftDomainUsingOrigin );
127 case itk::ImageIOBase::LONGLONG:
129 typedef ImageContainerByITKImage<Domain, long long> DGtalITKImage;
130 return readDGtalImageFromITKtypes<DGtalITKImage>( filename, aFunctor, shiftDomainUsingOrigin );
133 case itk::ImageIOBase::FLOAT:
135 typedef ImageContainerByITKImage<Domain, float> DGtalITKImage;
136 return readDGtalImageFromITKtypes<DGtalITKImage>( filename, aFunctor, shiftDomainUsingOrigin );
138 case itk::ImageIOBase::DOUBLE:
140 typedef ImageContainerByITKImage<Domain, double> DGtalITKImage;
141 return readDGtalImageFromITKtypes<DGtalITKImage>( filename, aFunctor, shiftDomainUsingOrigin );
146 template <typename I>
147 itk::ImageIOBase::IOComponentType
148 ITKReader<I>::getITKComponentType( const std::string & filename )
150 typedef itk::ImageIOBase::IOComponentType IOComponentType;
151 IOComponentType componentType=itk::ImageIOBase::IOComponentType::UCHAR;
154 itk::ImageIOBase::Pointer imageIO = itk::ImageIOFactory::CreateImageIO(
155 filename.c_str(), itk::ImageIOFactory::ReadMode );
158 imageIO->SetFileName( filename.c_str() );
159 imageIO->ReadImageInformation();
160 componentType = imageIO->GetComponentType();
163 catch ( itk::ExceptionObject & e )
168 return componentType;
171 template <typename I>
172 template <typename Domain, typename PixelType>
174 ImageContainerByITKImage<Domain, PixelType>
175 ITKReader<I>::readDGtalITKImage(const std::string & filename, bool shiftDomainUsingOrigin)
177 typedef ImageContainerByITKImage<Domain, PixelType> TypeDGtalImage;
178 const unsigned int dimension = Domain::dimension;
179 typedef itk::Image<PixelType, dimension> ItkImage;
180 typedef itk::ImageFileReader<ItkImage> ITKImageReader;
181 typedef typename TypeDGtalImage::ITKImagePointer ITKImagePointer;
182 ITKImagePointer itk_image = nullptr;
186 typename ITKImageReader::Pointer reader = ITKImageReader::New();
187 reader->SetFileName( filename );
191 itk_image = reader->GetOutput();
193 catch ( itk::ExceptionObject &e )
199 TypeDGtalImage image( itk_image );
200 if (shiftDomainUsingOrigin)
202 const auto c = itk_image->GetOrigin();
203 typename Domain::Point p;
204 for (unsigned int k = 0; k < dimension; k++)
206 p[k] = static_cast<typename Domain::Integer>(c[k]);
208 image.updateDomain(p);
213 template <typename I>
214 template <typename Image, typename Domain, typename OrigValue,
215 typename TFunctor, typename Value>
217 ITKReader<I>::Aux<Image, Domain, OrigValue, TFunctor, Value>::
218 readDGtalImageFromITKtypes(const std::string & filename,
219 const TFunctor & aFunctor, bool shiftDomainUsingOrigin)
221 typedef ImageContainerByITKImage<Domain, OrigValue> TypeDGtalImage;
222 TypeDGtalImage dgtal_itk_image =
223 ITKReader<I>::readDGtalITKImage<Domain, OrigValue>( filename , shiftDomainUsingOrigin);
225 const Domain& domain = dgtal_itk_image.domain();
227 typedef ConstImageAdapter<TypeDGtalImage, Domain, functors::Identity,
228 Value, TFunctor> AdaptedImage;
229 const functors::Identity identityFunctor{};
230 const AdaptedImage adapted( dgtal_itk_image, domain, identityFunctor,
233 Image image( domain );
234 std::copy( adapted.constRange().begin(), adapted.constRange().end(),
235 image.range().outputIterator() );
242 template <typename I>
243 template <typename Domain, typename OrigValue, typename TFunctor,
245 ImageContainerByITKImage<Domain, Value>
246 ITKReader<I>::Aux<ImageContainerByITKImage<Domain, Value>, Domain, OrigValue,
248 readDGtalImageFromITKtypes( const std::string & filename,
249 const TFunctor & aFunctor, bool shiftDomainUsingOrigin )
251 typedef ImageContainerByITKImage<Domain, Value> Image;
253 typedef ImageContainerByITKImage<Domain, OrigValue> TypeDGtalImage;
254 TypeDGtalImage dgtal_itk_image =
255 ITKReader<I>::readDGtalITKImage<Domain, OrigValue>( filename, shiftDomainUsingOrigin );
257 const Domain& domain = dgtal_itk_image.domain();
259 typedef ConstImageAdapter<TypeDGtalImage, Domain, functors::Identity,
262 const functors::Identity identityFunctor{};
263 const AdaptedImage adapted( dgtal_itk_image, domain, identityFunctor,
266 Image image( domain );
267 std::copy( adapted.constRange().begin(), adapted.constRange().end(),
268 image.range().outputIterator() );
270 //copy ITKImage spatial parameters
271 image.getITKImagePointer()->SetOrigin(
272 dgtal_itk_image.getITKImagePointer()->GetOrigin() );
273 image.getITKImagePointer()->SetSpacing(
274 dgtal_itk_image.getITKImagePointer()->GetSpacing() );
275 image.getITKImagePointer()->SetDirection(
276 dgtal_itk_image.getITKImagePointer()->GetDirection() );
283 template <typename I>
284 template <typename TypeDGtalImage, typename TFunctor>
285 typename ITKReader<I>::Image ITKReader<I>::
286 readDGtalImageFromITKtypes( const std::string & filename,
287 const TFunctor & aFunctor, bool shiftDomainUsingOrigin )
289 typedef typename Image::Domain Domain;
290 typedef typename TypeDGtalImage::Value OrigValue;
292 return Aux<Image, Domain, OrigValue, TFunctor,
293 Value>::readDGtalImageFromITKtypes( filename, aFunctor, shiftDomainUsingOrigin );