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 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366
|
/*=========================================================================
*
* Copyright NumFOCUS
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#ifndef itkMultiScaleHessianBasedMeasureImageFilter_hxx
#define itkMultiScaleHessianBasedMeasureImageFilter_hxx
#include "itkImageRegionIterator.h"
#include "itkMath.h"
/*
*
* This code was contributed in the Insight Journal paper:
* "Generalizing vesselness with respect to dimensionality and shape"
* by Antiga L.
* https://www.insight-journal.org/browse/publication/175
*
*/
namespace itk
{
template <typename TInputImage, typename THessianImage, typename TOutputImage>
MultiScaleHessianBasedMeasureImageFilter<TInputImage, THessianImage, TOutputImage>::
MultiScaleHessianBasedMeasureImageFilter()
{
m_NonNegativeHessianBasedMeasure = true;
m_SigmaMinimum = 0.2;
m_SigmaMaximum = 2.0;
m_NumberOfSigmaSteps = 10;
m_SigmaStepMethod = Self::SigmaStepMethodEnum::LogarithmicSigmaSteps;
m_HessianFilter = HessianFilterType::New();
m_HessianToMeasureFilter = nullptr;
// Instantiate Update buffer
m_UpdateBuffer = UpdateBufferType::New();
m_GenerateScalesOutput = false;
m_GenerateHessianOutput = false;
auto scalesImage = ScalesImageType::New();
auto hessianImage = HessianImageType::New();
this->ProcessObject::SetNumberOfRequiredOutputs(3);
this->ProcessObject::SetNthOutput(1, scalesImage.GetPointer());
this->ProcessObject::SetNthOutput(2, hessianImage.GetPointer());
}
template <typename TInputImage, typename THessianImage, typename TOutputImage>
void
MultiScaleHessianBasedMeasureImageFilter<TInputImage, THessianImage, TOutputImage>::EnlargeOutputRequestedRegion(
DataObject * output)
{
// currently this filter can not stream so we must set the outputs
// requested region to the largest
output->SetRequestedRegionToLargestPossibleRegion();
}
template <typename TInputImage, typename THessianImage, typename TOutputImage>
auto
MultiScaleHessianBasedMeasureImageFilter<TInputImage, THessianImage, TOutputImage>::MakeOutput(
DataObjectPointerArraySizeType idx) -> DataObjectPointer
{
if (idx == 1)
{
return ScalesImageType::New().GetPointer();
}
if (idx == 2)
{
return HessianImageType::New().GetPointer();
}
return Superclass::MakeOutput(idx);
}
template <typename TInputImage, typename THessianImage, typename TOutputImage>
void
MultiScaleHessianBasedMeasureImageFilter<TInputImage, THessianImage, TOutputImage>::AllocateUpdateBuffer()
{
/* The update buffer looks just like the output and holds the best response
in the objectness measure */
typename TOutputImage::Pointer output = this->GetOutput();
// this copies meta data describing the output such as origin,
// spacing and the largest region
m_UpdateBuffer->CopyInformation(output);
m_UpdateBuffer->SetRequestedRegion(output->GetRequestedRegion());
m_UpdateBuffer->SetBufferedRegion(output->GetBufferedRegion());
m_UpdateBuffer->Allocate();
// Update buffer is used for > comparisons so make it really really small,
// just to be sure. Thanks to Hauke Heibel.
if (m_NonNegativeHessianBasedMeasure)
{
m_UpdateBuffer->FillBuffer(BufferValueType{});
}
else
{
m_UpdateBuffer->FillBuffer(itk::NumericTraits<BufferValueType>::NonpositiveMin());
}
}
template <typename TInputImage, typename THessianImage, typename TOutputImage>
void
MultiScaleHessianBasedMeasureImageFilter<TInputImage, THessianImage, TOutputImage>::GenerateData()
{
// TODO: Move the allocation to a derived AllocateOutputs method
// Allocate the output
this->GetOutput()->SetBufferedRegion(this->GetOutput()->GetRequestedRegion());
this->GetOutput()->Allocate();
if (m_HessianToMeasureFilter.IsNull())
{
itkExceptionMacro(" HessianToMeasure filter is not set. Use SetHessianToMeasureFilter() ");
}
if (m_GenerateScalesOutput)
{
typename ScalesImageType::Pointer scalesImage = dynamic_cast<ScalesImageType *>(this->ProcessObject::GetOutput(1));
scalesImage->SetBufferedRegion(scalesImage->GetRequestedRegion());
scalesImage->AllocateInitialized();
}
if (m_GenerateHessianOutput)
{
typename HessianImageType::Pointer hessianImage =
dynamic_cast<HessianImageType *>(this->ProcessObject::GetOutput(2));
hessianImage->SetBufferedRegion(hessianImage->GetRequestedRegion());
hessianImage->Allocate();
// SymmetricSecondRankTensor is already filled with zero elements at
// construction.
// No strict need of filling the buffer, but we do it explicitly here to
// make sure.
typename HessianImageType::PixelType zeroTensor(0.0);
hessianImage->FillBuffer(zeroTensor);
}
// Allocate the buffer
AllocateUpdateBuffer();
typename InputImageType::ConstPointer input = this->GetInput();
this->m_HessianFilter->SetInput(input);
this->m_HessianFilter->SetNormalizeAcrossScale(true);
// Create a process accumulator for tracking the progress of this
// minipipeline
auto progress = ProgressAccumulator::New();
progress->SetMiniPipelineFilter(this);
// prevent a divide by zero
if (m_NumberOfSigmaSteps > 0)
{
progress->RegisterInternalFilter(this->m_HessianFilter, .5 / m_NumberOfSigmaSteps);
progress->RegisterInternalFilter(this->m_HessianToMeasureFilter, .5 / m_NumberOfSigmaSteps);
}
for (unsigned int scaleLevel = 0; scaleLevel < m_NumberOfSigmaSteps; ++scaleLevel)
{
const double sigma = this->ComputeSigmaValue(scaleLevel);
itkDebugMacro("Computing measure for scale with sigma = " << sigma);
m_HessianFilter->SetSigma(sigma);
m_HessianToMeasureFilter->SetInput(m_HessianFilter->GetOutput());
m_HessianToMeasureFilter->Update();
this->UpdateMaximumResponse(sigma);
}
// Write out the best response to the output image
// we can assume that the meta-data should match between these two
// image, therefore we iterate over the desired output region
OutputRegionType outputRegion = this->GetOutput()->GetBufferedRegion();
ImageRegionIterator<UpdateBufferType> it(m_UpdateBuffer, outputRegion);
ImageRegionIterator<TOutputImage> oit(this->GetOutput(), outputRegion);
while (!oit.IsAtEnd())
{
oit.Value() = static_cast<OutputPixelType>(it.Get());
++oit;
++it;
}
// Release data from the update buffer.
m_UpdateBuffer->ReleaseData();
}
template <typename TInputImage, typename THessianImage, typename TOutputImage>
void
MultiScaleHessianBasedMeasureImageFilter<TInputImage, THessianImage, TOutputImage>::UpdateMaximumResponse(double sigma)
{
// the meta-data should match between these images, therefore we
// iterate over the desired output region
OutputRegionType outputRegion = this->GetOutput()->GetBufferedRegion();
ImageRegionIterator<UpdateBufferType> oit(m_UpdateBuffer, outputRegion);
typename ScalesImageType::Pointer scalesImage = static_cast<ScalesImageType *>(this->ProcessObject::GetOutput(1));
ImageRegionIterator<ScalesImageType> osit;
typename HessianImageType::Pointer hessianImage = static_cast<HessianImageType *>(this->ProcessObject::GetOutput(2));
ImageRegionIterator<HessianImageType> ohit;
if (m_GenerateScalesOutput)
{
osit = ImageRegionIterator<ScalesImageType>(scalesImage, outputRegion);
osit.GoToBegin();
}
if (m_GenerateHessianOutput)
{
ohit = ImageRegionIterator<HessianImageType>(hessianImage, outputRegion);
ohit.GoToBegin();
}
using HessianToMeasureOutputImageType = typename HessianToMeasureFilterType::OutputImageType;
ImageRegionIterator<HessianToMeasureOutputImageType> it(m_HessianToMeasureFilter->GetOutput(), outputRegion);
ImageRegionIterator<HessianImageType> hit(m_HessianFilter->GetOutput(), outputRegion);
while (!oit.IsAtEnd())
{
if (oit.Value() < it.Value())
{
oit.Value() = it.Value();
if (m_GenerateScalesOutput)
{
osit.Value() = static_cast<ScalesPixelType>(sigma);
}
if (m_GenerateHessianOutput)
{
ohit.Value() = hit.Value();
}
}
++oit;
++it;
if (m_GenerateScalesOutput)
{
++osit;
}
if (m_GenerateHessianOutput)
{
++ohit;
++hit;
}
}
}
template <typename TInputImage, typename THessianImage, typename TOutputImage>
double
MultiScaleHessianBasedMeasureImageFilter<TInputImage, THessianImage, TOutputImage>::ComputeSigmaValue(int scaleLevel)
{
double sigmaValue;
if (m_NumberOfSigmaSteps < 2)
{
return m_SigmaMinimum;
}
switch (m_SigmaStepMethod)
{
case Self::SigmaStepMethodEnum::EquispacedSigmaSteps:
{
const double stepSize = std::max(1e-10, (m_SigmaMaximum - m_SigmaMinimum) / (m_NumberOfSigmaSteps - 1));
sigmaValue = m_SigmaMinimum + stepSize * scaleLevel;
break;
}
case Self::SigmaStepMethodEnum::LogarithmicSigmaSteps:
{
const double stepSize =
std::max(1e-10, (std::log(m_SigmaMaximum) - std::log(m_SigmaMinimum)) / (m_NumberOfSigmaSteps - 1));
sigmaValue = std::exp(std::log(m_SigmaMinimum) + stepSize * scaleLevel);
break;
}
default:
throw ExceptionObject(__FILE__, __LINE__, "Invalid SigmaStepMethod.", ITK_LOCATION);
break;
}
return sigmaValue;
}
template <typename TInputImage, typename THessianImage, typename TOutputImage>
void
MultiScaleHessianBasedMeasureImageFilter<TInputImage, THessianImage, TOutputImage>::SetSigmaStepMethodToEquispaced()
{
this->SetSigmaStepMethod(Self::SigmaStepMethodEnum::EquispacedSigmaSteps);
}
template <typename TInputImage, typename THessianImage, typename TOutputImage>
void
MultiScaleHessianBasedMeasureImageFilter<TInputImage, THessianImage, TOutputImage>::SetSigmaStepMethodToLogarithmic()
{
this->SetSigmaStepMethod(Self::SigmaStepMethodEnum::LogarithmicSigmaSteps);
}
/** Get the image containing the Hessian at which each pixel gave the
* best response */
template <typename TInputImage, typename THessianImage, typename TOutputImage>
auto
MultiScaleHessianBasedMeasureImageFilter<TInputImage, THessianImage, TOutputImage>::GetHessianOutput() const
-> const HessianImageType *
{
return static_cast<const HessianImageType *>(this->ProcessObject::GetOutput(2));
}
/** Get the image containing the scales at which each pixel gave the
* best response */
template <typename TInputImage, typename THessianImage, typename TOutputImage>
auto
MultiScaleHessianBasedMeasureImageFilter<TInputImage, THessianImage, TOutputImage>::GetScalesOutput() const
-> const ScalesImageType *
{
return static_cast<const ScalesImageType *>(this->ProcessObject::GetOutput(1));
}
template <typename TInputImage, typename THessianImage, typename TOutputImage>
void
MultiScaleHessianBasedMeasureImageFilter<TInputImage, THessianImage, TOutputImage>::PrintSelf(std::ostream & os,
Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << indent << "SigmaMinimum: " << m_SigmaMinimum << std::endl;
os << indent << "SigmaMaximum: " << m_SigmaMaximum << std::endl;
os << indent << "NumberOfSigmaSteps: " << m_NumberOfSigmaSteps << std::endl;
os << indent << "SigmaStepMethod: " << m_SigmaStepMethod << std::endl;
os << indent << "HessianToMeasureFilter: " << m_HessianToMeasureFilter << std::endl;
os << indent << "NonNegativeHessianBasedMeasure: " << m_NonNegativeHessianBasedMeasure << std::endl;
os << indent << "GenerateScalesOutput: " << m_GenerateScalesOutput << std::endl;
os << indent << "GenerateHessianOutput: " << m_GenerateHessianOutput << std::endl;
}
} // end namespace itk
#endif
|