22 #include "itkScalarImageToHistogramGenerator.h"
23 #include "itkListSample.h"
25 #define _USE_MATH_DEFINES
34 : m_MaxIt(100), m_StepsNumIntegration(100)
46 int s = h.
xVals.size();
50 params->means[0] = h.
xVals(s30);
51 params->means[1] = h.
xVals(s70);
52 params->sigmas[0] = (h.
xVals(s-1) - h.
xVals(0)) / 5.0;
53 params->sigmas[1] = (h.
xVals(s-1) - h.
xVals(0)) / 5.0;
64 params->Initialize(initialGuess);
66 double ll = 9999999999999999999.9;
70 int arraysz = h.
xVals.size();
72 for (
unsigned int it = 0; it <
m_MaxIt; it++)
78 for(
int i=0; i<arraysz; i++)
82 curves.
combiVals(i) = 0.00000000000000000001;
91 for(
int i=0; i<arraysz; i++)
96 if( it>2 && oll-ll < arraysz/2*1e-15 )
101 for(
int j=0; j<2; j++)
104 array = curves.
vals[j];
106 for(
int i=0; i<arraysz; i++)
112 params->means[j] = 0;
113 for(
int i=0; i<arraysz; i++)
115 params->ps[j] += p(i);
116 params->means[j] += h.
xVals(i)*p(i);
118 params->means[j] /= params->ps[j];
121 for(
int i=0; i<arraysz; i++)
123 vr(i) -= params->means[j];
126 params->sigmas[j] = 0;
127 for(
int i=0; i<arraysz; i++)
129 params->sigmas[j] += vr(i)*vr(i)*p(i);
131 params->sigmas[j] /= params->ps[j];
132 params->sigmas[j] += sml;
137 for(
int i=0; i<arraysz; i++)
144 for(
int j=0; j<2; j++)
146 params->ps[j] = params->ps[j] + 1e-3;
147 sum += params->ps[j];
151 for(
int j=0; j<2; j++)
153 params->ps[j] = params->ps[j] / sum;
165 double sum1=0, sum2=0, sum3=0;
166 int arraysz = curves->
vals[0].size();
168 for(
int i=0; i<arraysz; i++)
170 sum1 += curves->
vals[0](i);
171 sum2 += curves->
vals[1](i);
175 sum1 /= params.
ps[0];
176 sum2 /= params.
ps[1];
177 sum3 /= 1.0 -params.
ps[0] -params.
ps[1];
179 for(
int i=0; i<arraysz; i++)
181 curves->
vals[0](i) /= sum1;
182 curves->
vals[1](i) /= sum2;
186 for(
int i=0; i<arraysz; i++)
196 int arraysz = xVals.size();
199 for(
int j=0; j<2; j++)
201 for(
int i=0; i<arraysz; i++)
203 double d = xVals(i)-params.
means[j];
206 result.
vals[j](i) = amp*exp(-0.5 * (d*d)/params.
sigmas[j]);
210 for(
int i=0; i<arraysz; i++)
217 for(
int i=0; i<arraysz; i++)
219 double d = xVals(i)-(t*params.
means[0]+(1-t)*params.
means[1]);
221 double p = 1.0 - params.
ps[0] - params.
ps[1];
233 for(
int i=0; i<arraysz; i++)
258 switch(rgbChannels->r->GetDimension())
261 InternalGenerateRGB<2>(rgbChannels, outImage);
264 InternalGenerateRGB<3>(rgbChannels, outImage);
267 InternalGenerateRGB<4>(rgbChannels, outImage);
270 InternalGenerateRGB<3>(rgbChannels, outImage);
275 retval->rgbChannels = rgbChannels;
276 retval->rgb = outImage;
277 retval->params = resultr->
params;
278 retval->result = resultr->
result;
279 retval->hist = resultr->
hist;
288 template <
unsigned int VImageDimension >
291 typedef itk::Image< float, VImageDimension > ProbImageType;
292 typedef itk::Image< typename itk::RGBAPixel<unsigned char>, VImageDimension > RGBImageType;
296 castFilter->SetInput( rgbin->
r );
297 castFilter->Update();
302 castFilter->SetInput( rgbin->
g );
303 castFilter->Update();
307 rgb->SetSpacing( g->GetSpacing() );
308 rgb->SetOrigin( g->GetOrigin() );
309 rgb->SetDirection( g->GetDirection() );
310 rgb->SetRegions( g->GetLargestPossibleRegion() );
313 itk::ImageRegionConstIterator<ProbImageType>
314 itr(r, r->GetLargestPossibleRegion());
315 itk::ImageRegionConstIterator<ProbImageType>
316 itg(g, g->GetLargestPossibleRegion());
318 itk::ImageRegionIterator<RGBImageType>
319 itrgb(rgb, rgb->GetLargestPossibleRegion());
327 while( !itr.IsAtEnd() )
350 while( !itr.IsAtEnd() )
357 float valr = (pr/maxr)*255.0f;
358 float valg = (pg/maxg)*255.0f;
359 float alpha = valr>valg ? valr : valg;
360 prgb.Set(valr, valg, 0.0f, alpha);
369 retval->InitializeByItk(rgb.GetPointer());
370 retval->SetVolume(rgb->GetBufferPointer());
382 if(precResult ==
nullptr)
385 retval->hist->InitByMitkHistogram(histogram);
393 retval->params->Initialize(¶ms);
395 retval->result->Initialize(&result);
400 retval->params->Initialize(precResult->
params);
402 retval->result->Initialize(precResult->
result);
405 VecType totalProbs = retval->result->combiVals;
406 VecType pvProbs = retval->result->mixedVals[0];
412 double p_interesting;
415 fiberProbs = retval->result->vals[1];
416 nonFiberProbs = retval->result->vals[0];
417 p_fiber = retval->params->ps[1];
418 p_nonFiber = retval->params->ps[0];
431 interestingProbs = nonFiberProbs;
432 p_interesting = p_nonFiber;
435 interestingProbs = pvProbs;
436 p_interesting = 1 - p_fiber - p_nonFiber;
440 interestingProbs = fiberProbs;
441 p_interesting = p_fiber;
445 double sum = histogram->GetTotalFrequency();
448 MitkHistType::MeasurementVectorType
min(1);
449 MitkHistType::MeasurementVectorType
max(1);
450 min.Fill(histogram->GetDimensionMins(0)[0]);
451 max.Fill(histogram->GetDimensionMaxs(0)[histogram->GetDimensionMaxs(0).size()-1]);
454 interestingHist->SetMeasurementVectorSize(1);
455 interestingHist->Initialize(histogram->GetSize(),
min,
max);
456 MitkHistType::Iterator newIt = interestingHist->Begin();
457 MitkHistType::Iterator newEnd = interestingHist->End();
460 totalHist->SetMeasurementVectorSize(1);
461 totalHist->Initialize(histogram->GetSize(),
min,
max);
462 MitkHistType::Iterator totalIt = totalHist->Begin();
465 while (newIt != newEnd)
467 newIt.SetFrequency(interestingProbs(i)*sum);
468 totalIt.SetFrequency(totalProbs(i)*sum);
487 outImage1, outImage2);
489 retval->clusteredImage = outImage1;
490 retval->displayImage = outImage2;
496 template <
typename TPixel,
unsigned int VImageDimension >
498 const itk::Image< TPixel, VImageDimension > *image,
503 typedef itk::Image< TPixel, VImageDimension >
ImageType;
504 typedef itk::Image< itk::RGBAPixel<unsigned char>, VImageDimension > DisplayImageType;
505 typedef itk::Image< float, VImageDimension > ProbImageType;
508 probimage->SetSpacing( image->GetSpacing() );
509 probimage->SetOrigin( image->GetOrigin() );
510 probimage->SetDirection( image->GetDirection() );
511 probimage->SetRegions( image->GetLargestPossibleRegion() );
512 probimage->Allocate();
513 probimage->FillBuffer(0);
516 displayimage->SetSpacing( image->GetSpacing() );
517 displayimage->SetOrigin( image->GetOrigin() );
518 displayimage->SetDirection( image->GetDirection() );
519 displayimage->SetRegions( image->GetLargestPossibleRegion() );
520 displayimage->Allocate();
523 rgba.Set(0.0f, 0.0f, 0.0f, 0.0f);
524 displayimage->FillBuffer(rgba);
526 itk::ImageRegionConstIterator<ImageType>
527 itimage(image, image->GetLargestPossibleRegion());
529 itk::ImageRegionIterator<ProbImageType>
530 itprob(probimage, probimage->GetLargestPossibleRegion());
532 itk::ImageRegionIterator<DisplayImageType>
533 itdisp(displayimage, displayimage->GetLargestPossibleRegion());
538 MitkHistType::IndexType index(1);
540 while( !itimage.IsAtEnd() )
544 MitkHistType::MeasurementVectorType meas(1);
545 meas.Fill(itimage.Get());
546 double aposteriori = 0;
550 double aprioriProb = clusterResults.
interestingHist->GetFrequency(index);
551 double intensityProb = clusterResults.
totalHist->GetFrequency(index);
553 aposteriori = p_interesting * aprioriProb / intensityProb;
560 if(aposteriori > 0.0000000000000001)
562 itprob.Set( aposteriori );
563 maxp = aposteriori > maxp ? aposteriori : maxp;
578 while( !itprob.IsAtEnd() )
583 rgba.Set(255.0f, 0.0f, 0.0f, 255.0f*(itprob.Get()/maxp));
590 outImage1->InitializeByItk(probimage.GetPointer());
591 outImage1->SetVolume(probimage->GetBufferPointer());
593 outImage2->InitializeByItk(displayimage.GetPointer());
594 outImage2->SetVolume(displayimage->GetBufferPointer());
603 auto retval =
new double[2];
609 clusteredImage.GetPointer(),
616 template <
typename TPixel,
unsigned int VImageDimension >
618 const itk::Image< TPixel, VImageDimension > *image,
621 typedef itk::Image< TPixel, VImageDimension >
ImageType;
622 typedef itk::Image< float, VImageDimension > ProbImageType;
623 typedef itk::Image< unsigned char, VImageDimension >
MaskImageType;
627 castFilter->SetInput( clusteredImage );
628 castFilter->Update();
636 castFilter2->SetInput( mask );
637 castFilter2->Update();
638 itkmask = castFilter2->GetOutput();
643 itkmask->SetSpacing( clusterImage->GetSpacing() );
644 itkmask->SetOrigin( clusterImage->GetOrigin() );
645 itkmask->SetDirection( clusterImage->GetDirection() );
646 itkmask->SetRegions( clusterImage->GetLargestPossibleRegion() );
648 itkmask->FillBuffer(1);
651 itk::ImageRegionConstIterator<ImageType>
652 itimage(image, image->GetLargestPossibleRegion());
654 itk::ImageRegionConstIterator<ProbImageType>
655 itprob(clusterImage, clusterImage->GetLargestPossibleRegion());
657 itk::ImageRegionConstIterator<MaskImageType>
658 itmask(itkmask, itkmask->GetLargestPossibleRegion());
664 double totalProb = 0;
665 double measurement = 0;
668 while( !itimage.IsAtEnd() && !itprob.IsAtEnd() && !itmask.IsAtEnd() )
670 double valImag = itimage.Get();
671 double valProb = itprob.Get();
672 double valMask = itmask.Get();
677 measurement += valImag * prop;
678 error += valImag * valImag * prop;
685 measurement = measurement / totalProb;
686 error = error / totalProb;
687 retval[0] = measurement;
688 retval[1] = sqrt( error - measurement*measurement );
701 caster->SetInput(comp1);
706 caster->SetInput(comp2);
711 caster->SetInput(probImg);
717 itk::ImageRegionConstIterator<ImageType>
718 itprob(probImage, probImage->GetLargestPossibleRegion());
720 while( !itprob.IsAtEnd() )
722 maxProb = itprob.Get() > maxProb ? itprob.Get() : maxProb;
728 typedef float MeasurementType;
729 const unsigned int MeasurementVectorLength = 2;
730 typedef itk::Vector< MeasurementType , MeasurementVectorLength >
731 MeasurementVectorType;
732 typedef itk::Statistics::ListSample< MeasurementVectorType > ListSampleType;
734 listSample->SetMeasurementVectorSize( MeasurementVectorLength );
736 itk::ImageRegionIterator<ImageType>
737 it1(comp1Image, comp1Image->GetLargestPossibleRegion());
738 itk::ImageRegionIterator<ImageType>
739 it2(comp2Image, comp2Image->GetLargestPossibleRegion());
745 while( !itprob.IsAtEnd() )
747 if(itprob.Get() > 0.2 * maxProb)
749 MeasurementVectorType mv;
750 mv[0] = ( MeasurementType ) it1.Get();
751 mv[1] = ( MeasurementType ) it2.Get();
752 listSample->PushBack(mv);
760 typedef float HistogramMeasurementType;
761 typedef itk::Statistics::Histogram< HistogramMeasurementType, itk::Statistics::DenseFrequencyContainer2 > HistogramType;
762 typedef itk::Statistics::SampleToHistogramFilter< ListSampleType, HistogramType > GeneratorType;
765 GeneratorType::HistogramType::SizeType size(2);
767 generator->SetHistogramSize( size );
769 generator->SetInput( listSample );
770 generator->SetMarginalScale( 10.0 );
776 GeneratorType::HistogramType::ConstIterator iter = histogram->Begin();
778 MeasurementVectorType maxValue;
780 while ( iter != histogram->End() )
782 if(iter.GetFrequency() > maxFreq)
784 maxFreq = iter.GetFrequency();
785 maxValue[0] = iter.GetMeasurementVector()[0];
786 maxValue[1] = iter.GetMeasurementVector()[1];
794 returnImage->SetSpacing( comp1Image->GetSpacing() );
795 returnImage->SetOrigin( comp1Image->GetOrigin() );
796 returnImage->SetDirection( comp1Image->GetDirection() );
797 returnImage->SetRegions( comp1Image->GetLargestPossibleRegion() );
798 returnImage->Allocate();
800 itk::ImageRegionConstIterator<ImageType>
801 cit1(comp1Image, comp1Image->GetLargestPossibleRegion());
803 itk::ImageRegionConstIterator<ImageType>
804 cit2(comp2Image, comp2Image->GetLargestPossibleRegion());
806 itk::ImageRegionIterator<ImageType>
807 itout(returnImage, returnImage->GetLargestPossibleRegion());
813 vnl_vector<float> v(3);
814 v[0] = cos( maxValue[0] ) * sin( maxValue[1] );
815 v[1] = sin( maxValue[0] ) * sin( maxValue[1] );
816 v[2] = cos( maxValue[1] );
818 while( !cit1.IsAtEnd() )
820 vnl_vector<float> v1(3);
821 v1[0] = cos( cit1.Get() ) * sin( cit2.Get() );
822 v1[1] = sin( cit1.Get() ) * sin( cit2.Get() );
823 v1[2] = cos( cit2.Get() );
825 itout.Set(fabs(angle(v,v1)));
834 retval->InitializeByItk(returnImage.GetPointer());
835 retval->SetVolume(returnImage->GetBufferPointer());
856 switch(rgbChannels->r->GetDimension())
859 InternalGenerateRGB<2>(rgbChannels, outImage);
862 InternalGenerateRGB<3>(rgbChannels, outImage);
865 InternalGenerateRGB<4>(rgbChannels, outImage);
868 InternalGenerateRGB<3>(rgbChannels, outImage);
873 retval->rgbChannels = rgbChannels;
874 retval->rgb = outImage;
875 retval->params = resultr->
params;
876 retval->result = resultr->
result;
877 retval->hist = resultr->
hist;
894 retval->hist->InitByMitkHistogram(histogram);
896 auto q =
new double[2];
897 q[0] = histogram->Quantile(0, p1);
898 q[1] = histogram->Quantile(0, p2);
907 outImage1, outImage2);
909 retval->clusteredImage = outImage1;
910 retval->displayImage = outImage2;
917 template <
typename TPixel,
unsigned int VImageDimension >
919 const itk::Image< TPixel, VImageDimension > *image,
double* q,
923 typedef itk::Image< TPixel, VImageDimension >
ImageType;
924 typedef itk::Image< itk::RGBAPixel<unsigned char>, VImageDimension > DisplayImageType;
925 typedef itk::Image< float, VImageDimension > ProbImageType;
928 probimage->SetSpacing( image->GetSpacing() );
929 probimage->SetOrigin( image->GetOrigin() );
930 probimage->SetDirection( image->GetDirection() );
931 probimage->SetRegions( image->GetLargestPossibleRegion() );
932 probimage->Allocate();
933 probimage->FillBuffer(0);
936 displayimage->SetSpacing( image->GetSpacing() );
937 displayimage->SetOrigin( image->GetOrigin() );
938 displayimage->SetDirection( image->GetDirection() );
939 displayimage->SetRegions( image->GetLargestPossibleRegion() );
940 displayimage->Allocate();
943 rgba.Set(0.0f, 0.0f, 0.0f, 0.0f);
944 displayimage->FillBuffer(rgba);
946 itk::ImageRegionConstIterator<ImageType>
947 itimage(image, image->GetLargestPossibleRegion());
949 itk::ImageRegionIterator<ProbImageType>
950 itprob(probimage, probimage->GetLargestPossibleRegion());
952 itk::ImageRegionIterator<DisplayImageType>
953 itdisp(displayimage, displayimage->GetLargestPossibleRegion());
958 while( !itimage.IsAtEnd() )
960 if(itimage.Get() > q[0] && itimage.Get() < q[1])
972 while( !itprob.IsAtEnd() )
977 rgba.Set(255.0f, 0.0f, 0.0f, 255.0f);
984 outImage1->InitializeByItk(probimage.GetPointer());
985 outImage1->SetVolume(probimage->GetBufferPointer());
987 outImage2->InitializeByItk(displayimage.GetPointer());
988 outImage2->SetVolume(displayimage->GetBufferPointer());
unsigned int m_StepsNumIntegration
mitk::Image::HistogramType MitkHistType
itk::SmartPointer< Self > Pointer
std::vector< VecType > vals
void Normalize(ParamsType params, ClusterResultType *curves) const
HelperStructPerformClusteringRetval * PerformQuantiles(mitk::Image::ConstPointer image, const MitkHistType *histogram, double p1, double p2) const
MitkHistType * interestingHist
HelperStructPerformClusteringRetval * PerformClustering(mitk::Image::ConstPointer image, const MitkHistType *histogram, int classIdent, HelperStructPerformClusteringRetval *precResult=nullptr) const
DataCollection - Class to facilitate loading/accessing structured data.
#define AccessFixedDimensionByItk_3(mitkImage, itkImageTypeFunction, dimension, arg1, arg2, arg3)
itk::SmartPointer< const Self > ConstPointer
virtual ~PartialVolumeAnalysisClusteringCalculator()
map::core::discrete::Elements< 3 >::InternalImageType ImageType
void InternalQuantify(const itk::Image< TPixel, VImageDimension > *image, mitk::Image::Pointer clusteredImage, double *retval, mitk::Image::Pointer mask) const
HelperStructPerformRGBClusteringRetval * PerformRGBQuantiles(mitk::Image::ConstPointer image, const MitkHistType *histogram, double p1, double p2) const
double * PerformQuantification(mitk::Image::ConstPointer image, mitk::Image::Pointer clusteredImage, mitk::Image::Pointer mask=nullptr) const
std::vector< VecType > mixedVals
mitk::Image::Pointer CaculateAngularErrorImage(mitk::Image::Pointer comp1, mitk::Image::Pointer comp2, mitk::Image::Pointer probImg) const
itk::Image< unsigned char, 3 > MaskImageType
HelperStructPerformRGBClusteringRetval * PerformRGBClustering(mitk::Image::ConstPointer image, const MitkHistType *histogram) const
ParamsType * InitialGuess(HistType h) const
ParamsType * Cluster(const HistType h, ParamsType *initialGuess) const
PartialVolumeAnalysisClusteringCalculator()
void InternalGenerateRGB(HelperStructRGBChannels *rgb, mitk::Image::Pointer retval) const
void InternalGenerateQuantileImage(const itk::Image< TPixel, VImageDimension > *image, double *q, mitk::Image::Pointer outImage1, mitk::Image::Pointer outImage2) const
vnl_vector< double > VecType
void Initialize(const ParamsType *p)
void InternalGenerateProbabilityImage(const itk::Image< TPixel, VImageDimension > *image, const HelperStructClusteringResults clusterResults, mitk::Image::Pointer outImage1, mitk::Image::Pointer outImage2) const
ClusterResultType CalculateCurves(ParamsType params, VecType xVals) const
static itkEventMacro(BoundingShapeInteractionEvent, itk::AnyEvent) class MITKBOUNDINGSHAPE_EXPORT BoundingShapeInteractor Pointer New()
Basic interaction methods for mitk::GeometryData.