]> Creatis software - STMS.git/blob - Src/STMS_GrayLevelFiltering.cxx
First Relase on creatis's public git!
[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 "01"
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
77 double gettime_hp()
78 {
79     struct timespec timestamp;
80
81     clock_gettime(CLOCK_REALTIME, &timestamp);
82     return timestamp.tv_sec * 1000.0 + timestamp.tv_nsec * 1.0e-6;
83 }
84
85 // Only --expDescription and numTimePoints parameter are compulsory.
86
87 //--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
88
89 //--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
90 //--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
91 //--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
92
93 // --expDescription E:/Documents/Creatis/Projets/15_MUST/Data_Cardiac/Parser_AIF_Cardiac.xml --imageDimension 2 --xScale 10 --yScale 10 --rScale 10 --numTimePoints 80
94
95 int main(int argc, char **argv){
96     double dtime;
97
98     std::cout << "The Numbering form is set to numbers like = " << STMS_NUMBERING_FORM_ONE << std::endl;
99     std::cout << "size : " << sizeof(STMS_NUMBERING_FORM_ONE) << std::endl;
100
101
102     itkSTMS::itkSTMS_ArgumentsAnalysis* argsAnalysis
103             = new itkSTMS::itkSTMS_ArgumentsAnalysis(argc, argv);
104     argsAnalysis->Update();
105
106     itkSTMS::ParamsAnalysisOutputType* params
107             = argsAnalysis->GetSTMSParams();
108
109
110     switch(params->dim){
111         case 2:
112         {
113             typedef itk::Image< PixelType, 2 >     ImageType2D;
114             typedef itk::Image< unsigned char, 2 > MaskImageType2D;
115
116
117             typedef itkSTMS::itkSTMS_ImageSequenceToTemporalSet< ImageType2D, MaskImageType2D >::IndexType            IndexType;
118             typedef itkSTMS::itkSTMS_ImageSequenceToTemporalSet< ImageType2D, MaskImageType2D >::SpatialType          SpatialType;
119             typedef itkSTMS::itkSTMS_ImageSequenceToTemporalSet< ImageType2D, MaskImageType2D >::IndexSampleSetType   IndexSampleSetType;
120             typedef itkSTMS::itkSTMS_ImageSequenceToTemporalSet< ImageType2D, MaskImageType2D >::SpatialSampleSetType SpatialSampleSetType;
121             typedef itkSTMS::itkSTMS_ImageSequenceToTemporalSet< ImageType2D, MaskImageType2D >::RangeSampleSetType   RangeSampleSetType;
122
123             typedef itk::Image< IndexType, 2 >  ClassImageType2D;
124
125
126             dtime=gettime_hp();
127
128             itkSTMS::itkSTMS_ImageSequenceToTemporalSet< ImageType2D, MaskImageType2D >* preProcess
129                     = new itkSTMS::itkSTMS_ImageSequenceToTemporalSet< ImageType2D, MaskImageType2D > ( params );
130             preProcess->GenerateDataSets();
131
132             dtime = gettime_hp()-dtime;
133             std::cout<<std::endl<< std::setw(30) << std::left << "Characteristics extraction: " << dtime/1000 << " s" <<std::endl;
134
135             IndexSampleSetType*   indexSet   = preProcess->GetIndexSet();
136             IndexSampleSetType*   classSet   = preProcess->GetClassSet();
137             IndexSampleSetType*   weightsSet = preProcess->GetWeightsSet();
138             SpatialSampleSetType* spatialSet = preProcess->GetSpatialSet();
139             RangeSampleSetType*   rangeSet   = preProcess->GetRangeSet();
140
141
142             dtime=gettime_hp();
143
144             itkSTMS::itkSTMS_BlurringSTMS< IndexType, SpatialType, PixelType, ImageType2D >* stmsFilter
145                     = new itkSTMS::itkSTMS_BlurringSTMS< IndexType, SpatialType, PixelType, ImageType2D >
146                     (indexSet, classSet, weightsSet, spatialSet, rangeSet,  params, preProcess->GetExperimentDescription());
147
148             stmsFilter->GenerateData();
149
150             dtime = gettime_hp()-dtime;
151             std::cout<<std::endl<< std::setw(30) << std::left << "STMS filtering: " << dtime/1000 << " s" <<std::endl;
152
153
154             dtime=gettime_hp();
155
156             itkSTMS::itkSTMS_TemporalSetToImageSequence< ImageType2D, ClassImageType2D >* postProcess
157                     = new itkSTMS::itkSTMS_TemporalSetToImageSequence< ImageType2D, ClassImageType2D >(stmsFilter->GetClassMemory(),
158                                                                                                        stmsFilter->GetSpatialMemory(),
159                                                                                                        stmsFilter->GetRangeSet(),
160                                                                                                        params,
161                                                                                                        preProcess->GetExperimentDescription());
162
163             postProcess->GenerateImageSequence();
164
165             dtime = gettime_hp()-dtime;
166             std::cout<<std::endl<< std::setw(30) << std::left << "Image sequence saving: " << dtime/1000 << " s" <<std::endl;
167
168             delete argsAnalysis;
169             delete preProcess;
170             delete stmsFilter;
171             delete postProcess;
172             break;
173         }
174
175         case 3:
176         {
177             typedef itk::Image< PixelType, 3 >     ImageType3D;
178             typedef itk::Image< unsigned char, 3 > MaskImageType3D;
179
180             typedef itkSTMS::itkSTMS_ImageSequenceToTemporalSet< ImageType3D, MaskImageType3D >::IndexType            IndexType;
181             typedef itkSTMS::itkSTMS_ImageSequenceToTemporalSet< ImageType3D, MaskImageType3D >::SpatialType          SpatialType;
182             typedef itkSTMS::itkSTMS_ImageSequenceToTemporalSet< ImageType3D, MaskImageType3D >::IndexSampleSetType   IndexSampleSetType;
183             typedef itkSTMS::itkSTMS_ImageSequenceToTemporalSet< ImageType3D, MaskImageType3D >::SpatialSampleSetType SpatialSampleSetType;
184             typedef itkSTMS::itkSTMS_ImageSequenceToTemporalSet< ImageType3D, MaskImageType3D >::RangeSampleSetType   RangeSampleSetType;
185
186
187             dtime=gettime_hp();
188
189             itkSTMS::itkSTMS_ImageSequenceToTemporalSet< ImageType3D, MaskImageType3D >* preProcess
190                     = new itkSTMS::itkSTMS_ImageSequenceToTemporalSet< ImageType3D, MaskImageType3D > ( params );
191             preProcess->GenerateDataSets();
192
193             dtime = gettime_hp()-dtime;
194             std::cout<<std::endl<< std::setw(30) << std::left << "Characteristics extraction: " << dtime/1000 << " s" <<std::endl;
195
196             IndexSampleSetType*   indexSet   = preProcess->GetIndexSet();
197             IndexSampleSetType*   classSet   = preProcess->GetClassSet();
198             IndexSampleSetType*   weightsSet = preProcess->GetWeightsSet();
199             SpatialSampleSetType* spatialSet = preProcess->GetSpatialSet();
200             RangeSampleSetType*   rangeSet   = preProcess->GetRangeSet();
201
202
203
204
205             dtime=gettime_hp();
206
207             itkSTMS::itkSTMS_BlurringSTMS< IndexType, SpatialType, PixelType, ImageType3D >* stmsFilter
208                     = new itkSTMS::itkSTMS_BlurringSTMS< IndexType, SpatialType, PixelType, ImageType3D >
209                     (indexSet, classSet, weightsSet, spatialSet, rangeSet,  params, preProcess->GetExperimentDescription());
210
211             stmsFilter->GenerateData();
212
213             dtime = gettime_hp()-dtime;
214             std::cout<<std::endl<< std::setw(30) << std::left << "STMS filtering: " << dtime/1000 << " s" <<std::endl;
215
216
217             dtime=gettime_hp();
218
219             itkSTMS::itkSTMS_TemporalSetToImageSequence< ImageType3D, MaskImageType3D >* postProcess
220                     = new itkSTMS::itkSTMS_TemporalSetToImageSequence< ImageType3D, MaskImageType3D >(stmsFilter->GetClassMemory(),
221                                                                                                       stmsFilter->GetSpatialMemory(),
222                                                                                                       stmsFilter->GetRangeSet(),
223                                                                                                       params,
224                                                                                                       preProcess->GetExperimentDescription());
225
226             postProcess->GenerateImageSequence();
227
228             dtime = gettime_hp()-dtime;
229             std::cout<<std::endl<< std::setw(30) << std::left << "Image sequence saving: " << dtime/1000 << " s" <<std::endl;
230
231
232             delete argsAnalysis;
233             delete preProcess;
234             delete stmsFilter;
235             delete postProcess;
236             break;
237         }
238
239         default:
240         {
241             delete argsAnalysis;
242
243             std::cout << std::endl << "Image dimensionality should be equal to 2 or 3.";
244             std::exit( EXIT_FAILURE );
245             break;
246         }
247     }
248
249     return EXIT_SUCCESS;
250 }