1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
5 - University of LYON http://www.universite-lyon.fr/
6 - Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
7 - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the copyright notices for more information.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
18 #ifndef CLITKIMAGECOMMON_TXX
19 #define CLITKIMAGECOMMON_TXX
22 //--------------------------------------------------------------------
23 template<class PixelType>
24 typename itk::Image<PixelType,1>::Pointer NewImage1D(int vsize, double vspacing)
26 typedef itk::Image<PixelType,1> ImageType;
27 typename ImageType::Pointer g = ImageType::New();
28 typename ImageType::SizeType size;
30 typename ImageType::RegionType region;
32 g->SetRegions(region);
33 typename ImageType::SpacingType spacing;
34 spacing[0] = vspacing;
35 g->SetSpacing(spacing);
38 //--------------------------------------------------------------------
40 //--------------------------------------------------------------------
41 template<class PixelType>
42 typename itk::Image<PixelType,2>::Pointer NewImage2D(int sx, int sy, double dx, double dy)
44 typedef itk::Image<PixelType,2> ImageType;
45 typename ImageType::Pointer g = ImageType::New();
46 typename ImageType::SizeType size;
49 typename ImageType::RegionType region;
51 g->SetRegions(region);
52 typename ImageType::SpacingType spacing;
55 g->SetSpacing(spacing);
58 //--------------------------------------------------------------------
60 //--------------------------------------------------------------------
61 template<class PixelType>
62 typename itk::Image<PixelType,3>::Pointer NewImage3D(int sx, int sy, int sz, double dx, double dy, double dz)
64 typedef itk::Image<PixelType,3> ImageType;
65 typename ImageType::Pointer g = ImageType::New();
66 typename ImageType::SizeType size;
70 typename ImageType::RegionType region;
72 g->SetRegions(region);
73 typename ImageType::SpacingType spacing;
77 g->SetSpacing(spacing);
80 //--------------------------------------------------------------------
82 //--------------------------------------------------------------------
83 template<class PixelType>
84 typename itk::Image<PixelType,4>::Pointer NewImage4D(int sx, int sy, int sz, int st, double dx, double dy, double dz, double dt)
86 typedef itk::Image<PixelType,3> ImageType;
87 typename ImageType::Pointer g = ImageType::New();
88 typename ImageType::SizeType size;
93 typename ImageType::RegionType region;
95 g->SetRegions(region);
96 typename ImageType::SpacingType spacing;
101 g->SetSpacing(spacing);
104 //--------------------------------------------------------------------
107 //--------------------------------------------------------------------
108 template<class ImageType>
109 typename ImageType::Pointer NewImageLike(const typename ImageType::Pointer input, bool allocate)
111 typename ImageType::Pointer output = ImageType::New();
112 output->CopyInformation(input);
113 output->SetRegions(input->GetLargestPossibleRegion());
114 if (allocate) output->Allocate();
117 //--------------------------------------------------------------------
120 //--------------------------------------------------------------------
121 template<class ImageType>
122 void CopyValues(const typename ImageType::Pointer input,
123 typename ImageType::Pointer output)
125 typedef itk::ImageRegionConstIterator<ImageType> ConstIteratorType;
126 ConstIteratorType pi(input,input->GetLargestPossibleRegion());
128 typedef itk::ImageRegionIterator<ImageType> IteratorType;
129 IteratorType po(output,input->GetLargestPossibleRegion());
131 while (!pi.IsAtEnd()) {
137 //--------------------------------------------------------------------
140 //--------------------------------------------------------------------
141 template<class ImageType>
142 typename ImageType::Pointer readImage(const std::string & filename, const bool verbose)
144 typedef itk::ImageFileReader<ImageType> ReaderType;
145 typename ReaderType::Pointer reader = ReaderType::New();
146 reader->SetFileName(filename.c_str());
148 std::cout << "Reading [" << filename << "] ... " << std::endl;
152 } catch(itk::ExceptionObject & err) {
153 std::cerr << "Exception while reading image [" << filename << "]" << std::endl;
154 std::cerr << err << std::endl;
157 return reader->GetOutput();
159 //--------------------------------------------------------------------
161 //--------------------------------------------------------------------
162 template<typename ImageType>
163 typename ImageType::Pointer readImage(const std::vector<std::string> & filenames,
166 if (filenames.size() == 1) return readImage<ImageType>(filenames[0], verbose);
167 typedef itk::ImageSeriesReader<ImageType> ReaderType;
168 typename ReaderType::Pointer reader = ReaderType::New();
169 reader->SetFileNames(filenames);
171 std::cout << "Reading " << filenames[0] << " and others ..." << std::endl;
175 } catch( itk::ExceptionObject & err ) {
176 std::cerr << "Error while reading " << filenames[0]
177 << " or other files ..." << err << std::endl;
180 return reader->GetOutput();
182 //--------------------------------------------------------------------
185 //--------------------------------------------------------------------
186 template<class ImageType>
187 void writeImage(const typename ImageType::Pointer image, const std::string & filename, const bool verbose)
189 typedef itk::ImageFileWriter<ImageType> WriterType;
190 typename WriterType::Pointer writer = WriterType::New();
191 writer->SetFileName(filename.c_str());
192 writer->SetInput(image);
194 std::cout << "Writing [" << filename << "] ... " << std::endl;
198 } catch( itk::ExceptionObject & err ) {
199 std::cerr << "Exception while writing image [" << filename << "]" << std::endl;
200 std::cerr << err << std::endl;
204 //--------------------------------------------------------------------
207 //--------------------------------------------------------------------
208 template<class ImageType>
209 void writeImage(const ImageType* image, const std::string & filename, const bool verbose)
211 typedef itk::ImageFileWriter<ImageType> WriterType;
212 typename WriterType::Pointer writer = WriterType::New();
213 writer->SetFileName(filename.c_str());
214 writer->SetInput(image);
216 std::cout << "Writing [" << filename << "] ... " << std::endl;
220 } catch( itk::ExceptionObject & err ) {
221 std::cerr << "Exception while writing image [" << filename << "]" << std::endl;
222 std::cerr << err << std::endl;
226 // //--------------------------------------------------------------------
228 // //--------------------------------------------------------------------
229 // template<class ImageType>
230 // void writeImage(const typename ImageType::ConstPointer image, const std::string & filename, const bool verbose=false) {
231 // typedef itk::ImageFileWriter<ImageType> WriterType;
232 // typename WriterType::Pointer writer = WriterType::New();
233 // writer->SetFileName(filename.c_str());
234 // writer->SetInput(image);
236 // std::cout << "Writing [" << filename << "] ... " << std::endl;
241 // catch( itk::ExceptionObject & err ) {
242 // std::cerr << "Exception while writing image [" << filename << "]" << std::endl;
243 // std::cerr << err << std::endl;
248 //--------------------------------------------------------------------
250 //--------------------------------------------------------------------
251 // Compute the number of different intensities in an image
252 template<class ImageType>
253 int ComputeHowManyDifferentIntensity(const typename ImageType::Pointer & image,
254 std::vector<typename ImageType::PixelType> & l)
256 //std::set<typename ImageType::PixelType> listOfIntensities;
257 std::map<typename ImageType::PixelType, bool> listOfIntensities;
258 // listOfIntensities.resize(0);
259 typedef itk::ImageRegionConstIterator<ImageType> ConstIteratorType;
260 ConstIteratorType pi(image, image->GetLargestPossibleRegion());
262 while (!pi.IsAtEnd()) {
263 if (!listOfIntensities[pi.Get()]) listOfIntensities[pi.Get()] = true;
264 // if (std::find(listOfIntensities.begin(),
265 // listOfIntensities.end(),
266 // pi.Get()) == listOfIntensities.end()) {
267 // listOfIntensities.insert(pi.Get());
272 //typename std::set<typename ImageType::PixelType>::const_iterator ppi = listOfIntensities.begin();
273 typename std::map<typename ImageType::PixelType, bool>::const_iterator ppi = listOfIntensities.begin();
274 while (ppi != listOfIntensities.end()) {
275 l.push_back(ppi->first);
279 return listOfIntensities.size();
281 //--------------------------------------------------------------------
284 //--------------------------------------------------------------------
285 template<class InputImageType, class MaskImageType>
286 void ComputeWeightsOfEachClasses(const typename InputImageType::Pointer & input,
287 const typename MaskImageType::Pointer & mask,
288 const std::vector<typename MaskImageType::PixelType> & listOfIntensities,
289 std::map<typename MaskImageType::PixelType,
290 std::map<typename InputImageType::PixelType, double> > & mapOfLabelsAndWeights)
293 if (input->GetLargestPossibleRegion() != mask->GetLargestPossibleRegion()) {
294 itkGenericExceptionMacro(<< "Input and mask images have not the same size"
296 << "Input = " << input->GetLargestPossibleRegion()
298 << "Mask = " << mask->GetLargestPossibleRegion());
301 // reset weights list
302 mapOfLabelsAndWeights.clear();
305 typedef itk::ImageRegionConstIterator<InputImageType> ConstInputIteratorType;
306 ConstInputIteratorType pi(input, input->GetLargestPossibleRegion());
307 typedef itk::ImageRegionConstIterator<MaskImageType> ConstMaskIteratorType;
308 ConstMaskIteratorType pm(mask, mask->GetLargestPossibleRegion());
311 while (!pi.IsAtEnd()) {
312 mapOfLabelsAndWeights[pm.Get()][pi.Get()]++;
317 //--------------------------------------------------------------------
320 //--------------------------------------------------------------------
321 template<class ImageType1, class ImageType2>
322 bool HaveSameSizeAndSpacing(typename ImageType1::ConstPointer A,
323 typename ImageType2::ConstPointer B)
325 if (A->GetImageDimension() != B->GetImageDimension()) return false;
326 for(unsigned int i=0; i<A->GetImageDimension(); i++) {
327 if (A->GetSpacing()[i] != B->GetSpacing()[i]) return false;
328 if (A->GetLargestPossibleRegion().GetSize()[i] != B->GetLargestPossibleRegion().GetSize()[i]) return false;
332 //--------------------------------------------------------------------
334 //--------------------------------------------------------------------
335 template<class ImageType1, class ImageType2>
336 bool HaveSameSizeAndSpacing(typename ImageType1::Pointer A,
337 typename ImageType2::Pointer B)
339 if (A->GetImageDimension() != B->GetImageDimension()) return false;
340 for(unsigned int i=0; i<A->GetImageDimension(); i++) {
341 if (A->GetSpacing()[i] != B->GetSpacing()[i]) return false;
342 if (A->GetLargestPossibleRegion().GetSize()[i] != B->GetLargestPossibleRegion().GetSize()[i]) return false;
346 //--------------------------------------------------------------------
348 //--------------------------------------------------------------------
349 template<class ImageType1, class ImageType2>
350 bool HaveSameSpacing(typename ImageType1::ConstPointer A,
351 typename ImageType2::ConstPointer B)
353 if (A->GetImageDimension() != B->GetImageDimension()) return false;
354 for(unsigned int i=0; i<A->GetImageDimension(); i++) {
355 if (A->GetSpacing()[i] != B->GetSpacing()[i]) return false;
359 //--------------------------------------------------------------------
361 //--------------------------------------------------------------------
362 template<class ImageType1, class ImageType2>
363 bool HaveSameSpacing(typename ImageType1::Pointer A,
364 typename ImageType2::Pointer B)
366 if (A->GetImageDimension() != B->GetImageDimension()) return false;
367 for(unsigned int i=0; i<A->GetImageDimension(); i++) {
368 if (A->GetSpacing()[i] != B->GetSpacing()[i]) return false;
372 //--------------------------------------------------------------------
374 #endif /* end #define CLITKIMAGECOMMON_TXX */