15 #define RESIZE_FACTOR 20 16 #define MATLAB_INDEX_FACTOR 1 134 if (
Data.empty())
return false;
153 void PrintPairs(
const std::vector<TPairedExtrema>& pairs)
const 155 for (std::vector<TPairedExtrema>::const_iterator it = pairs.begin();
156 it != pairs.end(); it++)
158 std::cout <<
"Persistence: " << (*it).Persistence
159 <<
" minimum index: " << (*it).MinIndex
160 <<
" maximum index: " << (*it).MaxIndex
172 void PrintResults(
const float threshold = 0.0,
const bool matlabIndexing =
false)
const 176 std::cout <<
"Error. Threshold value must be greater than or equal to 0" << std::endl;
178 if (threshold==0 && !matlabIndexing)
184 std::vector<TPairedExtrema> pairs;
206 bool GetPairedExtrema(std::vector<TPairedExtrema> & pairs,
const float threshold = 0,
const bool matlabIndexing =
false)
const 217 pairs = std::vector<TPairedExtrema>(lower_bound,
PairedExtrema.end());
221 for (std::vector<TPairedExtrema>::iterator p = pairs.begin(); p != pairs.end(); p++)
241 bool GetExtremaIndices(std::vector<int> & min, std::vector<int> & max,
const float threshold = 0,
const bool matlabIndexing =
false)
const 252 int matlabIndexFactor = 0;
257 for (std::vector<TPairedExtrema>::const_iterator p = lower_bound; p !=
PairedExtrema.end(); p++)
259 min.push_back((*p).MinIndex + matlabIndexFactor);
260 max.push_back((*p).MaxIndex + matlabIndexFactor);
307 std::vector<int> min, max;
308 std::vector<int> combinedIndices;
314 std::sort(min.begin(), min.end());
315 std::sort(max.begin(), max.end());
316 combinedIndices.reserve(min.size() + max.size());
317 std::set_union(min.begin(), min.end(), max.begin(), max.end(), std::inserter(combinedIndices, combinedIndices.begin()));
320 if (combinedIndices.size() != (min.size() + max.size()) ||
321 std::binary_search(combinedIndices.begin(), combinedIndices.end(), globalMinIdx) ==
true)
326 if ((globalMinIdx > (
int)
Data.size()-1) || (globalMinIdx < -1)) flag =
false;
327 if (globalMinIdx == -1 && min.size() != 0) flag =
false;
329 std::vector<int>::iterator minUniqueEnd = std::unique(min.begin(), min.end());
330 std::vector<int>::iterator maxUniqueEnd = std::unique(max.begin(), max.end());
332 if (minUniqueEnd != min.end() ||
333 maxUniqueEnd != max.end() ||
334 (minUniqueEnd - min.begin()) != (maxUniqueEnd - max.begin()))
391 int survivorIdx, destroyedIdx;
395 survivorIdx = firstIdx;
396 destroyedIdx = secondIdx;
400 survivorIdx = secondIdx;
401 destroyedIdx = firstIdx;
403 else if (firstIdx < secondIdx)
406 survivorIdx = firstIdx;
407 destroyedIdx = secondIdx;
411 survivorIdx = secondIdx;
412 destroyedIdx = firstIdx;
444 if (
Data[firstIdx] >
Data[secondIdx])
449 else if (
Data[secondIdx] >
Data[firstIdx])
455 else if (firstIdx < secondIdx)
524 assert(
Components[componentIdx].Alive ==
true)
528 if (dataIdx + 1 ==
Components[componentIdx].LeftEdgeIndex)
530 Components[componentIdx].LeftEdgeIndex = dataIdx;
533 else if (dataIdx - 1 ==
Components[componentIdx].RightEdgeIndex)
535 Components[componentIdx].RightEdgeIndex = dataIdx;
540 std::string errorMessage =
"ExtendComponent: index mismatch. Data index: ";
541 errorMessage += std::to_string((
long long)dataIdx);
542 throw (errorMessage);
546 Colors[dataIdx] = componentIdx;
585 if (
Data.size()==0)
return;
587 for (std::vector<float>::size_type i = 0; i !=
Data.size(); i++)
593 dataidxpair.
Idx = (int)i;
639 else if (i ==
Colors.size()-1)
667 int leftComp, rightComp;
707 if (threshold == 0 || threshold < 0)
return PairedExtrema.begin();
732 throw "Error. Component 0 is not Alive, assumed to contain global minimum";
736 for (std::vector<TComponent>::const_iterator it =
Components.begin()+1; it !=
Components.end(); it++)
738 if ((*it).Alive ==
true)
745 throw "Error. Found more than one alive component";
#define MATLAB_INDEX_FACTOR
int MinIndex
Index of local minimum, as per Data vector.
bool AliveComponentsVerified
bool operator<(const TPairedExtrema &other) const
A pair of matched local minimum and local maximum that define a component above a certain persistence...
unsigned int TotalComponents
int GetGlobalMinimumIndex(const bool matlabIndexing=false) const
Returns the index of the global minimum.
bool GetExtremaIndices(std::vector< int > &min, std::vector< int > &max, const float threshold=0, const bool matlabIndexing=false) const
Use this method to get two vectors with all indices of PairedExterma.
void CreateIndexValueVector()
Creates SortedData vector.
int LeftEdgeIndex
A component is defined by the indices of its edges.
bool operator<(const TIdxAndData &other) const
bool Alive
Set to true when a component is created.
void CreateComponent(const int minIdx)
Creates a new component at a local minimum.
void Watershed()
Main algorithm - all of the work happen here.
std::vector< TIdxAndData > SortedData
Contains a copy the value and index pairs of Data, sorted according to the data values.
void SortPairedExtrema()
Sorts the PairedExtrema list according to the persistence of the features.
bool RunPersistence(const std::vector< float > &InputData)
Call this function with a vector of one dimensional data to find extrema features in the data.
std::vector< float > Data
Contain a copy of the original input data.
void PrintPairs(const std::vector< TPairedExtrema > &pairs) const
Prints the contents of the TPairedExtrema vector.
void MergeComponents(const int firstIdx, const int secondIdx)
Merges two components by doing the following:
float MinValue
The value of the Data[MinIndex].
int MaxIndex
Index of local maximum, as per Data vector.
Used to sort data according to its absolute value and refer to its original index in the Data vector.
Finds extrema and their persistence in one-dimensional data.
bool GetPairedExtrema(std::vector< TPairedExtrema > &pairs, const float threshold=0, const bool matlabIndexing=false) const
Use this method to get the results of RunPersistence.
std::vector< TPairedExtrema >::const_iterator FilterByPersistence(const float threshold=0) const
Returns an iterator to the first element in PairedExtrema whose persistence is bigger or equal to thr...
float Data
Vertex data value from the original Data vector sent as an argument to RunPersistence.
void Init()
Initializes main data structures used in class:
bool VerifyAliveComponents()
Runs at the end of RunPersistence, after Watershed.
int MinIndex
The index of the local minimum within the component as longs as its alive.
void ExtendComponent(const int componentIdx, const int dataIdx)
Extends the component's region by one vertex:
void PrintResults(const float threshold=0.0, const bool matlabIndexing=false) const
Prints the global minimum and all paired extrema whose persistence is greater or equal to threshold.
float Persistence
The persistence of the two extrema.
std::vector< TPairedExtrema > PairedExtrema
A vector of paired extrema features - always a minimum and a maximum.
float GetGlobalMinimumValue() const
Returns the value of the global minimum.
std::vector< int > Colors
Contains the Component assignment for each vertex in Data.
int Idx
The index of the vertex within the Data vector.
Defines a component within the data domain.
void CreatePairedExtrema(const int firstIdx, const int secondIdx)
Creates a new PairedExtrema from the two indices, and adds it to PairedFeatures.
std::vector< TComponent > Components
A vector of Components.
bool VerifyResults()
Runs basic sanity checks on results of RunPersistence: