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
21 -------------------------------------------------
22 * @file clitkImageCommon.txx
23 * @author David Sarrut <david.sarrut@creatis.insa-lyon.fr>
24 * @date 07 Sep 2007 11:34:19
29 -------------------------------------------------*/
31 //--------------------------------------------------------------------
32 template<class PixelType>
33 typename itk::Image<PixelType,1>::Pointer NewImage1D(int vsize, double vspacing) {
34 typedef itk::Image<PixelType,1> ImageType;
35 typename ImageType::Pointer g = ImageType::New();
36 typename ImageType::SizeType size;
38 typename ImageType::RegionType region;
40 g->SetRegions(region);
41 typename ImageType::SpacingType spacing;
42 spacing[0] = vspacing;
43 g->SetSpacing(spacing);
46 //--------------------------------------------------------------------
48 //--------------------------------------------------------------------
49 template<class PixelType>
50 typename itk::Image<PixelType,2>::Pointer NewImage2D(int sx, int sy, double dx, double dy) {
51 typedef itk::Image<PixelType,2> ImageType;
52 typename ImageType::Pointer g = ImageType::New();
53 typename ImageType::SizeType size;
54 size[0] = sx; size[1] = sy;
55 typename ImageType::RegionType region;
57 g->SetRegions(region);
58 typename ImageType::SpacingType spacing;
59 spacing[0] = dx; spacing[1] = dy;
60 g->SetSpacing(spacing);
63 //--------------------------------------------------------------------
65 //--------------------------------------------------------------------
66 template<class PixelType>
67 typename itk::Image<PixelType,3>::Pointer 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;
71 size[0] = sx; size[1] = sy; size[2] = sz;
72 typename ImageType::RegionType region;
74 g->SetRegions(region);
75 typename ImageType::SpacingType spacing;
76 spacing[0] = dx; spacing[1] = dy; spacing[2] = dz;
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) {
85 typedef itk::Image<PixelType,3> ImageType;
86 typename ImageType::Pointer g = ImageType::New();
87 typename ImageType::SizeType size;
88 size[0] = sx; size[1] = sy; size[2] = sz; size[3] = st;
89 typename ImageType::RegionType region;
91 g->SetRegions(region);
92 typename ImageType::SpacingType spacing;
93 spacing[0] = dx; spacing[1] = dy; spacing[2] = dz; spacing[3] = dt;
94 g->SetSpacing(spacing);
97 //--------------------------------------------------------------------
100 //--------------------------------------------------------------------
101 template<class ImageType>
102 typename ImageType::Pointer NewImageLike(const typename ImageType::Pointer input, bool allocate) {
103 typename ImageType::Pointer output = ImageType::New();
104 output->SetRegions(input->GetLargestPossibleRegion());
105 output->SetOrigin(input->GetOrigin());
106 output->SetSpacing(input->GetSpacing());
107 if (allocate) output->Allocate();
110 //--------------------------------------------------------------------
113 //--------------------------------------------------------------------
114 template<class ImageType>
115 void CopyValues(const typename ImageType::Pointer input,
116 typename ImageType::Pointer output) {
117 typedef itk::ImageRegionConstIterator<ImageType> ConstIteratorType;
118 ConstIteratorType pi(input,input->GetLargestPossibleRegion());
120 typedef itk::ImageRegionIterator<ImageType> IteratorType;
121 IteratorType po(output,input->GetLargestPossibleRegion());
123 while (!pi.IsAtEnd()) {
129 //--------------------------------------------------------------------
132 //--------------------------------------------------------------------
133 template<class ImageType>
134 typename ImageType::Pointer readImage(const std::string & filename, const bool verbose) {
135 typedef itk::ImageFileReader<ImageType> ReaderType;
136 typename ReaderType::Pointer reader = ReaderType::New();
137 reader->SetFileName(filename.c_str());
139 std::cout << "Reading [" << filename << "] ... " << std::endl;
144 catch(itk::ExceptionObject & err) {
145 std::cerr << "Exception while reading image [" << filename << "]" << std::endl;
146 std::cerr << err << std::endl;
149 return reader->GetOutput();
151 //--------------------------------------------------------------------
153 //--------------------------------------------------------------------
154 template<typename ImageType>
155 typename ImageType::Pointer readImage(const std::vector<std::string> & filenames,
156 const bool verbose) {
157 if (filenames.size() == 1) return readImage<ImageType>(filenames[0], verbose);
158 typedef itk::ImageSeriesReader<ImageType> ReaderType;
159 typename ReaderType::Pointer reader = ReaderType::New();
160 reader->SetFileNames(filenames);
162 std::cout << "Reading " << filenames[0] << " and others ..." << std::endl;
167 catch( itk::ExceptionObject & err ) {
168 std::cerr << "Error while reading " << filenames[0]
169 << " or other files ..." << err << std::endl;
172 return reader->GetOutput();
174 //--------------------------------------------------------------------
176 //--------------------------------------------------------------------
177 template<class ImageType>
178 void writeImage(const typename ImageType::Pointer image, const std::string & filename, const bool verbose) {
179 typedef itk::ImageFileWriter<ImageType> WriterType;
180 typename WriterType::Pointer writer = WriterType::New();
181 writer->SetFileName(filename.c_str());
182 writer->SetInput(image);
184 std::cout << "Writing [" << filename << "] ... " << std::endl;
189 catch( itk::ExceptionObject & err ) {
190 std::cerr << "Exception while writing image [" << filename << "]" << std::endl;
191 std::cerr << err << std::endl;
195 //--------------------------------------------------------------------
197 //--------------------------------------------------------------------
198 template<class ImageType>
199 void writeImage(const ImageType* image, const std::string & filename, const bool verbose) {
200 typedef itk::ImageFileWriter<ImageType> WriterType;
201 typename WriterType::Pointer writer = WriterType::New();
202 writer->SetFileName(filename.c_str());
203 writer->SetInput(image);
205 std::cout << "Writing [" << filename << "] ... " << std::endl;
210 catch( itk::ExceptionObject & err ) {
211 std::cerr << "Exception while writing image [" << filename << "]" << std::endl;
212 std::cerr << err << std::endl;
216 // //--------------------------------------------------------------------
218 // //--------------------------------------------------------------------
219 // template<class ImageType>
220 // void writeImage(const typename ImageType::ConstPointer image, const std::string & filename, const bool verbose=false) {
221 // typedef itk::ImageFileWriter<ImageType> WriterType;
222 // typename WriterType::Pointer writer = WriterType::New();
223 // writer->SetFileName(filename.c_str());
224 // writer->SetInput(image);
226 // std::cout << "Writing [" << filename << "] ... " << std::endl;
231 // catch( itk::ExceptionObject & err ) {
232 // std::cerr << "Exception while writing image [" << filename << "]" << std::endl;
233 // std::cerr << err << std::endl;
238 //--------------------------------------------------------------------
240 //--------------------------------------------------------------------
241 // Compute the number of different intensities in an image
242 template<class ImageType>
243 int ComputeHowManyDifferentIntensity(const typename ImageType::Pointer & image,
244 std::vector<typename ImageType::PixelType> & l)
246 //std::set<typename ImageType::PixelType> listOfIntensities;
247 std::map<typename ImageType::PixelType, bool> listOfIntensities;
248 // listOfIntensities.resize(0);
249 typedef itk::ImageRegionConstIterator<ImageType> ConstIteratorType;
250 ConstIteratorType pi(image, image->GetLargestPossibleRegion());
252 while (!pi.IsAtEnd()) {
253 if (!listOfIntensities[pi.Get()]) listOfIntensities[pi.Get()] = true;
254 // if (std::find(listOfIntensities.begin(),
255 // listOfIntensities.end(),
256 // pi.Get()) == listOfIntensities.end()) {
257 // listOfIntensities.insert(pi.Get());
262 //typename std::set<typename ImageType::PixelType>::const_iterator ppi = listOfIntensities.begin();
263 typename std::map<typename ImageType::PixelType, bool>::const_iterator ppi = listOfIntensities.begin();
264 while (ppi != listOfIntensities.end()) {
265 l.push_back(ppi->first);
269 return listOfIntensities.size();
271 //--------------------------------------------------------------------
273 //--------------------------------------------------------------------
274 template<class InputImageType, class MaskImageType>
275 void ComputeWeightsOfEachClasses(const typename InputImageType::Pointer & input,
276 const typename MaskImageType::Pointer & mask,
277 const std::vector<typename MaskImageType::PixelType> & listOfIntensities,
278 std::map<typename MaskImageType::PixelType,
279 std::map<typename InputImageType::PixelType, double> > & mapOfLabelsAndWeights) {
281 if (input->GetLargestPossibleRegion() != mask->GetLargestPossibleRegion()) {
282 itkGenericExceptionMacro(<< "Input and mask images have not the same size"
284 << "Input = " << input->GetLargestPossibleRegion()
286 << "Mask = " << mask->GetLargestPossibleRegion());
289 // reset weights list
290 mapOfLabelsAndWeights.clear();
293 typedef itk::ImageRegionConstIterator<InputImageType> ConstInputIteratorType;
294 ConstInputIteratorType pi(input, input->GetLargestPossibleRegion());
295 typedef itk::ImageRegionConstIterator<MaskImageType> ConstMaskIteratorType;
296 ConstMaskIteratorType pm(mask, mask->GetLargestPossibleRegion());
299 while (!pi.IsAtEnd()) {
300 mapOfLabelsAndWeights[pm.Get()][pi.Get()]++;
305 //--------------------------------------------------------------------
307 #endif /* end #define CLITKIMAGECOMMON_TXX */