1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301
|
/*=========================================================================
Program: Advanced Normalization Tools
Copyright (c) ConsortiumOfANTS. All rights reserved.
See accompanying COPYING.txt or
https://github.com/stnava/ANTs/blob/master/ANTSCopyright.txt
for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef __antsJointHistogramParzenWindowsListSampleFunction_hxx
#define __antsJointHistogramParzenWindowsListSampleFunction_hxx
#include "itkArray.h"
#include "itkBSplineInterpolateImageFunction.h"
#include "itkContinuousIndex.h"
#include "itkDiscreteGaussianImageFilter.h"
#include "itkDivideByConstantImageFilter.h"
#include "itkStatisticsImageFilter.h"
namespace itk
{
namespace ants
{
namespace Statistics
{
template <typename TListSample, typename TOutput, typename TCoordRep>
JointHistogramParzenWindowsListSampleFunction<TListSample, TOutput, TCoordRep>::
JointHistogramParzenWindowsListSampleFunction()
{
this->m_NumberOfJointHistogramBins = 32;
this->m_Sigma = 1.0;
this->m_UseNNforJointHistIncrements = true;
}
template <typename TListSample, typename TOutput, typename TCoordRep>
JointHistogramParzenWindowsListSampleFunction<TListSample, TOutput, TCoordRep>::
~JointHistogramParzenWindowsListSampleFunction()
{}
template <typename TListSample, typename TOutput, typename TCoordRep>
void
JointHistogramParzenWindowsListSampleFunction<TListSample, TOutput, TCoordRep>::IncrementJointHistogram(
RealType eigenvalue1,
RealType eigenvalue2,
unsigned int which_hist)
{
RealType newWeight = 1.0;
// now define two joint histograms, one for shape, one for orientation.
// first, the shape histogram --- 0,0 origin and spacing of 1
if (this->m_JointHistogramImages.size() == which_hist)
{
typename JointHistogramImageType::SpacingType spacing;
spacing.Fill(1);
typename JointHistogramImageType::PointType origin;
origin.Fill(0);
typename JointHistogramImageType::SizeType size;
size.Fill(this->m_NumberOfJointHistogramBins);
typename JointHistogramImageType::DirectionType direction;
direction.SetIdentity();
typename JointHistogramImageType::Pointer curJHI =
AllocImage<JointHistogramImageType>(size, spacing, origin, regions, 0);
this->m_JointHistogramImages.push_back(curJHI);
}
typename JointHistogramImageType::PointType shapePoint;
if (eigenvalue1 > 1)
{
eigenvalue1 = 1;
}
if (eigenvalue2 > 1)
{
eigenvalue2 = 1;
}
if (eigenvalue1 < 0)
{
eigenvalue1 = 0;
}
if (eigenvalue2 < 0)
{
eigenvalue2 = 0;
}
shapePoint[0] = eigenvalue1 * (this->m_NumberOfJointHistogramBins - 1);
shapePoint[1] = eigenvalue2 * (this->m_NumberOfJointHistogramBins - 1);
ContinuousIndex<double, 2> shapeCidx;
this->m_JointHistogramImages[which_hist]->TransformPhysicalPointToContinuousIndex(shapePoint, shapeCidx);
typename JointHistogramImageType::IndexType shapeIdx;
/** Nearest neighbor increment to JH */
if (this->m_UseNNforJointHistIncrements)
{
shapeIdx[0] = std::floor(shapeCidx[0] + 0.5);
shapeIdx[1] = std::floor(shapeCidx[1] + 0.5);
if (this->m_JointHistogramImages[which_hist]->GetLargestPossibleRegion().IsInside(shapeIdx))
{
RealType oldWeight = this->m_JointHistogramImages[which_hist]->GetPixel(shapeIdx);
this->m_JointHistogramImages[which_hist]->SetPixel(shapeIdx, 1 + oldWeight);
}
}
else
{
/** linear addition */
shapeIdx[0] = static_cast<typename JointHistogramImageType::IndexType::IndexValueType>(std::floor(shapeCidx[0]));
shapeIdx[1] = static_cast<typename JointHistogramImageType::IndexType::IndexValueType>(std::floor(shapeCidx[1]));
RealType dist1 = sqrt((shapeCidx[0] - shapeIdx[0]) * (shapeCidx[0] - shapeIdx[0]) +
(shapeCidx[1] - shapeIdx[1]) * (shapeCidx[1] - shapeIdx[1]));
shapeIdx[0]++;
RealType dist2 = sqrt((shapeCidx[0] - shapeIdx[0]) * (shapeCidx[0] - shapeIdx[0]) +
(shapeCidx[1] - shapeIdx[1]) * (shapeCidx[1] - shapeIdx[1]));
shapeIdx[1]++;
RealType dist3 = sqrt((shapeCidx[0] - shapeIdx[0]) * (shapeCidx[0] - shapeIdx[0]) +
(shapeCidx[1] - shapeIdx[1]) * (shapeCidx[1] - shapeIdx[1]));
shapeIdx[0]--;
RealType dist4 = sqrt((shapeCidx[0] - shapeIdx[0]) * (shapeCidx[0] - shapeIdx[0]) +
(shapeCidx[1] - shapeIdx[1]) * (shapeCidx[1] - shapeIdx[1]));
RealType distsum = dist1 + dist2 + dist3 + dist4;
dist1 /= distsum;
dist2 /= distsum;
dist3 /= distsum;
dist4 /= distsum;
shapeIdx[0] = static_cast<typename JointHistogramImageType::IndexType::IndexValueType>(std::floor(shapeCidx[0]));
shapeIdx[1] = static_cast<typename JointHistogramImageType::IndexType::IndexValueType>(std::floor(shapeCidx[1]));
if (this->m_JointHistogramImages[which_hist]->GetLargestPossibleRegion().IsInside(shapeIdx))
{
RealType oldWeight = this->m_JointHistogramImages[which_hist]->GetPixel(shapeIdx);
this->m_JointHistogramImages[which_hist]->SetPixel(shapeIdx, (1.0 - dist1) * newWeight + oldWeight);
}
shapeIdx[0]++;
if (this->m_JointHistogramImages[which_hist]->GetLargestPossibleRegion().IsInside(shapeIdx))
{
RealType oldWeight = this->m_JointHistogramImages[which_hist]->GetPixel(shapeIdx);
this->m_JointHistogramImages[which_hist]->SetPixel(shapeIdx, (1.0 - dist2) * newWeight + oldWeight);
}
shapeIdx[1]++;
if (this->m_JointHistogramImages[which_hist]->GetLargestPossibleRegion().IsInside(shapeIdx))
{
RealType oldWeight = this->m_JointHistogramImages[which_hist]->GetPixel(shapeIdx);
this->m_JointHistogramImages[which_hist]->SetPixel(shapeIdx, (1.0 - dist3) * newWeight + oldWeight);
}
shapeIdx[0]--;
if (this->m_JointHistogramImages[which_hist]->GetLargestPossibleRegion().IsInside(shapeIdx))
{
RealType oldWeight = this->m_JointHistogramImages[which_hist]->GetPixel(shapeIdx);
this->m_JointHistogramImages[which_hist]->SetPixel(shapeIdx, (1.0 - dist4) * newWeight + oldWeight);
}
}
return;
}
template <typename TListSample, typename TOutput, typename TCoordRep>
void
JointHistogramParzenWindowsListSampleFunction<TListSample, TOutput, TCoordRep>::SetInputListSample(
const InputListSampleType * ptr)
{
this->m_ListSample = ptr;
this->m_JointHistogramImages.clear();
if (!this->m_ListSample)
{
return;
}
if (this->m_ListSample->Size() <= 1)
{
itkWarningMacro("The input list sample has <= 1 element."
<< "Function evaluations will be equal to 0.");
return;
}
typename InputListSampleType::ConstIterator It = this->m_ListSample->Begin();
InputMeasurementVectorType inputMeasurement = It.GetMeasurementVector();
unsigned int Dimension = inputMeasurement.Size();
if ((Dimension % 2) != 0)
{
itkWarningMacro("The input list should contain 2*N images where N > 0.");
return;
}
/**
* Find the min/max values to define the histogram domain
*/
Array<RealType> minValues(Dimension);
minValues.Fill(NumericTraits<RealType>::max());
Array<RealType> maxValues(Dimension);
maxValues.Fill(NumericTraits<RealType>::NonpositiveMin());
It = this->m_ListSample->Begin();
while (It != this->m_ListSample->End())
{
InputMeasurementVectorType inputMeasurement = It.GetMeasurementVector();
for (unsigned int d = 0; d < Dimension; d++)
{
if (inputMeasurement[d] < minValues[d])
{
minValues[d] = inputMeasurement[d];
}
if (inputMeasurement[d] > maxValues[d])
{
maxValues[d] = inputMeasurement[d];
}
}
++It;
}
It = this->m_ListSample->Begin();
while (It != this->m_ListSample->End())
{
InputMeasurementVectorType inputMeasurement = It.GetMeasurementVector();
/** joint-hist model for the eigenvalues */
unsigned int jhcount = 0;
for (unsigned int d = 0; d < Dimension; d = d + 2)
{
RealType value1 = (inputMeasurement[d] - minValues[d]) / (maxValues[d] - minValues[d]);
RealType value2 = (inputMeasurement[d + 1] - minValues[d + 1]) / (maxValues[d + 1] - minValues[d + 1]);
this->IncrementJointHistogram(value1, value2, jhcount);
jhcount++;
}
++It;
}
for (unsigned int d = 0; d < this->m_JointHistogramImages.size(); d++)
{
typedef DiscreteGaussianImageFilter<JointHistogramImageType, JointHistogramImageType> GaussianFilterType;
typename GaussianFilterType::Pointer gaussian = GaussianFilterType::New();
gaussian->SetInput(this->m_JointHistogramImages[d]);
gaussian->SetVariance(this->m_Sigma * this->m_Sigma);
gaussian->SetMaximumError(0.01);
gaussian->SetUseImageSpacing(false);
gaussian->Update();
typedef StatisticsImageFilter<JointHistogramImageType> StatsFilterType;
typename StatsFilterType::Pointer stats = StatsFilterType::New();
stats->SetInput(gaussian->GetOutput());
stats->Update();
typedef DivideByConstantImageFilter<JointHistogramImageType, RealType, JointHistogramImageType> DividerType;
typename DividerType::Pointer divider = DividerType::New();
divider->SetInput(gaussian->GetOutput());
divider->SetConstant(stats->GetSum());
divider->Update();
this->m_JointHistogramImages[d] = divider->GetOutput();
}
}
template <typename TListSample, typename TOutput, typename TCoordRep>
TOutput
JointHistogramParzenWindowsListSampleFunction<TListSample, TOutput, TCoordRep>::Evaluate(
const InputMeasurementVectorType & measurement) const
{
try
{
typedef BSplineInterpolateImageFunction<JointHistogramImageType> InterpolatorType;
RealType probability = 1.0;
for (unsigned int d = 0; d < this->m_JointHistogramImages.size(); d++)
{
typename JointHistogramImageType::PointType point;
point[0] = measurement[d];
typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
interpolator->SetSplineOrder(3);
interpolator->SetInputImage(this->m_JointHistogramImages[d]);
if (interpolator->IsInsideBuffer(point))
{
probability *= interpolator->Evaluate(point);
}
else
{
return 0;
}
}
return probability;
}
catch (...)
{
return 0;
}
}
/**
* Standard "PrintSelf" method
*/
template <typename TListSample, typename TOutput, typename TCoordRep>
void
JointHistogramParzenWindowsListSampleFunction<TListSample, TOutput, TCoordRep>::PrintSelf(std::ostream & os,
Indent indent) const
{
os << indent << "Sigma: " << this->m_Sigma << std::endl;
os << indent << "Number of histogram bins: " << this->m_NumberOfJointHistogramBins << std::endl;
}
} // end of namespace Statistics
} // end of namespace ants
} // end of namespace itk
#endif
|