From 21312c472d4ae614822f175153362443f27c9aa2 Mon Sep 17 00:00:00 2001 From: dsarrut Date: Wed, 3 Mar 2010 12:42:06 +0000 Subject: [PATCH] - add stuffs needed in EllipseFitting --- common/clitkSignal.cxx | 409 ++++++++++++++++++++++++++++++++--------- common/clitkSignal.h | 17 +- 2 files changed, 336 insertions(+), 90 deletions(-) diff --git a/common/clitkSignal.cxx b/common/clitkSignal.cxx index 25a7db0..825243d 100644 --- a/common/clitkSignal.cxx +++ b/common/clitkSignal.cxx @@ -2,12 +2,33 @@ #define CLITKSIGNAL_CXX #include "clitkSignal.h" +#include namespace clitk { + //--------------------------------------------------------------------- + Signal::Signal(const Signal & s) { // Copy + CopyFrom(s); + } + //--------------------------------------------------------------------- + + + //--------------------------------------------------------------------- + void Signal::CopyFrom(const Signal & s) { + SetSamplingPeriod(s.GetSamplingPeriod()); + // DD(GetSamplingPeriod()); + resize(s.size()); + for(uint i=0; i> sub; + // DD(sub); + // DD(sub.size()); + // DD(iss.bad()); + if (!iss) { + std::cerr << "ERROR: no col n" << col << " in the line '" << line << "' ?" << std::endl; + return false; //exit(0); + } } iss >> point; - if (!iss) { - std::cerr << "ERROR: no col n" << col << " in the line '" << line << "' ?" << std::endl; - exit(0); - } skipComment(signalStream); m_Data.push_back(point); // cout << point << endl; } signalStream.close(); SetSamplingPeriod(1.); + return true; } //--------------------------------------------------------------------- @@ -70,23 +95,23 @@ namespace clitk { Signal Signal::ConvertImageToSignal( Signal::ImageType::Pointer image) { //empty signal - Signal signal; + Signal signal; - //make an image iterator - itk::ImageRegionConstIterator it(image,image->GetLargestPossibleRegion()); - it.Begin(); + //make an image iterator + itk::ImageRegionConstIterator it(image,image->GetLargestPossibleRegion()); + it.Begin(); - //copy - while(!it.IsAtEnd()) - { - signal.push_back(it.Get()); - ++it; - } + //copy + while(!it.IsAtEnd()) + { + signal.push_back(it.Get()); + ++it; + } - //Spacing - signal.SetSamplingPeriod(image->GetSpacing()[0]); + //Spacing + signal.SetSamplingPeriod(image->GetSpacing()[0]); - return signal; + return signal; } //--------------------------------------------------------------------- @@ -95,41 +120,41 @@ namespace clitk { //Convert 1D signal to image Signal::ImageType::Pointer Signal::ConvertSignalToImage(Signal signal) { - //empty image - ImageType::Pointer image =ImageType::New(); - ImageType::RegionType region; - ImageType::RegionType::IndexType index; - index[0]=0; - ImageType::RegionType::SizeType size; - size[0]=signal.size(); - - region.SetIndex(index); - region.SetSize(size); - - image->SetRegions(region); - image->Allocate(); - - //make an image iterator - itk::ImageRegionIterator mIt(image,image->GetLargestPossibleRegion()); - mIt.Begin(); - - //make a signal iterator - Signal::const_iterator sIt=signal.begin(); - - //copy - while(sIt!=signal.end()) - { - mIt.Set(*sIt); - sIt++;++mIt; - } + //empty image + ImageType::Pointer image =ImageType::New(); + ImageType::RegionType region; + ImageType::RegionType::IndexType index; + index[0]=0; + ImageType::RegionType::SizeType size; + size[0]=signal.size(); + + region.SetIndex(index); + region.SetSize(size); + + image->SetRegions(region); + image->Allocate(); + + //make an image iterator + itk::ImageRegionIterator mIt(image,image->GetLargestPossibleRegion()); + mIt.Begin(); + + //make a signal iterator + Signal::const_iterator sIt=signal.begin(); + + //copy + while(sIt!=signal.end()) + { + mIt.Set(*sIt); + sIt++;++mIt; + } - //spacing - ImageType::SpacingType spacing; - spacing[0]=signal.GetSamplingPeriod(); - image->SetSpacing(spacing); + //spacing + ImageType::SpacingType spacing; + spacing[0]=signal.GetSamplingPeriod(); + image->SetSpacing(spacing); - return image; + return image; } //--------------------------------------------------------------------- @@ -188,8 +213,8 @@ namespace clitk { iterator itD; itD=mul.begin(); while(it!=end()){ - *it *= *itD; - it++;itD++; + *it *= *itD; + it++;itD++; } return *this; } @@ -341,7 +366,7 @@ namespace clitk { //--------------------------------------------------------------------- - //--------------------------------------------------------------------- + //--------------------------------------------------------------------- // Signal Signal::LowPassFilter (double sampPeriod, double cutOffFrequency ) // { // //output @@ -484,7 +509,7 @@ namespace clitk { //Search the first value in extrema not 0.5 while(m_Data[beginCycle]!=1) { - beginCycle++; + beginCycle++; } //We search the corresponding end @@ -492,7 +517,7 @@ namespace clitk { while(endCycle != size() && m_Data[endCycle]!=1){ endCycle++; - } + } //============================================================ //Calculate phase at the beginning (before the first extremum) @@ -547,7 +572,7 @@ namespace clitk { //the last part is shorter, copy the last cycle values else{ phase[i] = phase[i -(endCycle-beginCycle)]; - } + } } //=================================================================== @@ -581,7 +606,7 @@ namespace clitk { //Search the first value in extrema not 0.5 while(m_Data[beginCycle]!=1) { - beginCycle++; + beginCycle++; } //We search the corresponding end @@ -589,7 +614,7 @@ namespace clitk { while(endCycle != size() && m_Data[endCycle]!=1){ endCycle++; - } + } //=================================================================== //Calculate phase at the beginning (before the first extremum) @@ -645,7 +670,7 @@ namespace clitk { //the last part is shorter, copy the last cycle values else{ phase[i] = phase[i -(endCycle-beginCycle)]+1; - } + } } //=================================================================== @@ -676,11 +701,11 @@ namespace clitk { iterator monPhaseIt=monPhase.begin(); iterator extremaIt =begin(); while (extremaIt!= end()) - { - if (*extremaIt==0.) *phaseIt=eIPhaseValue+floor(*monPhaseIt); - else if (*extremaIt==1.) *phaseIt=eEPhaseValue+floor(*monPhaseIt); - extremaIt++; phaseIt++;monPhaseIt++; - } + { + if (*extremaIt==0.) *phaseIt=eIPhaseValue+floor(*monPhaseIt); + else if (*extremaIt==1.) *phaseIt=eEPhaseValue+floor(*monPhaseIt); + extremaIt++; phaseIt++;monPhaseIt++; + } return phase; @@ -761,6 +786,30 @@ namespace clitk { //--------------------------------------------------------------------- + +// //--------------------------------------------------------------------- +// Signal Signal::ApproximateScatteredValuesWithBSplines(unsigned int splineOrder, unsigned int numberOfControlPoints) +// { +// //filter requires a vector pixelType +// typedef itk::PointSet PointSetType; +// PointSetType::Pointer pointSet=PointSetType::New(); +// typedef PointSetType::PointType PointType; + +// unsigned int i=0; +// unsigned int pointIndex=0; +// while (i< size()) +// { +// if(m_Data[i]!=-1) +// { +// PointType p; +// p[0]= i;//JV spacing is allways 1 +// pointSet->SetPoint( pointIndex, p ); +// pointSet->SetPointData( pointIndex, m_Data[i] ); +// pointIndex++; +// } +// i++; +// } +// ======= // //--------------------------------------------------------------------- // Signal Signal::ApproximateScatteredValuesWithBSplines(unsigned int splineOrder, unsigned int numberOfControlPoints) // { @@ -899,18 +948,202 @@ namespace clitk { //--------------------------------------------------------------------- + //--------------------------------------------------------------------- + void Signal::ResampleWithTimeWarpTo(clitk::Signal & output, bool lin) const { + double sp = output.GetSamplingPeriod()/output.GetTotalTimeDuration()*GetTotalTimeDuration(); + // DD(GetTotalTimeDuration()); + // DD(output.GetTotalTimeDuration()); + // DD(sp); + if (lin) { + for(uint i=0; i=(int)size())) { + FATAL("out of bound for time " << t << "in this signal having " + << size() << " values with sampling " << GetSamplingPeriod() << std::endl); + } + return (*this)[index]; + } + //--------------------------------------------------------------------- + //--------------------------------------------------------------------- + double Signal::GetValueAtLin(double t) const { + double ti = t/GetSamplingPeriod(); + // DD(t); +// DD(ti); + int index = (int)floor(ti); // floor point + // DD(index); + if (index<0) { + FATAL("out of bound for time " << t << " in this signal having " + << size() << " values with sampling " << GetSamplingPeriod() << std::endl); + } + if (index>=(int)size()-1) { // last value + return (*this)[index]; + } + // DD(index); +// DD(ti-index); +// DD((*this)[index]); +// DD((*this)[index+1]); + return ((ti-index)*((*this)[index+1]) + (1.0-(ti-index))*((*this)[index])); + } + //--------------------------------------------------------------------- + //--------------------------------------------------------------------- + void clitk::Signal::Shift(int d, int length) { + std::cout << "Shift d=" << d << " length = " << length << std::endl; + // int d = (int)lrint(s/GetSamplingPeriod()); // closest integer delta + // DD(d); + clitk::Signal temp; + temp.resize(length);//(uint)size()); + temp.SetSamplingPeriod(GetSamplingPeriod()); + for(uint i=0; iGetGlobalMean(); + mean2=input2.GetGlobalMean(); + + for (unsigned int k= 0;kGetGlobalMean(); + mean2=input2.GetGlobalMean(); + double value=0.; + for (unsigned int n=0;nUnormalizedCorrelation(input2); + cc11=this->UnormalizedCorrelation(input1); + cc22=input2.UnormalizedCorrelation (input2); + cc=cc/sqrt(cc11*cc22); + std::cout<<"coefficient de corrélation : "<UnormalizedXcov(input2,output); + cc11=this->UnormalizedCorrelation(input1); + cc22=input2.UnormalizedCorrelation (input2); + + unsigned int tailleOutput; + tailleOutput=output.size(); + for (unsigned int k= 0;kcoef){ + coef=output[k]; + shift=k; + } + } + cc=this->Correlation(input2); + shift=shift-N+1; + std::cout<<"shift: "< GetGlobalMinMax() const; double GetGlobalMean() const; @@ -101,6 +103,17 @@ class Signal{ void AddValue(double v); void ComputeAugmentedSpace(Signal & outputX, Signal & outputY, unsigned int delay) const; + void ResampleWithTimeWarpTo(clitk::Signal & output, bool lin=false) const ; + double GetValueAtNN(double t) const; + double GetValueAtLin(double t) const; + void Shift(int s, int length); + + double GetTotalTimeDuration() const { return size()*GetSamplingPeriod(); } + double UnormalizedXcov(const Signal &, Signal &) const; + Signal Xcov(const Signal &, int &, double &, double &); + double UnormalizedCorrelation(const Signal &input2) const; + double Correlation(const Signal &input2) const; + // double Compare(Signal & sigRef); // int DerivateSigne( const_iterator & it) const; // void CenteredFiniteDifferences(Signal & result,int order,int* weights); -- 2.45.1