]> Creatis software - STMS.git/blob - Lib/PrePostProcessing/itkSTMS_ImageSequenceToTemporalSet_Spine.txx
First Relase on creatis's public git!
[STMS.git] / Lib / PrePostProcessing / itkSTMS_ImageSequenceToTemporalSet_Spine.txx
1 /*
2  #
3  #  File        : itkSTMS_ImageSequenceToTemporalSet_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_ImageSequenceToTemporalSetSpine_txx
61 #define itkSTMS_ImageSequenceToTemporalSetSpine_txx
62
63 #include <string> 
64 #include "itkSTMS_ImageSequenceToTemporalSet_Spine.h"
65 #include "itkSTMS_ArgumentsAnalysis_Spine.h"
66
67
68 namespace itkSTMS_Spine
69 {
70
71 template < class ImageType, class MaskImageType >
72 itkSTMS_ImageSequenceToTemporalSet < ImageType, MaskImageType >
73 ::itkSTMS_ImageSequenceToTemporalSet( ParamsAnalysisOutputType* stmsParameters )
74 {
75     // Get experiments parameters given by the user
76     this->stmsParameters = stmsParameters;
77
78     // If a mask image is specified ==> instanciation of a mask iterator (region with the highest value in the mask)
79     if( !this->stmsParameters->mask.empty() )
80     {
81         mask = MaskImageType::New();
82         mReader = MaskReaderType::New();
83         MinMaxCalculator = MinMaxCalculatorType::New();
84
85         mReader->SetFileName( this->stmsParameters->mask );
86         mReader->Update();
87         mask = mReader->GetOutput();
88
89         MinMaxCalculator->SetImage( mask );
90         MinMaxCalculator->ComputeMaximum();
91         maskValue = (MaskPixelType)MinMaxCalculator->GetMaximum();
92
93         mIt = MaskIteratorType( mask, mask->GetLargestPossibleRegion() ); //->GetBufferedRegion() );
94     }
95 }
96
97
98 template < class ImageType, class MaskImageType >
99 void
100 itkSTMS_ImageSequenceToTemporalSet< ImageType, MaskImageType >
101 ::GenerateDataSets()
102 {
103     bool first = true;
104     std::string str;
105     IndexType idx, i;
106
107     // Preprocessing of the whole image sequence
108     i=1;
109     for (std::list<std::string>::iterator it_images = stmsParameters->images.begin(); it_images != stmsParameters->images.end(); ++it_images)
110     {
111         idx = 0;
112
113         ImagePointer image
114                 = ImageType::New();
115         ReaderPointer reader
116                 = itk::ImageFileReader< ImageType >::New();
117
118         reader->SetFileName( *it_images );
119         reader->Update();
120         image = reader->GetOutput();
121
122         IteratorType it( image, image->GetLargestPossibleRegion() );
123         SizeType Size = image->GetLargestPossibleRegion().GetSize();
124
125         // If there is no mask image specified, all the pixels are extracted
126         if( stmsParameters->mask.empty() )
127         {
128             // Only the range values have to be extracted at each time-point. The others need to be extracted just one time.
129             if( first )
130             {
131                 // Containers initialisation
132                 unsigned int numSamples = (unsigned int)Size[0];
133
134                 for( unsigned int j=1 ; j<stmsParameters->dim ; ++j )
135                     numSamples *= (unsigned int)Size[j];
136
137                 indexSet   = IndexSampleSetType( numSamples, 0 );
138                 classSet   = IndexSampleSetType( numSamples, 0 );
139                 mergingSet = IndexSampleSetType( numSamples, 0 );
140                 weightsSet = IndexSampleSetType( numSamples, 1 );
141                 spatialSet = SpatialSampleSetType( numSamples, SpatialVectorType(stmsParameters->dim, 0));
142                 rangeSet   = RangeSampleSetType  ( numSamples, RangeVectorType(stmsParameters->numTimePoints, 0) );
143             }
144
145             // Samples extraction
146             it.GoToBegin();
147             while( !it.IsAtEnd() ){
148                 if( first ){
149                     indexSet[idx]    = idx;
150                     classSet[idx]    = idx+1;
151
152                     for( unsigned int j=0 ; j<stmsParameters->dim ; ++j )
153                         spatialSet[idx][j] = (SpatialType)( it.GetIndex()[j]/stmsParameters->spScales[j] );
154                 }
155
156                 rangeSet[idx++][i-1] = (PixelType)(it.Get()/stmsParameters->rScale);
157                 ++it;
158             }
159
160             if( first )
161                 first = false;
162         }
163         // If a mask is specified, just the characteristics of the pixels equal to maskValue in the mask image are extracted
164         else
165         {
166             // In this case the number of samples is not known in advance
167             if( first )
168             {
169                 indexSet   = IndexSampleSetType();
170                 classSet   = IndexSampleSetType();
171                 mergingSet = IndexSampleSetType();
172                 weightsSet = IndexSampleSetType();
173                 spatialSet = SpatialSampleSetType();
174                 rangeSet   = RangeSampleSetType();
175
176                 indexSet.reserve  ( Size[0]*Size[1] );
177                 classSet.reserve  ( Size[0]*Size[1] );
178                 mergingSet.reserve( Size[0]*Size[1] );
179                 weightsSet.reserve( Size[0]*Size[1] );
180                 spatialSet.reserve( Size[0]*Size[1] );
181                 rangeSet.reserve  ( Size[0]*Size[1] );
182             }
183
184             // Samples extraction
185             mIt.GoToBegin();
186             it.GoToBegin();
187             while( !mIt.IsAtEnd() )
188             {
189                 if(mIt.Get() == maskValue)
190                 {
191                     if( first )
192                     {
193                         SpatialVectorType spComp = SpatialVectorType(stmsParameters->dim, 0);
194                         for( unsigned int j=0 ; j<stmsParameters->dim ; ++j )
195                             spComp[j] = (SpatialType)( it.GetIndex()[j]/stmsParameters->spScales[j] );
196
197                         indexSet.push_back  ( idx    );
198                         classSet.push_back  ( ++idx  );
199                         mergingSet.push_back( 0      );
200                         weightsSet.push_back( 1      );
201                         spatialSet.push_back( spComp );
202                         rangeSet.push_back( RangeVectorType(stmsParameters->numTimePoints, (PixelType)(it.Get()/stmsParameters->rScale)) );
203                     }
204                     else
205                         rangeSet[idx++][i-1] = (PixelType)(it.Get()/stmsParameters->rScale);
206                 }
207
208                 ++mIt;
209                 ++it;
210             }
211
212             if( first )
213                 first = false;
214         }
215
216         ++i;
217     }
218 }
219
220 } // end of namespace itkSTMS
221
222 #endif