Skip to content
Snippets Groups Projects
Commit 35e7cdc9 authored by Jordi Inglada's avatar Jordi Inglada
Browse files

ENH: option to process isolated pixels only

parent 4bac24ed
No related branches found
No related tags found
No related merge requests found
...@@ -149,6 +149,9 @@ public: ...@@ -149,6 +149,9 @@ public:
//Creates a SetKeepOriginalLabelBool method //Creates a SetKeepOriginalLabelBool method
itkSetMacro(KeepOriginalLabelBool, bool); itkSetMacro(KeepOriginalLabelBool, bool);
//Process only isolated pixels
itkSetMacro(OnlyIsolatedPixels, bool);
protected: protected:
NeighborhoodMajorityVotingImageFilter(); NeighborhoodMajorityVotingImageFilter();
...@@ -167,10 +170,11 @@ protected: ...@@ -167,10 +170,11 @@ protected:
void GenerateOutputInformation() ITK_OVERRIDE; void GenerateOutputInformation() ITK_OVERRIDE;
//Get a histogram of frequencies of labels with the 2 highest frequencies sorted in decreasing order //Get a histogram of frequencies of labels with the 2 highest frequencies sorted in decreasing order
void FillNeighborhoodHistogram(std::vector<std::pair<PixelType, unsigned int> >& histoNeigh, // and return the frequency of the label of the center pixel
const NeighborhoodIteratorType &nit, unsigned int FillNeighborhoodHistogram(std::vector<std::pair<PixelType, unsigned int> >& histoNeigh,
const KernelIteratorType kernelBegin, const NeighborhoodIteratorType &nit,
const KernelIteratorType kernelEnd); const KernelIteratorType kernelBegin,
const KernelIteratorType kernelEnd);
struct CompareHistoFequencies struct CompareHistoFequencies
{ {
...@@ -191,6 +195,7 @@ private: ...@@ -191,6 +195,7 @@ private:
PixelType m_LabelForNoDataPixels; PixelType m_LabelForNoDataPixels;
PixelType m_LabelForUndecidedPixels; PixelType m_LabelForUndecidedPixels;
bool m_KeepOriginalLabelBool; bool m_KeepOriginalLabelBool;
bool m_OnlyIsolatedPixels;
}; // end of class }; // end of class
......
...@@ -38,15 +38,16 @@ NeighborhoodMajorityVotingImageFilter<TInputImage, TOutputImage, TKernel>::Neigh ...@@ -38,15 +38,16 @@ NeighborhoodMajorityVotingImageFilter<TInputImage, TOutputImage, TKernel>::Neigh
this->SetLabelForNoDataPixels(itk::NumericTraits<PixelType>::NonpositiveMin()); //m_LabelForNoDataPixels = 0 this->SetLabelForNoDataPixels(itk::NumericTraits<PixelType>::NonpositiveMin()); //m_LabelForNoDataPixels = 0
this->SetLabelForUndecidedPixels(itk::NumericTraits<PixelType>::NonpositiveMin()); //m_LabelForUndecidedPixels = 0 this->SetLabelForUndecidedPixels(itk::NumericTraits<PixelType>::NonpositiveMin()); //m_LabelForUndecidedPixels = 0
this->SetKeepOriginalLabelBool(true); //m_KeepOriginalLabelBool = true this->SetKeepOriginalLabelBool(true); //m_KeepOriginalLabelBool = true
} this->SetOnlyIsolatedPixels(false); //process all pixels
}
template<class TInputImage, class TOutputImage, class TKernel> template<class TInputImage, class TOutputImage, class TKernel>
typename NeighborhoodMajorityVotingImageFilter<TInputImage, TOutputImage, typename NeighborhoodMajorityVotingImageFilter<TInputImage, TOutputImage,
TKernel>::PixelType NeighborhoodMajorityVotingImageFilter<TInputImage, TKernel>::PixelType NeighborhoodMajorityVotingImageFilter<TInputImage,
TOutputImage, TKernel>::Evaluate(const NeighborhoodIteratorType &nit, TOutputImage, TKernel>::Evaluate(const NeighborhoodIteratorType &nit,
const KernelIteratorType kernelBegin, const KernelIteratorType kernelBegin,
const KernelIteratorType kernelEnd) const KernelIteratorType kernelEnd)
{ {
PixelType majorityLabel = 0; //Value of the more representative pixels in the neighborhood PixelType majorityLabel = 0; //Value of the more representative pixels in the neighborhood
unsigned int majorityFreq = 0; //Number of pixels with the more representative value (majorityLabel) in the neighborhood unsigned int majorityFreq = 0; //Number of pixels with the more representative value (majorityLabel) in the neighborhood
...@@ -57,22 +58,29 @@ const KernelIteratorType kernelEnd) ...@@ -57,22 +58,29 @@ const KernelIteratorType kernelEnd)
{ {
std::vector< std::pair<PixelType, unsigned int> > histoNeighVec; std::vector< std::pair<PixelType, unsigned int> > histoNeighVec;
//Get a histogram of label frequencies where the 2 highest are at the beginning and sorted //Get a histogram of label frequencies where the 2 highest are at the beginning and sorted
this->FillNeighborhoodHistogram(histoNeighVec, nit, kernelBegin, kernelEnd); unsigned int freqCenterLabel = this->FillNeighborhoodHistogram(histoNeighVec, nit, kernelBegin, kernelEnd);
//Extraction of the more representative Label in the neighborhood (majorityLabel) and its Frequency (majorityFreq) if(m_OnlyIsolatedPixels && freqCenterLabel > 1)
majorityFreq = histoNeighVec[0].second; //Frequency
majorityLabel = histoNeighVec[0].first; //Label
//If the majorityLabel is NOT unique in the neighborhood
if(histoNeighVec[1].second == majorityFreq && histoNeighVec[1].first != majorityLabel)
{ {
if (m_KeepOriginalLabelBool == true) //The center label is not unique in the neighborhood and we only want to filter isolated pixesl
{ majorityLabel = centerPixel;
majorityLabel = centerPixel; }
} else
else {
//Extraction of the more representative Label in the neighborhood (majorityLabel) and its Frequency (majorityFreq)
majorityFreq = histoNeighVec[0].second; //Frequency
majorityLabel = histoNeighVec[0].first; //Label
//If the majorityLabel is NOT unique in the neighborhood
if(histoNeighVec[1].second == majorityFreq && histoNeighVec[1].first != majorityLabel)
{ {
majorityLabel = m_LabelForUndecidedPixels; if (m_KeepOriginalLabelBool == true)
{
majorityLabel = centerPixel;
}
else
{
majorityLabel = m_LabelForUndecidedPixels;
}
} }
} }
...@@ -88,12 +96,12 @@ return majorityLabel; ...@@ -88,12 +96,12 @@ return majorityLabel;
} }
template<class TInputImage, class TOutputImage, class TKernel> template<class TInputImage, class TOutputImage, class TKernel>
void NeighborhoodMajorityVotingImageFilter<TInputImage, unsigned int NeighborhoodMajorityVotingImageFilter<TInputImage,
TOutputImage, TOutputImage,
TKernel>::FillNeighborhoodHistogram(std::vector<std::pair<PixelType, unsigned int> >& histoNeighVec, TKernel>::FillNeighborhoodHistogram(std::vector<std::pair<PixelType, unsigned int> >& histoNeighVec,
const NeighborhoodIteratorType &nit, const NeighborhoodIteratorType &nit,
const KernelIteratorType kernelBegin, const KernelIteratorType kernelBegin,
const KernelIteratorType kernelEnd) const KernelIteratorType kernelEnd)
{ {
std::map<PixelType, unsigned int> histoNeigh; std::map<PixelType, unsigned int> histoNeigh;
PixelType centerPixel = nit.GetCenterPixel(); PixelType centerPixel = nit.GetCenterPixel();
...@@ -126,6 +134,7 @@ void NeighborhoodMajorityVotingImageFilter<TInputImage, ...@@ -126,6 +134,7 @@ void NeighborhoodMajorityVotingImageFilter<TInputImage,
std::nth_element(histoNeighVec.begin(), histoIt+1, std::nth_element(histoNeighVec.begin(), histoIt+1,
histoNeighVec.end(), CompareHistoFequencies()); histoNeighVec.end(), CompareHistoFequencies());
return histoNeigh[centerPixel];
} }
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment