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 //--------------------------------------------------------------------
184 //--------------------------------------------------------------------
185 template<class ImageType>
186 void writeImage(const typename ImageType::Pointer image, const std::string & filename, const bool verbose)
188 typedef itk::ImageFileWriter<ImageType> WriterType;
189 typename WriterType::Pointer writer = WriterType::New();
190 writer->SetFileName(filename.c_str());
191 writer->SetInput(image);
193 std::cout << "Writing [" << filename << "] ... " << std::endl;
197 } catch( itk::ExceptionObject & err ) {
198 std::cerr << "Exception while writing image [" << filename << "]" << std::endl;
199 std::cerr << err << std::endl;
203 //--------------------------------------------------------------------
205 //--------------------------------------------------------------------
206 template<class ImageType>
207 void writeImage(const ImageType* image, const std::string & filename, const bool verbose)
209 typedef itk::ImageFileWriter<ImageType> WriterType;
210 typename WriterType::Pointer writer = WriterType::New();
211 writer->SetFileName(filename.c_str());
212 writer->SetInput(image);
214 std::cout << "Writing [" << filename << "] ... " << std::endl;
218 } catch( itk::ExceptionObject & err ) {
219 std::cerr << "Exception while writing image [" << filename << "]" << std::endl;
220 std::cerr << err << std::endl;
224 // //--------------------------------------------------------------------
226 // //--------------------------------------------------------------------
227 // template<class ImageType>
228 // void writeImage(const typename ImageType::ConstPointer image, const std::string & filename, const bool verbose=false) {
229 // typedef itk::ImageFileWriter<ImageType> WriterType;
230 // typename WriterType::Pointer writer = WriterType::New();
231 // writer->SetFileName(filename.c_str());
232 // writer->SetInput(image);
234 // std::cout << "Writing [" << filename << "] ... " << std::endl;
239 // catch( itk::ExceptionObject & err ) {
240 // std::cerr << "Exception while writing image [" << filename << "]" << std::endl;
241 // std::cerr << err << std::endl;
246 //--------------------------------------------------------------------
248 //--------------------------------------------------------------------
249 // Compute the number of different intensities in an image
250 template<class ImageType>
251 int ComputeHowManyDifferentIntensity(const typename ImageType::Pointer & image,
252 std::vector<typename ImageType::PixelType> & l)
254 //std::set<typename ImageType::PixelType> listOfIntensities;
255 std::map<typename ImageType::PixelType, bool> listOfIntensities;
256 // listOfIntensities.resize(0);
257 typedef itk::ImageRegionConstIterator<ImageType> ConstIteratorType;
258 ConstIteratorType pi(image, image->GetLargestPossibleRegion());
260 while (!pi.IsAtEnd()) {
261 if (!listOfIntensities[pi.Get()]) listOfIntensities[pi.Get()] = true;
262 // if (std::find(listOfIntensities.begin(),
263 // listOfIntensities.end(),
264 // pi.Get()) == listOfIntensities.end()) {
265 // listOfIntensities.insert(pi.Get());
270 //typename std::set<typename ImageType::PixelType>::const_iterator ppi = listOfIntensities.begin();
271 typename std::map<typename ImageType::PixelType, bool>::const_iterator ppi = listOfIntensities.begin();
272 while (ppi != listOfIntensities.end()) {
273 l.push_back(ppi->first);
277 return listOfIntensities.size();
279 //--------------------------------------------------------------------
281 //--------------------------------------------------------------------
282 template<class InputImageType, class MaskImageType>
283 void ComputeWeightsOfEachClasses(const typename InputImageType::Pointer & input,
284 const typename MaskImageType::Pointer & mask,
285 const std::vector<typename MaskImageType::PixelType> & listOfIntensities,
286 std::map<typename MaskImageType::PixelType,
287 std::map<typename InputImageType::PixelType, double> > & mapOfLabelsAndWeights)
290 if (input->GetLargestPossibleRegion() != mask->GetLargestPossibleRegion()) {
291 itkGenericExceptionMacro(<< "Input and mask images have not the same size"
293 << "Input = " << input->GetLargestPossibleRegion()
295 << "Mask = " << mask->GetLargestPossibleRegion());
298 // reset weights list
299 mapOfLabelsAndWeights.clear();
302 typedef itk::ImageRegionConstIterator<InputImageType> ConstInputIteratorType;
303 ConstInputIteratorType pi(input, input->GetLargestPossibleRegion());
304 typedef itk::ImageRegionConstIterator<MaskImageType> ConstMaskIteratorType;
305 ConstMaskIteratorType pm(mask, mask->GetLargestPossibleRegion());
308 while (!pi.IsAtEnd()) {
309 mapOfLabelsAndWeights[pm.Get()][pi.Get()]++;
314 //--------------------------------------------------------------------
316 #endif /* end #define CLITKIMAGECOMMON_TXX */