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
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)
193 typedef itk::ImageFileWriter<ImageType> WriterType;
194 typename WriterType::Pointer writer = WriterType::New();
195 writer->SetFileName(filename.c_str());
196 writer->SetInput(image);
198 std::cout << "Writing [" << filename << "] ... " << std::endl;
202 } catch( itk::ExceptionObject & err ) {
203 std::cerr << "Exception while writing image [" << filename << "]" << std::endl;
204 std::cerr << err << std::endl;
208 //--------------------------------------------------------------------
211 //--------------------------------------------------------------------
212 template<class ImageType>
213 void writeImage(const ImageType* image, const std::string & filename, const bool verbose)
215 typedef itk::ImageFileWriter<ImageType> WriterType;
216 typename WriterType::Pointer writer = WriterType::New();
217 writer->SetFileName(filename.c_str());
218 writer->SetInput(image);
220 std::cout << "Writing [" << filename << "] ... " << std::endl;
224 } catch( itk::ExceptionObject & err ) {
225 std::cerr << "Exception while writing image [" << filename << "]" << std::endl;
226 std::cerr << err << std::endl;
230 // //--------------------------------------------------------------------
232 // //--------------------------------------------------------------------
233 // template<class ImageType>
234 // void writeImage(const typename ImageType::ConstPointer image, const std::string & filename, const bool verbose=false) {
235 // typedef itk::ImageFileWriter<ImageType> WriterType;
236 // typename WriterType::Pointer writer = WriterType::New();
237 // writer->SetFileName(filename.c_str());
238 // writer->SetInput(image);
240 // std::cout << "Writing [" << filename << "] ... " << std::endl;
245 // catch( itk::ExceptionObject & err ) {
246 // std::cerr << "Exception while writing image [" << filename << "]" << std::endl;
247 // std::cerr << err << std::endl;
252 //--------------------------------------------------------------------
254 //--------------------------------------------------------------------
255 // Compute the number of different intensities in an image
256 template<class ImageType>
257 int ComputeHowManyDifferentIntensity(const typename ImageType::Pointer & image,
258 std::vector<typename ImageType::PixelType> & l)
260 //std::set<typename ImageType::PixelType> listOfIntensities;
261 std::map<typename ImageType::PixelType, bool> listOfIntensities;
262 // listOfIntensities.resize(0);
263 typedef itk::ImageRegionConstIterator<ImageType> ConstIteratorType;
264 ConstIteratorType pi(image, image->GetLargestPossibleRegion());
266 while (!pi.IsAtEnd()) {
267 if (!listOfIntensities[pi.Get()]) listOfIntensities[pi.Get()] = true;
268 // if (std::find(listOfIntensities.begin(),
269 // listOfIntensities.end(),
270 // pi.Get()) == listOfIntensities.end()) {
271 // listOfIntensities.insert(pi.Get());
276 //typename std::set<typename ImageType::PixelType>::const_iterator ppi = listOfIntensities.begin();
277 typename std::map<typename ImageType::PixelType, bool>::const_iterator ppi = listOfIntensities.begin();
278 while (ppi != listOfIntensities.end()) {
279 l.push_back(ppi->first);
283 return listOfIntensities.size();
285 //--------------------------------------------------------------------
288 //--------------------------------------------------------------------
289 template<class InputImageType, class MaskImageType>
290 void ComputeWeightsOfEachClasses(const typename InputImageType::Pointer & input,
291 const typename MaskImageType::Pointer & mask,
292 const std::vector<typename MaskImageType::PixelType> & listOfIntensities,
293 std::map<typename MaskImageType::PixelType,
294 std::map<typename InputImageType::PixelType, double> > & mapOfLabelsAndWeights)
297 if (input->GetLargestPossibleRegion() != mask->GetLargestPossibleRegion()) {
298 itkGenericExceptionMacro(<< "Input and mask images have not the same size"
300 << "Input = " << input->GetLargestPossibleRegion()
302 << "Mask = " << mask->GetLargestPossibleRegion());
305 // reset weights list
306 mapOfLabelsAndWeights.clear();
309 typedef itk::ImageRegionConstIterator<InputImageType> ConstInputIteratorType;
310 ConstInputIteratorType pi(input, input->GetLargestPossibleRegion());
311 typedef itk::ImageRegionConstIterator<MaskImageType> ConstMaskIteratorType;
312 ConstMaskIteratorType pm(mask, mask->GetLargestPossibleRegion());
315 while (!pi.IsAtEnd()) {
316 mapOfLabelsAndWeights[pm.Get()][pi.Get()]++;
321 //--------------------------------------------------------------------
324 //--------------------------------------------------------------------
325 template<class ImageType1, class ImageType2>
326 bool HaveSameSizeAndSpacing(typename ImageType1::ConstPointer A,
327 typename ImageType2::ConstPointer B)
329 if (A->GetImageDimension() != B->GetImageDimension()) return false;
330 for(unsigned int i=0; i<A->GetImageDimension(); i++) {
331 if (A->GetSpacing()[i] != B->GetSpacing()[i]) return false;
332 if (A->GetLargestPossibleRegion().GetSize()[i] != B->GetLargestPossibleRegion().GetSize()[i]) return false;
336 //--------------------------------------------------------------------
338 //--------------------------------------------------------------------
339 template<class ImageType1, class ImageType2>
340 bool HaveSameSizeAndSpacing(typename ImageType1::Pointer A,
341 typename ImageType2::Pointer B)
343 if (A->GetImageDimension() != B->GetImageDimension()) return false;
344 for(unsigned int i=0; i<A->GetImageDimension(); i++) {
345 if (A->GetSpacing()[i] != B->GetSpacing()[i]) return false;
346 if (A->GetLargestPossibleRegion().GetSize()[i] != B->GetLargestPossibleRegion().GetSize()[i]) return false;
350 //--------------------------------------------------------------------
352 //--------------------------------------------------------------------
353 template<class ImageType1, class ImageType2>
354 bool HaveSameSpacing(typename ImageType1::ConstPointer A,
355 typename ImageType2::ConstPointer B)
357 if (A->GetImageDimension() != B->GetImageDimension()) return false;
358 for(unsigned int i=0; i<A->GetImageDimension(); i++) {
359 if (A->GetSpacing()[i] != B->GetSpacing()[i]) return false;
363 //--------------------------------------------------------------------
365 //--------------------------------------------------------------------
366 template<class ImageType1, class ImageType2>
367 bool HaveSameSpacing(typename ImageType1::Pointer A,
368 typename ImageType2::Pointer B)
370 if (A->GetImageDimension() != B->GetImageDimension()) return false;
371 for(unsigned int i=0; i<A->GetImageDimension(); i++) {
372 if (A->GetSpacing()[i] != B->GetSpacing()[i]) return false;
376 //--------------------------------------------------------------------
378 #endif /* end #define CLITKIMAGECOMMON_TXX */