]> Creatis software - STMS.git/blob - Src/STMS_GrayLevelFiltering.cxx
V 1.5
[STMS.git] / Src / STMS_GrayLevelFiltering.cxx
1 /*
2  #
3  #  File        : STMS_GrayLevelFiltering.cxx
4  #                ( C++ example 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 V2.1
18  #                ( http://www.cecill.info/licences/Licence_CeCILL_V2.1-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 #include <iomanip>
61 #include <ctime>
62
63 #define STMS_NUMBERING_FORM_ONE "0001"
64
65 #include "itkImage.h"
66 #include "itkSTMS_ArgumentsAnalysis.h"
67 #include "itkSTMS_ImageSequenceToTemporalSet.h"
68 #include "itkSTMS_TemporalSetToImageSequence.h"
69 #include "itkSTMS_BlurringSTMS.h"
70
71 #include <array>
72
73
74
75 typedef float PixelType;
76 typedef unsigned char OutputPixelType2D;
77 typedef float OutputPixelType3D;
78
79
80 double gettime_hp()
81 {
82     struct timespec timestamp;
83
84     clock_gettime(CLOCK_REALTIME, &timestamp);
85     return timestamp.tv_sec * 1000.0 + timestamp.tv_nsec * 1.0e-6;
86 }
87
88 // Only --expDescription and numTimePoints parameter are compulsory.
89
90 //--expDescription /run/media/mure/HDD/Recherche/These/CVS/Mure/Dev/Cpp/itkSTMS/Data/P43_Parser.xml --imageDimension 2 --xScale 20 --yScale 20 --rScale 1 --epsilon 0.1 --maxIt 20 --numTimePoints 25 --merge
91
92 //--expDescription /run/media/mure/HDD/Recherche/These/CVS/Mure/Dev/Cpp/itkSTMS/Data/L43_3_Parser.xml --imageDimension 3 --xScale 1000 --yScale 1000 --zScale 1000 --rScale 0.75 --epsilon 0.1 --maxIt 50 --numTimePoints 25 --merge
93 //--expDescription /run/media/mure/HDD/Recherche/These/CVS/Mure/Dev/Cpp/itkSTMS/Data/Simu_Parser.xml --imageDimension 2 --xScale 255 --yScale 255 --rScale 0.4 --epsilon 0.01 --maxIt 50 --numTimePoints 8
94 //--expDescription /run/media/mure/HDD/Recherche/These/CVS/Mure/Dev/Cpp/itkSTMS/Data/G1_Parser.xml --imageDimension 3 --xScale 1000 --yScale 1000 --zScale 1000 --rScale 0.15 --epsilon 0.01 --maxIt 50 --numTimePoints 8
95
96 // --expDescription E:/Documents/Creatis/Projets/15_MUST/Data_Cardiac/Parser_AIF_Cardiac.xml --imageDimension 2 --xScale 10 --yScale 10 --rScale 10 --numTimePoints 80
97
98 int main(int argc, char **argv){
99     double dtime;
100
101     std::cout << "The Numbering form is set to numbers like = " << STMS_NUMBERING_FORM_ONE << std::endl;
102     std::cout << "size : " << sizeof(STMS_NUMBERING_FORM_ONE) << std::endl;
103
104
105     itkSTMS::itkSTMS_ArgumentsAnalysis* argsAnalysis
106             = new itkSTMS::itkSTMS_ArgumentsAnalysis(argc, argv);
107     argsAnalysis->Update();
108
109     itkSTMS::ParamsAnalysisOutputType* params
110             = argsAnalysis->GetSTMSParams();
111
112
113     switch(params->dim){
114         case 2:
115         {
116             typedef itk::Image< PixelType, 2 >          ImageType2D;
117             typedef itk::Image< OutputPixelType2D, 2 >  OutputImageType2D;
118             typedef itk::Image< unsigned char, 2 >      MaskImageType2D;
119
120
121             typedef itkSTMS::itkSTMS_ImageSequenceToTemporalSet< ImageType2D, MaskImageType2D >::IndexType            IndexType;
122             typedef itkSTMS::itkSTMS_ImageSequenceToTemporalSet< ImageType2D, MaskImageType2D >::SpatialType          SpatialType;
123             typedef itkSTMS::itkSTMS_ImageSequenceToTemporalSet< ImageType2D, MaskImageType2D >::IndexSampleSetType   IndexSampleSetType;
124             typedef itkSTMS::itkSTMS_ImageSequenceToTemporalSet< ImageType2D, MaskImageType2D >::SpatialSampleSetType SpatialSampleSetType;
125             typedef itkSTMS::itkSTMS_ImageSequenceToTemporalSet< ImageType2D, MaskImageType2D >::RangeSampleSetType   RangeSampleSetType;
126
127             typedef itk::Image< IndexType, 2 >  ClassImageType2D;
128
129
130             dtime=gettime_hp();
131
132             itkSTMS::itkSTMS_ImageSequenceToTemporalSet< ImageType2D, MaskImageType2D >* preProcess
133                     = new itkSTMS::itkSTMS_ImageSequenceToTemporalSet< ImageType2D, MaskImageType2D > ( params );
134             preProcess->GenerateDataSets();
135
136             dtime = gettime_hp()-dtime;
137             std::cout<<std::endl<< std::setw(30) << std::left << "Characteristics extraction: " << dtime/1000 << " s" <<std::endl;
138
139             IndexSampleSetType*   indexSet   = preProcess->GetIndexSet();
140             IndexSampleSetType*   classSet   = preProcess->GetClassSet();
141             IndexSampleSetType*   weightsSet = preProcess->GetWeightsSet();
142             SpatialSampleSetType* spatialSet = preProcess->GetSpatialSet();
143             RangeSampleSetType*   rangeSet   = preProcess->GetRangeSet();
144
145
146             dtime=gettime_hp();
147
148             itkSTMS::itkSTMS_BlurringSTMS< IndexType, SpatialType, PixelType, ImageType2D >* stmsFilter
149                     = new itkSTMS::itkSTMS_BlurringSTMS< IndexType, SpatialType, PixelType, ImageType2D >
150                     (indexSet, classSet, weightsSet, spatialSet, rangeSet,  params, preProcess->GetExperimentDescription());
151
152             stmsFilter->GenerateData();
153
154             dtime = gettime_hp()-dtime;
155             std::cout<<std::endl<< std::setw(30) << std::left << "STMS filtering: " << dtime/1000 << " s" <<std::endl;
156
157
158             dtime=gettime_hp();
159
160             itkSTMS::itkSTMS_TemporalSetToImageSequence< ImageType2D, ClassImageType2D, OutputImageType2D >* postProcess
161                     = new itkSTMS::itkSTMS_TemporalSetToImageSequence< ImageType2D, ClassImageType2D, OutputImageType2D>(stmsFilter->GetClassMemory(),
162                                                                                                        stmsFilter->GetSpatialMemory(),
163                                                                                                        stmsFilter->GetRangeSet(),
164                                                                                                        params,
165                                                                                                        preProcess->GetExperimentDescription());
166
167             postProcess->GenerateImageSequence();
168             if(preProcess->GetExperimentDescription()->outputCSV == "true"  )
169                 postProcess->GenerateCSVFile();
170
171             dtime = gettime_hp()-dtime;
172             std::cout<<std::endl<< std::setw(30) << std::left << "Image sequence saving: " << dtime/1000 << " s" <<std::endl;
173
174             delete argsAnalysis;
175             delete preProcess;
176             delete stmsFilter;
177             delete postProcess;
178             break;
179         }
180
181         case 3:
182         {
183             typedef itk::Image< PixelType, 3 >     ImageType3D;
184             typedef itk::Image< OutputPixelType3D, 3 >     OutputImageType3D;
185             typedef itk::Image< unsigned char, 3 > MaskImageType3D;
186
187             typedef itkSTMS::itkSTMS_ImageSequenceToTemporalSet< ImageType3D, MaskImageType3D >::IndexType            IndexType;
188             typedef itkSTMS::itkSTMS_ImageSequenceToTemporalSet< ImageType3D, MaskImageType3D >::SpatialType          SpatialType;
189             typedef itkSTMS::itkSTMS_ImageSequenceToTemporalSet< ImageType3D, MaskImageType3D >::IndexSampleSetType   IndexSampleSetType;
190             typedef itkSTMS::itkSTMS_ImageSequenceToTemporalSet< ImageType3D, MaskImageType3D >::SpatialSampleSetType SpatialSampleSetType;
191             typedef itkSTMS::itkSTMS_ImageSequenceToTemporalSet< ImageType3D, MaskImageType3D >::RangeSampleSetType   RangeSampleSetType;
192
193
194             dtime=gettime_hp();
195
196             itkSTMS::itkSTMS_ImageSequenceToTemporalSet< ImageType3D, MaskImageType3D >* preProcess
197                     = new itkSTMS::itkSTMS_ImageSequenceToTemporalSet< ImageType3D, MaskImageType3D > ( params );
198             preProcess->GenerateDataSets();
199
200             dtime = gettime_hp()-dtime;
201             std::cout<<std::endl<< std::setw(30) << std::left << "Characteristics extraction: " << dtime/1000 << " s" <<std::endl;
202
203             IndexSampleSetType*   indexSet   = preProcess->GetIndexSet();
204             IndexSampleSetType*   classSet   = preProcess->GetClassSet();
205             IndexSampleSetType*   weightsSet = preProcess->GetWeightsSet();
206             SpatialSampleSetType* spatialSet = preProcess->GetSpatialSet();
207             RangeSampleSetType*   rangeSet   = preProcess->GetRangeSet();
208
209
210
211
212             dtime=gettime_hp();
213
214             itkSTMS::itkSTMS_BlurringSTMS< IndexType, SpatialType, PixelType, ImageType3D >* stmsFilter
215                     = new itkSTMS::itkSTMS_BlurringSTMS< IndexType, SpatialType, PixelType, ImageType3D >
216                     (indexSet, classSet, weightsSet, spatialSet, rangeSet,  params, preProcess->GetExperimentDescription());
217
218             stmsFilter->GenerateData();
219
220             dtime = gettime_hp()-dtime;
221             std::cout<<std::endl<< std::setw(30) << std::left << "STMS filtering: " << dtime/1000 << " s" <<std::endl;
222
223
224             dtime=gettime_hp();
225
226             itkSTMS::itkSTMS_TemporalSetToImageSequence< ImageType3D, MaskImageType3D, OutputImageType3D>* postProcess
227                     = new itkSTMS::itkSTMS_TemporalSetToImageSequence< ImageType3D, MaskImageType3D, OutputImageType3D >(stmsFilter->GetClassMemory(),
228                                                                                                       stmsFilter->GetSpatialMemory(),
229                                                                                                       stmsFilter->GetRangeSet(),
230                                                                                                       params,
231                                                                                                       preProcess->GetExperimentDescription());
232
233             postProcess->GenerateImageSequence();
234             if(preProcess->GetExperimentDescription()->outputCSV == "true"  )
235                 postProcess->GenerateCSVFile();
236
237             dtime = gettime_hp()-dtime;
238             std::cout<<std::endl<< std::setw(30) << std::left << "Image sequence saving: " << dtime/1000 << " s" <<std::endl;
239
240
241             delete argsAnalysis;
242             delete preProcess;
243             delete stmsFilter;
244             delete postProcess;
245             break;
246         }
247
248         default:
249         {
250             delete argsAnalysis;
251
252             std::cout << std::endl << "Image dimensionality should be equal to 2 or 3.";
253             std::exit( EXIT_FAILURE );
254             break;
255         }
256     }
257
258     return EXIT_SUCCESS;
259 }