56 const std::list< Point3d >& selectedPoints,
60 data->getSizeT(), 1, 1,
61 carto::AllocatorContext::fast() );
63 std::list< Point3d >::const_iterator iter( selectedPoints.begin() ), last( selectedPoints.end() ) ;
67 while( iter != last ){
69 for(
int t = 0 ; t < data->getSizeT() ; ++t )
70 individuals( i, t ) = T(data->at( (*iter)[0], (*iter)[1], (*iter)[2], t ) ) ;
85 int nbFrame = individuals->getSizeY() ;
88 individuals->getSizeX(), individuals->getSizeY(), 1, 1,
89 carto::AllocatorContext::fast() );
91 carto::AllocatorContext::fast() );
94 for(
int ind = 0 ; ind < individuals->getSizeX() ; ++ind )
95 for(
int t = 0 ; t < individuals->getSizeY() ; ++t ){
96 val = individuals->at( ind, t ) ;
101 double meanMean = 0. ;
104 for(
int t = 0 ; t < nbFrame ; ++t )
107 for(
int ind = 0 ; ind < centeredIndivMatrix->
getSizeX() ; ++ind )
109 centeredIndivMatrix( ind, t ) = individuals->
at( ind, t ) -
_mean(t) ;
110 meanMean += centeredIndivMatrix( ind, t ) ;
118 for( y1=0; y1<dy1; ++y1 )
119 for( x1=0; x1<dx1; ++x1 )
121 for(
int k=0; k < centeredIndivMatrix->
getSizeX() ;++k)
122 matVarCov(x1, y1) += centeredIndivMatrix(k, x1) * centeredIndivMatrix(k, y1);
123 matVarCov(x1, y1) /= centeredIndivMatrix->
getSizeX() - 1 ;
132 carto::AllocatorContext::fast() );
139 carto::AllocatorContext::fast() ) ;
152 std::cout <<
"signif EV = " <<
_significantEV <<
" sig2 = " << sig2 << std::endl ;
154 double lnSig2 = log(sig2) ;
155 double sqrtSig2 = sqrt(sig2) ;
157 double lnDetVarCov = 0 ;
161 lnDetVarCov += 0.5 * log( w( i, i ) ) ;
163 invW( i, i ) = 1.0f / w( i, i );
167 lnDetVarCov += 0.5 * lnSig2 ;
170 invW( i, i ) = 1.0f / sig2 ;
190 _invVarCov = carto::matrix_product( carto::transpose( u ),
191 carto::matrix_product( invW, u ) );
197 for( y1=0; y1<dy1; ++y1 )
198 for( x1=0; x1<dx1; ++x1 )
207 std::cerr <<
"Bad var cov matrix !" << std::endl ;
208 throw std::runtime_error(
"Bad var cov matrix !") ;
321 const std::vector< std::list< Point3d > >& classes,
322 int significantEV,
const std::vector<double>& PIj )
323 : _significantEV(significantEV <= 0 ? data->getSizeT() : significantEV),
324 _classes(classes), _data(data), _PIj(PIj)
326 if( PIj.size() != 0 )
327 if( PIj.size() == classes.size() )
330 std::cerr <<
"Inconsistant data : number of classes and number of classes prior probabilities are different !" << std::endl ;
332 throw std::runtime_error(
"Inconsistant data : number of classes and number of classes prior probabilities are different !") ;
335 _PIj.reserve( _classes.size() ) ;
336 for( unsigned int c = 0 ; c < _classes.size() ; ++c )
337 _PIj.push_back( 1./_classes.size() ) ;
349 _discrElements.reserve( _classes.size() ) ;
350 for(
unsigned int c = 0 ; c < _classes.size() ; ++c ){
351 DiscriminantAnalysisElement el( significantEV, _PIj[c] ) ;
352 el.doIt( _classes[c], _data ) ;
355 std::cout <<
"Class " << c <<
" done !" << std::endl ;
357 _discrElements.push_back( el ) ;
359 std::cout <<
"Discriminant analysis initization completed" << std::endl ;
420 if( dynamicImage->getSizeT() != _data->getSizeT() )
422 if( segmented->getSizeX() != _data->getSizeX() && segmented->getSizeY() != _data->getSizeY()
423 && segmented->getSizeZ() != _data->getSizeZ() )
426 segmented->setVoxelSize( _data->getVoxelSize() );
430 carto::AllocatorContext::fast() ) ;
432 int dx = dynamicImage->getSizeX(), dy = dynamicImage->getSizeY(),
433 dz = dynamicImage->getSizeZ();
435 for( z=0; z<dz; ++z )
436 for( y=0; y<dy; ++y )
437 for( x=0; x<dx; ++x )
439 if(
mask->at(x, y, z) )
441 std::cout <<
"x : " << x <<
", y : " << y <<
", z : " << z << std::endl ;
442 for(
int t = 0 ; t < _data->getSizeT() ; ++t )
443 indiv(t) = dynamicImage->
at(x, y, z, t ) ;
444 segmented->at(x, y, z) =
affectedTo( indiv ) ;
448 std::cout <<
"Discriminant analysis classification completed" << std::endl ;
462 = ( indivPriorProbabilities->getSizeX() == 1
463 && indivPriorProbabilities->getSizeY() == 1
464 && indivPriorProbabilities->getSizeZ() == 1 );
471 int dx =
mask->getSizeX(), dy =
mask->getSizeY(), dz =
mask->getSizeZ();
473 for( z=0; z<dz; ++z )
474 for( y=0; y<dy; ++y )
475 for( x=0; x<dx; ++x )
476 if(
mask->at( x, y, z ) )
480 if( dynamicImage->getSizeT() != _data->getSizeT() )
482 if( fuzzySegmented->getSizeX() != _data->getSizeX()
483 && fuzzySegmented->getSizeY() != _data->getSizeY()
484 && fuzzySegmented->getSizeZ() != _data->getSizeZ() &&
485 fuzzySegmented->getSizeT() !=
int(_classes.size() ) )
488 _data->getSizeZ(), _classes.size() ) ;
489 fuzzySegmented->setVoxelSize( _data->getVoxelSize() ) ;
492 std::vector<double> probabilityRepartition ;
494 carto::AllocatorContext::fast() ) ;
496 int dx = dynamicImage->getSizeX(), dy = dynamicImage->getSizeY(),
497 dz = dynamicImage->getSizeZ();
499 for( z=0; z<dz; ++z )
500 for( y=0; y<dy; ++y )
501 for( x=0; x<dx; ++x )
503 if(
mask->at(x, y, z) )
506 for(
int t = 0 ; t < _data->getSizeT() ; ++t )
507 indiv(t) = dynamicImage->
at(x, y, z, t ) ;
511 for(
int c = 0 ; c < fuzzySegmented->getSizeT() ; ++c )
512 fuzzySegmented->at(x, y, z, c) = probabilityRepartition[c] ;
516 std::cout <<
"Discriminant analysis : fuzzy classification completed"
bool fuzzyClassification(const carto::rc_ptr< carto::Volume< T > > &dynamicImage, const carto::rc_ptr< carto::Volume< byte > > &mask, carto::rc_ptr< carto::Volume< float > > &fuzzySegmented, const carto::rc_ptr< carto::Volume< double > > &indivPriorProbabilities=carto::rc_ptr< carto::Volume< double > >())