aimsalgo 6.0.0
Neuroimaging image processing
softDecSimilarityCompAnalysis.h
Go to the documentation of this file.
1/* This software and supporting documentation are distributed by
2 * Institut Federatif de Recherche 49
3 * CEA/NeuroSpin, Batiment 145,
4 * 91191 Gif-sur-Yvette cedex
5 * France
6 *
7 * This software is governed by the CeCILL-B license under
8 * French law and abiding by the rules of distribution of free software.
9 * You can use, modify and/or redistribute the software under the
10 * terms of the CeCILL-B license as circulated by CEA, CNRS
11 * and INRIA at the following URL "http://www.cecill.info".
12 *
13 * As a counterpart to the access to the source code and rights to copy,
14 * modify and redistribute granted by the license, users are provided only
15 * with a limited warranty and the software's author, the holder of the
16 * economic rights, and the successive licensors have only limited
17 * liability.
18 *
19 * In this respect, the user's attention is drawn to the risks associated
20 * with loading, using, modifying and/or developing or reproducing the
21 * software by the user in light of its specific status of free software,
22 * that may mean that it is complicated to manipulate, and that also
23 * therefore means that it is reserved for developers and experienced
24 * professionals having in-depth computer knowledge. Users are therefore
25 * encouraged to load and test the software's suitability as regards their
26 * requirements in conditions enabling the security of their systems and/or
27 * data to be ensured and, more generally, to use and operate it in the
28 * same conditions as regards security.
29 *
30 * The fact that you are presently reading this means that you have had
31 * knowledge of the CeCILL-B license and that you accept its terms.
32 */
33
34
35
36#ifndef SOFT_DECISION_SIMILAR_COMPONENT_H
37#define SOFT_DECISION_SIMILAR_COMPONENT_H
38
39#include <cartodata/volume/volume.h>
40#include <vector>
41#include <string.h>
42
43
45{
46public:
47 SoftDecisionSimilarComponent( int nbClasses, int nbVar ) ;
49
50 void init( ) ;
51
52 double doIt( const carto::rc_ptr<carto::Volume<float> >& indivMatrix ) ;
53
55 {
56 return _Rnk ;
57 }
58
59 std::vector<short> getSegmentationResult() const
60 {
61 if( (!_isInit) || _An.size() == 0 )
62 throw std::runtime_error( "Should doIt before getting result !" ) ;
63
64 std::vector<short> segRes( _nbInd ) ;
65
66 for( int ind = 0 ; ind < _nbInd ; ++ind ){
67 if( _valids[ind ] ){
68 short bestClass = 0 ;
69 for( int k = 1 ; k < _nbClasses ; ++k ){
70 if( _Rnk(ind, k) > _Rnk(ind, bestClass) )
71 bestClass = k ;
72 }
73
74 segRes[ind] = bestClass ;
75 } else
76 segRes[ind] = -1 ;
77 }
78
79 return segRes ;
80 }
81
82private:
83 int _nbClasses ;
84 int _nbVar ;
85 int _nbInd ;
86 bool _isInit ;
87 int _corrNbInd ;
88
89 std::vector<short> _labels ;
90 std::vector<bool> _valids ;
91
92 double lnLikelyhood( const carto::rc_ptr<carto::Volume<float> >& indivMatrix ) ;
93 void expectationStep( const carto::rc_ptr<carto::Volume<float> >& indivMatrix ) ;
94 void maximisationStep( const carto::rc_ptr<carto::Volume<float> >& indivMatrix ) ;
95
96 bool stopCriterion( double threshold ) ;
97
98 inline double similarity( const carto::rc_ptr<carto::Volume<float> >& indivMatrix, int ind, int k ) ;
99 inline double projection( const carto::rc_ptr<carto::Volume<float> >& indivMatrix, int ind, int k,
100 const carto::rc_ptr<carto::Volume<double> >& newek ) ;
101 std::vector<double> _pk ; // class weight
102 std::vector< carto::VolumeRef<double> > _ek ; // class mean normalized vect
103 std::vector<double> _An ;
104 std::vector<double> _newpk ; // class weight
105 std::vector< carto::VolumeRef<double> > _newek ; // class mean normalized vect
106 std::vector<double> _newAn ;
107
108 carto::VolumeRef<double> _Rnk ; // posterior proba of classes regarding to data
109} ;
110
111double
112SoftDecisionSimilarComponent::similarity( const carto::rc_ptr<carto::Volume<float> >& indivMatrix, int ind, int k )
113{
114 double norm = 0., sim = 0. ;
115 for( int t = 0 ; t < _nbVar ; ++t ){
116 norm += indivMatrix->at( ind, t ) * indivMatrix->at( ind, t ) ;
117 sim += indivMatrix->at( ind, t ) * _ek[k](t) ;
118 }
119 if( norm <= 0. )
120 return 0. ;
121 return sim / sqrt(norm) ;
122}
123
124double
125SoftDecisionSimilarComponent::projection(
126 const carto::rc_ptr<carto::Volume<float> >& indivMatrix,
127 int ind, int /* k */,
128 const carto::rc_ptr<carto::Volume<double> >& newek )
129{
130 double proj = 0. ;
131 for( int t = 0 ; t < _nbVar ; ++t ){
132 proj += indivMatrix->at( ind, t ) * newek->at(t) ;
133 //std::cout << "proj : " << proj << " with indMat = " << indivMatrix->at( ind, t ) << " && newek(k)(t) = " << newek->at(t) << std::endl ;
134 }
135
136 return proj ;
137}
138
139
140#endif
SoftDecisionSimilarComponent(int nbClasses, int nbVar)
std::vector< short > getSegmentationResult() const
double doIt(const carto::rc_ptr< carto::Volume< float > > &indivMatrix)
const carto::VolumeRef< double > & getRnk() const
AIMSDATA_API float norm(const Tensor &thing)