3 # File : itkSTMS_TemporalSetToImageSequence.txx
4 # ( C++ header file - STMS )
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 )
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.
14 # Copyright : Thomas GRENIER - Simon MURE
15 # ( https://www.creatis.insa-lyon.fr/~grenier/ )
18 # ( http://www.cecill.info/licences/Licence_CeCILL-C_V1-en.txt )
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".
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
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.
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.
47 /* Please don't forget to cite our work :
49 title = {Unsupervised spatio-temporal filtering of image sequences. A mean-shift specification},
50 journal = {Pattern Recognition Letters},
51 volume = {68, Part 1},
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}
60 #ifndef itkSTMS_TemporalSetToImageSequence_txx
61 #define itkSTMS_TemporalSetToImageSequence_txx
63 #include "itkSTMS_TemporalSetToImageSequence.h"
64 #include "itkSTMS_XMLFileParser.h"
65 #include "itkSTMS_ArgumentsAnalysis.h"
70 template < class ImageType, class ClassImageType, class OutputImageType>
71 itkSTMS_TemporalSetToImageSequence < ImageType, ClassImageType, OutputImageType >
72 ::itkSTMS_TemporalSetToImageSequence( IndexSampleSetType* cl, SpatialSampleSetType* sp, RangeSampleSetType* ra,
73 itkSTMS::ParamsAnalysisOutputType* params, itkSTMS::ParserOutputType* desc )
75 this->stmsParameters = params;
76 this->expDescription = desc;
79 this->spatialSet = sp;
84 template < class ImageType, class ClassImageType, class OutputImageType >
86 itkSTMS_TemporalSetToImageSequence< ImageType, ClassImageType, OutputImageType >
87 ::GenerateImageSequence()
91 // Output images allocation
97 std::string imagePath = expDescription->experimentPath+expDescription->inputFolder+expDescription->inputCommonRoot+STMS_NUMBERING_FORM_ONE+expDescription->imageExtension;
99 reader->SetFileName( imagePath );
101 image = reader->GetOutput();
103 SpatialIndexType start, outIndex, classIndex;
107 region.SetSize( image->GetBufferedRegion().GetSize());
108 region.SetIndex(start);
110 ClassRegionType classRegion;
111 classRegion.SetSize( image->GetBufferedRegion().GetSize());
112 classRegion.SetIndex( start );
115 std::string outputPath, classPath;
117 ClassImagePointer classImage = ClassImageType::New();
118 classImage->SetRegions( classRegion );
119 classImage->Allocate();
120 classImage->FillBuffer( 15.0 );
123 classPath = expDescription->experimentPath+expDescription->outputFolder+
124 expDescription->outputCommonRoot+
125 "_Class_X-"+std::to_string((unsigned int)stmsParameters->spScales[0])+
126 "_Y-"+std::to_string((unsigned int)stmsParameters->spScales[1])+
127 "_Z-"+std::to_string((unsigned int)stmsParameters->spScales[2])+
128 "_R-"+std::to_string(stmsParameters->rScale)+
129 expDescription->outputClassExtension;
131 ClassWriterPointer classWriter = ClassWriterType::New();
132 classWriter->SetFileName( classPath );
134 // Filetered image sequence and class image saving
135 for( unsigned int i=1 ; i<=stmsParameters->numTimePoints - stmsParameters->startTimePoint + 1; ++i )
140 if( sizeof(STMS_NUMBERING_FORM_ONE) == 6 ) sprintf( buffer, "%05d", i + stmsParameters->startTimePoint - 1);
141 if( sizeof(STMS_NUMBERING_FORM_ONE) == 5 ) sprintf( buffer, "%04d", i + stmsParameters->startTimePoint - 1);
142 if( sizeof(STMS_NUMBERING_FORM_ONE) == 4 ) sprintf( buffer, "%03d", i + stmsParameters->startTimePoint - 1);
143 if( sizeof(STMS_NUMBERING_FORM_ONE) == 3 ) sprintf( buffer, "%02d", i + stmsParameters->startTimePoint - 1);
144 if( sizeof(STMS_NUMBERING_FORM_ONE) == 2 ) sprintf( buffer, "%01d", i + stmsParameters->startTimePoint - 1);
146 outputPath = expDescription->experimentPath+expDescription->outputFolder+
147 expDescription->outputCommonRoot+
148 "_X-"+std::to_string((unsigned int)stmsParameters->spScales[0])+
149 "_Y-"+std::to_string((unsigned int)stmsParameters->spScales[1])+
150 "_Z-"+std::to_string((unsigned int)stmsParameters->spScales[2])+
151 "_R-"+std::to_string(stmsParameters->rScale)+"_"+
152 buffer+expDescription->outputImageExtension;
156 typename OutputImageType::Pointer outImage = OutputImageType::New();
157 outImage->SetRegions( region );
158 outImage->Allocate();
159 outImage->FillBuffer( 0 );
161 WriterPointer writer = WriterType::New();
162 writer->SetFileName( outputPath );
164 //#pragma omp parrallel for ...
165 for(unsigned int j=0 ; j<classSet->size() ; ++j)
167 for( unsigned int k=0 ; k<stmsParameters->dim ; ++k)
169 outIndex[k] = (IndexType)( round(spatialSet->at(j)[k]*stmsParameters->spScales[k]) );
172 classIndex[k] = outIndex[k];
175 outImage->SetPixel( outIndex, static_cast<typename OutputImageType::PixelType>(rangeSet->at( classSet->at(j)-1 )[i-1]*stmsParameters->rScale) );
178 classImage->SetPixel(classIndex, classSet->at(j));
181 writer->SetInput( outImage );
186 classWriter->SetInput( classImage );
187 classWriter->Update();
194 template < class ImageType, class ClassImageType, class OutputImageType>
196 itkSTMS_TemporalSetToImageSequence< ImageType, ClassImageType, OutputImageType>
197 ::GenerateCSVFile(const std::string &delimiter)
199 std::string CSVFilename = expDescription->experimentPath+expDescription->outputFolder+
200 expDescription->outputCommonRoot+
201 "_X-"+std::to_string((unsigned int)stmsParameters->spScales[0])+
202 "_Y-"+std::to_string((unsigned int)stmsParameters->spScales[1])+
203 "_Z-"+std::to_string((unsigned int)stmsParameters->spScales[2])+
204 "_R-"+std::to_string(stmsParameters->rScale)+".csv";
206 std::cout << "Writing CSV File (classID, nb of pixels, values of time serie) : " << CSVFilename << std::endl;
208 ofs.open (CSVFilename.c_str(), std::ofstream::out | std::ofstream::trunc);
210 for(unsigned int j=0 ; j<rangeSet->size() ; ++j)
212 ofs << j+1 << delimiter << std::count (classSet->begin(), classSet->end(), j+1);
213 ofs << delimiter << stmsParameters->startTimePoint << delimiter << stmsParameters->numTimePoints;
214 for( unsigned int i=1 ; i<=stmsParameters->numTimePoints - stmsParameters->startTimePoint + 1; ++i )
215 ofs << delimiter << rangeSet->at(j)[i-1]*stmsParameters->rScale ;
224 } // end of namespace itkSTMS