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

ENH: some code simplifications for readability

- make vars const
- copy map into vector on construction
- typedefs for code readability
parent ce3a404c
Branches
Tags
No related merge requests found
...@@ -99,8 +99,9 @@ NeighborhoodMajorityVotingImageFilter<TInputImage, TOutputImage, ...@@ -99,8 +99,9 @@ NeighborhoodMajorityVotingImageFilter<TInputImage, TOutputImage,
const KernelIteratorType kernelBegin, const KernelIteratorType kernelBegin,
const KernelIteratorType kernelEnd) const const KernelIteratorType kernelEnd) const
{ {
std::map<PixelType, unsigned int> histoNeigh; typedef std::map<PixelType, unsigned int> HistogramType;
PixelType centerPixel = nit.GetCenterPixel(); typedef std::vector<std::pair<PixelType, unsigned int> > HistoAsVectorType;
HistogramType histoNeigh;
unsigned int i = 0; unsigned int i = 0;
for (KernelIteratorType kernel_it = kernelBegin; for (KernelIteratorType kernel_it = kernelBegin;
kernel_it < kernelEnd; ++kernel_it, ++i) kernel_it < kernelEnd; ++kernel_it, ++i)
...@@ -109,21 +110,20 @@ NeighborhoodMajorityVotingImageFilter<TInputImage, TOutputImage, ...@@ -109,21 +110,20 @@ NeighborhoodMajorityVotingImageFilter<TInputImage, TOutputImage,
// in the image // in the image
// note we use GetPixel() on the SmartNeighborhoodIterator to // note we use GetPixel() on the SmartNeighborhoodIterator to
// respect boundary conditions // respect boundary conditions
PixelType label = nit.GetPixel(i); const PixelType label = nit.GetPixel(i);
if ((*kernel_it > itk::NumericTraits<KernelPixelType>::Zero) && (label != m_LabelForNoDataPixels)) if ((*kernel_it > itk::NumericTraits<KernelPixelType>::Zero) &&
(label != m_LabelForNoDataPixels))
{ {
//If the current label has already been added to the histogram histoNeigh
histoNeigh[label] += 1; histoNeigh[label] += 1;
} }
} }
std::vector< std::pair<PixelType, unsigned int> > histoNeighVec; HistoAsVectorType histoNeighVec(histoNeigh.begin(), histoNeigh.end());
std::copy(histoNeigh.begin(), histoNeigh.end(), std::back_inserter(histoNeighVec)); //Sort the 2 max elements to the beginning
const typename std::vector<std::pair<PixelType, unsigned int> >::iterator histoIt = histoNeighVec.begin(); std::nth_element(histoNeighVec.begin(), histoNeighVec.begin()+1,
std::nth_element(histoNeighVec.begin(), histoIt+1,
histoNeighVec.end(), CompareHistoFequencies()); histoNeighVec.end(), CompareHistoFequencies());
typename NeighborhoodMajorityVotingImageFilter<TInputImage, TOutputImage, typename NeighborhoodMajorityVotingImageFilter<TInputImage, TOutputImage,
TKernel>::HistoSummary result; TKernel>::HistoSummary result;
result.freqCenterLabel = histoNeigh[centerPixel]; result.freqCenterLabel = histoNeigh[nit.GetCenterPixel()];
result.majorityFreq = histoNeighVec[0].second; result.majorityFreq = histoNeighVec[0].second;
result.majorityLabel = histoNeighVec[0].first; result.majorityLabel = histoNeighVec[0].first;
result.secondFreq = histoNeighVec[1].second; result.secondFreq = histoNeighVec[1].second;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment