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