]> Creatis software - STMS.git/blob - Lib/PrePostProcessing/itkSTMS_TemporalSetToImageSequence_Spine.txx
First Relase on creatis's public git!
[STMS.git] / Lib / PrePostProcessing / itkSTMS_TemporalSetToImageSequence_Spine.txx
1 /*
2  #
3  #  File        : itkSTMS_TemporalSetToImageSequence_Spine.txx
4  #                ( C++ header file - STMS )
5  #
6  #  Description : STMS lib that implements the STMS filter and clustering.
7  #                This file is a part of the STMS Library project.
8  #                ( https://www.creatis.insa-lyon.fr/site7/fr/realisations )
9  #
10  #  [1] S. Mure, Grenier, T., Meier, S., Guttmann, R. G., et Benoit-Cattin, H.,
11  #       « Unsupervised spatio-temporal filtering of image sequences. A mean-shift specification »,
12  #       Pattern Recognition Letters, vol. 68, Part 1, p. 48 - 55, 2015.
13  #
14  #  Copyright   : Thomas GRENIER - Simon MURE
15  #                ( https://www.creatis.insa-lyon.fr/~grenier/ )
16  #
17  #  License     : CeCILL C
18  #                ( http://www.cecill.info/licences/Licence_CeCILL-C_V1-en.txt )
19  #
20  #  This software is governed by the CeCILL  license under French law and
21  #  abiding by the rules of distribution of free software.  You can  use,
22  #  modify and/ or redistribute the software under the terms of the CeCILL
23  #  license as circulated by CEA, CNRS and INRIA at the following URL
24  #  "http://www.cecill.info".
25  #
26  #  As a counterpart to the access to the source code and  rights to copy,
27  #  modify and redistribute granted by the license, users are provided only
28  #  with a limited warranty  and the software's author,  the holder of the
29  #  economic rights,  and the successive licensors  have only  limited
30  #  liability.
31  #
32  #  In this respect, the user's attention is drawn to the risks associated
33  #  with loading,  using,  modifying and/or developing or reproducing the
34  #  software by the user in light of its specific status of free software,
35  #  that may mean  that it is complicated to manipulate,  and  that  also
36  #  therefore means  that it is reserved for developers  and  experienced
37  #  professionals having in-depth computer knowledge. Users are therefore
38  #  encouraged to load and test the software's suitability as regards their
39  #  requirements in conditions enabling the security of their systems and/or
40  #  data to be ensured and,  more generally, to use and operate it in the
41  #  same conditions as regards security.
42  #
43  #  The fact that you are presently reading this means that you have had
44  #  knowledge of the CeCILL license and that you accept its terms.
45  #
46 */
47 /* Please don't forget to cite our work :
48   @article {MURE-15a,
49     title = {Unsupervised spatio-temporal filtering of image sequences. A mean-shift specification},
50     journal = {Pattern Recognition Letters},
51     volume = {68, Part 1},
52     year = {2015},
53     pages = {48 - 55},
54     issn = {0167-8655},
55     doi = {http://dx.doi.org/10.1016/j.patrec.2015.07.021},
56     url = {http://www.sciencedirect.com/science/article/pii/S0167865515002305},
57     author = {S. Mure and T Grenier and Meier, S. and Guttmann, R.G. and H. Benoit-Cattin}
58 }
59 */
60 #ifndef itkSTMS_TemporalSetToImageSequenceSpine_txx
61 #define itkSTMS_TemporalSetToImageSequenceSpine_txx
62
63 #include "itkSTMS_TemporalSetToImageSequence_Spine.h"
64 #include "itkSTMS_ArgumentsAnalysis_Spine.h"
65
66 namespace itkSTMS_Spine
67 {
68
69 template < class ImageType, class ClassImageType >
70 itkSTMS_TemporalSetToImageSequence < ImageType, ClassImageType >
71 ::itkSTMS_TemporalSetToImageSequence( IndexSampleSetType* cl, SpatialSampleSetType* sp, RangeSampleSetType* ra,
72                                       itkSTMS_Spine::ParamsAnalysisOutputType* params)
73 {
74     this->stmsParameters = params;
75     this->classSet       = cl;
76     this->spatialSet     = sp;
77     this->rangeSet       = ra;
78 }
79
80
81 template < class ImageType, class ClassImageType >
82 void
83 itkSTMS_TemporalSetToImageSequence< ImageType, ClassImageType >
84 ::GenerateImageSequence()
85 {
86     bool first = true;
87
88     // Output images allocation
89     ImagePointer image
90             = ImageType::New();
91     ReaderPointer reader
92             = ReaderType::New();
93
94     reader->SetFileName( *stmsParameters->images.begin() );
95     reader->Update();
96     image = reader->GetOutput();
97
98     SpatialIndexType start, outIndex, classIndex;
99     start.Fill(0);
100
101     RegionType region;
102     region.SetSize( image->GetBufferedRegion().GetSize());
103     region.SetIndex(start);
104
105     ClassRegionType classRegion;
106     classRegion.SetSize( image->GetBufferedRegion().GetSize());
107     classRegion.SetIndex( start );
108
109
110     std::string outputPath, classPath;
111
112     ClassImagePointer classImage
113             = ClassImageType::New();
114
115     classImage->SetRegions( classRegion );
116     classImage->Allocate();
117     classImage->FillBuffer( 15.0 );
118
119
120     // Filetered image sequence and class image saving
121     //    for( unsigned int i=1 ; i<=stmsParameters->numTimePoints ; ++i )
122     //    {
123     unsigned int i=1;
124     for (std::list<std::string>::iterator it_images = stmsParameters->images.begin(); it_images != stmsParameters->images.end(); ++it_images)
125     {
126         outputPath = stmsParameters->outFolder+ it_images->substr( it_images->find_last_of("/"), it_images->length()-stmsParameters->imageExtension.length()-it_images->find_last_of("/") ) +
127                 "_X-"+std::to_string((unsigned int)stmsParameters->spScales[0])+
128                 "_Y-"+std::to_string((unsigned int)stmsParameters->spScales[1])+
129                 "_Z-"+std::to_string(stmsParameters->spScales[2])+
130                 "_R-"+std::to_string(stmsParameters->rScale)+"_"+
131                 std::to_string(i)+stmsParameters->imageExtension;
132
133         ImagePointer outImage
134                 = ImageType::New();
135
136         outImage->SetRegions( region );
137         outImage->Allocate();
138         outImage->FillBuffer( 15.0 );
139
140         WriterPointer writer
141                 = WriterType::New();
142         writer->SetFileName( outputPath );
143
144         //#pragma omp parrallel for ...
145         for(unsigned int j=0 ; j<classSet->size() ; ++j)
146         {
147             for( unsigned int k=0 ; k<stmsParameters->dim ; ++k)
148             {
149                 outIndex[k]   = (IndexType)( round(spatialSet->at(j)[k]*stmsParameters->spScales[k]) );
150
151                 if( first )
152                     classIndex[k] = outIndex[k];
153             }
154
155             outImage->SetPixel( outIndex, rangeSet->at( classSet->at(j)-1 )[i-1]*stmsParameters->rScale );
156
157             if( first )
158                 classImage->SetPixel(classIndex, classSet->at(j));
159         }
160
161         writer->SetInput( outImage );
162         writer->Update();
163
164         if( first )
165         {
166             classPath = stmsParameters->outFolder +
167                     it_images->substr( it_images->find_last_of("/"), it_images->length()-stmsParameters->imageExtension.length()-it_images->find_last_of("/") ) +
168                     "_Class_X-"+std::to_string((unsigned int)stmsParameters->spScales[0])+
169                     "_Y-"+std::to_string((unsigned int)stmsParameters->spScales[1])+
170                     "_Z-"+std::to_string(stmsParameters->spScales[2])+
171                     "_R-"+std::to_string(stmsParameters->rScale)+"_"+
172                     stmsParameters->imageExtension;
173
174             ClassWriterPointer classWriter
175                     = ClassWriterType::New();
176             classWriter->SetFileName( classPath );
177
178             classWriter->SetInput( classImage );
179             classWriter->Update();
180             first = false;
181         }
182
183         ++i;
184     }
185 }
186
187 } // end of namespace itkSTMS
188
189 #endif