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://www.centreleonberard.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
25 NewImage1D(int vsize, double vspacing)
27 typedef itk::Image<PixelType,1> ImageType;
28 typename ImageType::Pointer g = ImageType::New();
29 typename ImageType::SizeType size;
31 typename ImageType::RegionType region;
33 g->SetRegions(region);
34 typename ImageType::SpacingType spacing;
35 spacing[0] = vspacing;
36 g->SetSpacing(spacing);
39 //--------------------------------------------------------------------
41 //--------------------------------------------------------------------
42 template<class PixelType>
43 typename itk::Image<PixelType,2>::Pointer
44 NewImage2D(int sx, int sy, double dx, double dy)
46 typedef itk::Image<PixelType,2> ImageType;
47 typename ImageType::Pointer g = ImageType::New();
48 typename ImageType::SizeType size;
51 typename ImageType::RegionType region;
53 g->SetRegions(region);
54 typename ImageType::SpacingType spacing;
57 g->SetSpacing(spacing);
60 //--------------------------------------------------------------------
63 //--------------------------------------------------------------------
64 template<class PixelType>
65 typename itk::Image<PixelType,3>::Pointer
66 NewImage3D(int sx, int sy, int sz, double dx, double dy, double dz)
68 typedef itk::Image<PixelType,3> ImageType;
69 typename ImageType::Pointer g = ImageType::New();
70 typename ImageType::SizeType size;
74 typename ImageType::RegionType region;
76 g->SetRegions(region);
77 typename ImageType::SpacingType spacing;
81 g->SetSpacing(spacing);
84 //--------------------------------------------------------------------
86 //--------------------------------------------------------------------
87 template<class PixelType>
88 typename itk::Image<PixelType,4>::Pointer NewImage4D(int sx, int sy, int sz, int st, double dx, double dy, double dz, double dt)
90 typedef itk::Image<PixelType,3> ImageType;
91 typename ImageType::Pointer g = ImageType::New();
92 typename ImageType::SizeType size;
97 typename ImageType::RegionType region;
99 g->SetRegions(region);
100 typename ImageType::SpacingType spacing;
105 g->SetSpacing(spacing);
108 //--------------------------------------------------------------------
111 //--------------------------------------------------------------------
112 template<class ImageType>
113 typename ImageType::Pointer NewImageLike(const typename ImageType::Pointer input, bool allocate)
115 typename ImageType::Pointer output = ImageType::New();
116 output->CopyInformation(input);
117 output->SetRegions(input->GetLargestPossibleRegion());
118 if (allocate) output->Allocate();
121 //--------------------------------------------------------------------
124 //--------------------------------------------------------------------
125 template<class ImageType>
126 void CopyValues(const typename ImageType::Pointer input,
127 typename ImageType::Pointer output)
129 typedef itk::ImageRegionConstIterator<ImageType> ConstIteratorType;
130 ConstIteratorType pi(input,input->GetLargestPossibleRegion());
132 typedef itk::ImageRegionIterator<ImageType> IteratorType;
133 IteratorType po(output,input->GetLargestPossibleRegion());
135 while (!pi.IsAtEnd()) {
141 //--------------------------------------------------------------------
144 //--------------------------------------------------------------------
145 template<class ImageType>
146 typename ImageType::Pointer readImage(const std::string & filename, const bool verbose)
148 typedef itk::ImageFileReader<ImageType> ReaderType;
149 typename ReaderType::Pointer reader = ReaderType::New();
150 reader->SetFileName(filename.c_str());
152 std::cout << "Reading [" << filename << "] ... " << std::endl;
156 } catch(itk::ExceptionObject & err) {
157 std::cerr << "Exception while reading image [" << filename << "]" << std::endl;
158 std::cerr << err << std::endl;
161 return reader->GetOutput();
163 //--------------------------------------------------------------------
165 //--------------------------------------------------------------------
166 template<typename ImageType>
167 typename ImageType::Pointer readImage(const std::vector<std::string> & filenames,
170 if (filenames.size() == 1) return readImage<ImageType>(filenames[0], verbose);
171 typedef itk::ImageSeriesReader<ImageType> ReaderType;
172 typename ReaderType::Pointer reader = ReaderType::New();
173 reader->SetFileNames(filenames);
175 std::cout << "Reading " << filenames[0] << " and others ..." << std::endl;
179 } catch( itk::ExceptionObject & err ) {
180 std::cerr << "Error while reading " << filenames[0]
181 << " or other files ..." << err << std::endl;
184 return reader->GetOutput();
186 //--------------------------------------------------------------------
189 //--------------------------------------------------------------------
190 template<class ImageType>
191 void writeImage(const typename ImageType::Pointer image, const std::string & filename, const bool verbose, const bool compression)
193 return writeImage(image.GetPointer(), filename, verbose, compression);
195 //--------------------------------------------------------------------
198 //--------------------------------------------------------------------
199 template<class ImageType>
200 void writeImage(const ImageType* image, const std::string & filename, const bool verbose, const bool compression)
202 typedef itk::ImageFileWriter<ImageType> WriterType;
203 typename WriterType::Pointer writer = WriterType::New();
204 writer->SetFileName(filename.c_str());
205 writer->SetInput(image);
206 writer->SetUseCompression(compression);
208 std::cout << "Writing [" << filename << "] ... " << std::endl;
212 } catch( itk::ExceptionObject & err ) {
213 std::cerr << "Exception while writing image [" << filename << "]" << std::endl;
214 std::cerr << err << std::endl;
218 // //--------------------------------------------------------------------
220 // //--------------------------------------------------------------------
221 // template<class ImageType>
222 // void writeImage(const typename ImageType::ConstPointer image, const std::string & filename, const bool verbose=false) {
223 // typedef itk::ImageFileWriter<ImageType> WriterType;
224 // typename WriterType::Pointer writer = WriterType::New();
225 // writer->SetFileName(filename.c_str());
226 // writer->SetInput(image);
228 // std::cout << "Writing [" << filename << "] ... " << std::endl;
233 // catch( itk::ExceptionObject & err ) {
234 // std::cerr << "Exception while writing image [" << filename << "]" << std::endl;
235 // std::cerr << err << std::endl;
240 //--------------------------------------------------------------------
242 //--------------------------------------------------------------------
243 // Compute the number of different intensities in an image
244 template<class ImageType>
245 int ComputeHowManyDifferentIntensity(const typename ImageType::Pointer & image,
246 std::vector<typename ImageType::PixelType> & l)
248 //std::set<typename ImageType::PixelType> listOfIntensities;
249 std::map<typename ImageType::PixelType, bool> listOfIntensities;
250 // listOfIntensities.resize(0);
251 typedef itk::ImageRegionConstIterator<ImageType> ConstIteratorType;
252 ConstIteratorType pi(image, image->GetLargestPossibleRegion());
254 while (!pi.IsAtEnd()) {
255 if (!listOfIntensities[pi.Get()]) listOfIntensities[pi.Get()] = true;
256 // if (std::find(listOfIntensities.begin(),
257 // listOfIntensities.end(),
258 // pi.Get()) == listOfIntensities.end()) {
259 // listOfIntensities.insert(pi.Get());
264 //typename std::set<typename ImageType::PixelType>::const_iterator ppi = listOfIntensities.begin();
265 typename std::map<typename ImageType::PixelType, bool>::const_iterator ppi = listOfIntensities.begin();
266 while (ppi != listOfIntensities.end()) {
267 l.push_back(ppi->first);
271 return listOfIntensities.size();
273 //--------------------------------------------------------------------
276 //--------------------------------------------------------------------
277 template<class InputImageType, class MaskImageType>
278 void ComputeWeightsOfEachClasses(const typename InputImageType::Pointer & input,
279 const typename MaskImageType::Pointer & mask,
280 const std::vector<typename MaskImageType::PixelType> & listOfIntensities,
281 std::map<typename MaskImageType::PixelType,
282 std::map<typename InputImageType::PixelType, double> > & mapOfLabelsAndWeights)
285 if (input->GetLargestPossibleRegion() != mask->GetLargestPossibleRegion()) {
286 itkGenericExceptionMacro(<< "Input and mask images have not the same size"
288 << "Input = " << input->GetLargestPossibleRegion()
290 << "Mask = " << mask->GetLargestPossibleRegion());
293 // reset weights list
294 mapOfLabelsAndWeights.clear();
297 typedef itk::ImageRegionConstIterator<InputImageType> ConstInputIteratorType;
298 ConstInputIteratorType pi(input, input->GetLargestPossibleRegion());
299 typedef itk::ImageRegionConstIterator<MaskImageType> ConstMaskIteratorType;
300 ConstMaskIteratorType pm(mask, mask->GetLargestPossibleRegion());
303 while (!pi.IsAtEnd()) {
304 mapOfLabelsAndWeights[pm.Get()][pi.Get()]++;
309 //--------------------------------------------------------------------
312 //--------------------------------------------------------------------
313 template<class ImageType1, class ImageType2>
314 bool HaveSameSpacing(typename ImageType1::ConstPointer A,
315 typename ImageType2::ConstPointer B)
317 if (A->GetImageDimension() != B->GetImageDimension()) return false;
318 for(unsigned int i=0; i<A->GetImageDimension(); i++) {
319 if (A->GetSpacing()[i] != B->GetSpacing()[i]) return false;
323 //--------------------------------------------------------------------
325 //--------------------------------------------------------------------
326 template<class ImageType1, class ImageType2>
327 bool HaveSameSpacing(typename ImageType1::Pointer A,
328 typename ImageType2::Pointer B)
330 if (A->GetImageDimension() != B->GetImageDimension()) return false;
331 for(unsigned int i=0; i<A->GetImageDimension(); i++) {
332 if (A->GetSpacing()[i] != B->GetSpacing()[i]) return false;
336 //--------------------------------------------------------------------
338 //--------------------------------------------------------------------
339 template<class ImageType1, class ImageType2>
340 bool HaveSameSize(typename ImageType1::ConstPointer A,
341 typename ImageType2::ConstPointer B)
343 if (A->GetImageDimension() != B->GetImageDimension()) return false;
344 for(unsigned int i=0; i<A->GetImageDimension(); i++) {
345 if (A->GetLargestPossibleRegion().GetSize()[i] != B->GetLargestPossibleRegion().GetSize()[i]) return false;
349 //--------------------------------------------------------------------
351 //--------------------------------------------------------------------
352 template<class ImageType1, class ImageType2>
353 bool HaveSameSize(typename ImageType1::Pointer A,
354 typename ImageType2::Pointer B)
356 if (A->GetImageDimension() != B->GetImageDimension()) return false;
357 for(unsigned int i=0; i<A->GetImageDimension(); i++) {
358 if (A->GetLargestPossibleRegion().GetSize()[i] != B->GetLargestPossibleRegion().GetSize()[i]) return false;
362 //--------------------------------------------------------------------
364 //--------------------------------------------------------------------
365 template<class ImageType1, class ImageType2>
366 bool HaveSameSizeAndSpacing(typename ImageType1::ConstPointer A,
367 typename ImageType2::ConstPointer B)
369 return ( HaveSameSize<ImageType1, ImageType2>(A, B) &&
370 HaveSameSpacing<ImageType1, ImageType2>(A, B) );
372 //--------------------------------------------------------------------
374 //--------------------------------------------------------------------
375 template<class ImageType1, class ImageType2>
376 bool HaveSameSizeAndSpacing(typename ImageType1::Pointer A,
377 typename ImageType2::Pointer B)
379 return ( HaveSameSize<ImageType1, ImageType2>(A, B) &&
380 HaveSameSpacing<ImageType1, ImageType2>(A, B) );
382 //--------------------------------------------------------------------
384 #endif /* end #define CLITKIMAGECOMMON_TXX */