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 367 368 369 370 371 372 373 374 375 376 377 378 379 380
|
/*=========================================================================
*
* 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 itkBlockMatchingImageFilter_hxx
#define itkBlockMatchingImageFilter_hxx
#include "itkImageRegionConstIterator.h"
#include "itkConstNeighborhoodIterator.h"
#include <limits>
#include "itkMultiThreaderBase.h"
#include "itkMakeUniqueForOverwrite.h"
namespace itk
{
template <typename TFixedImage,
typename TMovingImage,
typename TFeatures,
typename TDisplacements,
typename TSimilarities>
BlockMatchingImageFilter<TFixedImage, TMovingImage, TFeatures, TDisplacements, TSimilarities>::
BlockMatchingImageFilter()
{
// defaults
this->m_BlockRadius.Fill(2);
this->m_SearchRadius.Fill(3);
// make the outputs
this->ProcessObject::SetNumberOfRequiredOutputs(2);
typename DisplacementsType::Pointer displacements =
static_cast<DisplacementsType *>(this->MakeOutput(0).GetPointer());
this->SetNthOutput(0, displacements.GetPointer());
typename SimilaritiesType::Pointer similarities = static_cast<SimilaritiesType *>(this->MakeOutput(1).GetPointer());
this->SetNthOutput(1, similarities.GetPointer());
// all inputs are required
this->SetPrimaryInputName("FeaturePoints");
this->AddRequiredInputName("FixedImage");
this->AddRequiredInputName("MovingImage");
}
template <typename TFixedImage,
typename TMovingImage,
typename TFeatures,
typename TDisplacements,
typename TSimilarities>
void
BlockMatchingImageFilter<TFixedImage, TMovingImage, TFeatures, TDisplacements, TSimilarities>::PrintSelf(
std::ostream & os,
Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << indent << "BlockRadius: " << static_cast<typename NumericTraits<ImageSizeType>::PrintType>(m_BlockRadius)
<< std::endl;
os << indent << "SearchRadius: " << static_cast<typename NumericTraits<ImageSizeType>::PrintType>(m_SearchRadius)
<< std::endl;
os << indent << "PointsCount: " << static_cast<typename NumericTraits<SizeValueType>::PrintType>(m_PointsCount)
<< std::endl;
os << indent << "DisplacementsVectorsArray: ";
if (m_DisplacementsVectorsArray != nullptr)
{
os << *m_DisplacementsVectorsArray.get() << std::endl;
}
else
{
os << "(null)" << std::endl;
}
os << indent << "SimilaritiesValuesArray: ";
if (m_SimilaritiesValuesArray != nullptr)
{
os << *m_SimilaritiesValuesArray.get() << std::endl;
}
else
{
os << "(null)" << std::endl;
}
}
template <typename TFixedImage,
typename TMovingImage,
typename TFeatures,
typename TDisplacements,
typename TSimilarities>
void
BlockMatchingImageFilter<TFixedImage, TMovingImage, TFeatures, TDisplacements, TSimilarities>::
GenerateOutputInformation()
{
// We use the constructor defaults for all regions.
}
template <typename TFixedImage,
typename TMovingImage,
typename TFeatures,
typename TDisplacements,
typename TSimilarities>
void
BlockMatchingImageFilter<TFixedImage, TMovingImage, TFeatures, TDisplacements, TSimilarities>::
EnlargeOutputRequestedRegion(DataObject * output)
{
output->SetRequestedRegionToLargestPossibleRegion();
}
template <typename TFixedImage,
typename TMovingImage,
typename TFeatures,
typename TDisplacements,
typename TSimilarities>
void
BlockMatchingImageFilter<TFixedImage, TMovingImage, TFeatures, TDisplacements, TSimilarities>::GenerateData()
{
// Call a method that can be overridden by a subclass to perform
// some calculations prior to splitting the main computations into
// separate threads
this->BeforeThreadedGenerateData();
// Set up the multithreaded processing
ThreadStruct str;
str.Filter = this;
this->GetMultiThreader()->SetNumberOfWorkUnits(this->GetNumberOfWorkUnits());
this->GetMultiThreader()->SetSingleMethodAndExecute(this->ThreaderCallback, &str);
// Call a method that can be overridden by a subclass to perform
// some calculations after all the threads have completed
this->AfterThreadedGenerateData();
}
template <typename TFixedImage,
typename TMovingImage,
typename TFeatures,
typename TDisplacements,
typename TSimilarities>
DataObject::Pointer
BlockMatchingImageFilter<TFixedImage, TMovingImage, TFeatures, TDisplacements, TSimilarities>::MakeOutput(
ProcessObject::DataObjectPointerArraySizeType idx)
{
switch (idx)
{
case 0:
{
return DisplacementsType::New().GetPointer();
}
break;
case 1:
{
return SimilaritiesType::New().GetPointer();
}
break;
}
itkExceptionMacro("Bad output index " << idx);
}
template <typename TFixedImage,
typename TMovingImage,
typename TFeatures,
typename TDisplacements,
typename TSimilarities>
void
BlockMatchingImageFilter<TFixedImage, TMovingImage, TFeatures, TDisplacements, TSimilarities>::
BeforeThreadedGenerateData()
{
this->m_PointsCount = SizeValueType{};
FeaturePointsConstPointer featurePoints = this->GetFeaturePoints();
if (featurePoints)
{
this->m_PointsCount = featurePoints->GetNumberOfPoints();
}
if (this->m_PointsCount < 1)
{
itkExceptionMacro("Invalid number of feature points: " << this->m_PointsCount << '.');
}
this->m_DisplacementsVectorsArray = make_unique_for_overwrite<DisplacementsVector[]>(this->m_PointsCount);
this->m_SimilaritiesValuesArray = make_unique_for_overwrite<SimilaritiesValue[]>(this->m_PointsCount);
}
template <typename TFixedImage,
typename TMovingImage,
typename TFeatures,
typename TDisplacements,
typename TSimilarities>
void
BlockMatchingImageFilter<TFixedImage, TMovingImage, TFeatures, TDisplacements, TSimilarities>::
AfterThreadedGenerateData()
{
FeaturePointsConstPointer featurePoints = this->GetFeaturePoints();
const typename FeaturePointsType::PointsContainer * points;
if (featurePoints)
{
points = featurePoints->GetPoints();
DisplacementsPointer displacements = this->GetDisplacements();
using DisplacementsPointsContainerPointerType = typename DisplacementsType::PointsContainerPointer;
using DisplacementsPointsContainerType = typename DisplacementsType::PointsContainer;
DisplacementsPointsContainerPointerType displacementsPoints = DisplacementsPointsContainerType::New();
using DisplacementsPointDataContainerPointerType = typename DisplacementsType::PointDataContainerPointer;
using DisplacementsPointDataContainerType = typename DisplacementsType::PointDataContainer;
DisplacementsPointDataContainerPointerType displacementsData = DisplacementsPointDataContainerType::New();
SimilaritiesPointer similarities = this->GetSimilarities();
using SimilaritiesPointsContainerPointerType = typename SimilaritiesType::PointsContainerPointer;
using SimilaritiesPointsContainerType = typename SimilaritiesType::PointsContainer;
SimilaritiesPointsContainerPointerType similaritiesPoints = SimilaritiesPointsContainerType::New();
using SimilaritiesPointDataContainerPointerType = typename SimilaritiesType::PointDataContainerPointer;
using SimilaritiesPointDataContainerType = typename SimilaritiesType::PointDataContainer;
SimilaritiesPointDataContainerPointerType similaritiesData = SimilaritiesPointDataContainerType::New();
// insert displacements and similarities
for (SizeValueType i = 0; i < this->m_PointsCount; ++i)
{
displacementsPoints->InsertElement(i, points->GetElement(i));
similaritiesPoints->InsertElement(i, points->GetElement(i));
displacementsData->InsertElement(i, this->m_DisplacementsVectorsArray[i]);
similaritiesData->InsertElement(i, this->m_SimilaritiesValuesArray[i]);
}
displacements->SetPoints(displacementsPoints);
displacements->SetPointData(displacementsData);
similarities->SetPoints(similaritiesPoints);
similarities->SetPointData(similaritiesData);
}
// clean up
m_DisplacementsVectorsArray.reset();
m_SimilaritiesValuesArray.reset();
}
template <typename TFixedImage,
typename TMovingImage,
typename TFeatures,
typename TDisplacements,
typename TSimilarities>
ITK_THREAD_RETURN_FUNCTION_CALL_CONVENTION
BlockMatchingImageFilter<TFixedImage, TMovingImage, TFeatures, TDisplacements, TSimilarities>::ThreaderCallback(
void * arg)
{
auto * str = (ThreadStruct *)(((MultiThreaderBase::WorkUnitInfo *)(arg))->UserData);
ThreadIdType workUnitID = ((MultiThreaderBase::WorkUnitInfo *)(arg))->WorkUnitID;
str->Filter->ThreadedGenerateData(workUnitID);
return ITK_THREAD_RETURN_DEFAULT_VALUE;
}
template <typename TFixedImage,
typename TMovingImage,
typename TFeatures,
typename TDisplacements,
typename TSimilarities>
void
BlockMatchingImageFilter<TFixedImage, TMovingImage, TFeatures, TDisplacements, TSimilarities>::ThreadedGenerateData(
ThreadIdType threadId)
{
FixedImageConstPointer fixedImage = this->GetFixedImage();
MovingImageConstPointer movingImage = this->GetMovingImage();
FeaturePointsConstPointer featurePoints = this->GetFeaturePoints();
SizeValueType workUnitCount = this->GetNumberOfWorkUnits();
// compute first point and number of points (count) for this thread
SizeValueType count = m_PointsCount / workUnitCount;
SizeValueType first = threadId * count;
if (threadId + 1 == workUnitCount) // last thread
{
count += this->m_PointsCount % workUnitCount;
}
// start constructing window region and center region (single voxel)
ImageRegionType window;
ImageRegionType center;
ImageSizeType windowSize;
windowSize.Fill(1);
center.SetSize(windowSize); // size of center region is 1
windowSize += m_SearchRadius + m_SearchRadius;
window.SetSize(windowSize); // size of window region is 1+2*m_BlockHalfWindow
// start constructing block iterator
SizeValueType numberOfVoxelInBlock = 1;
for (unsigned int i = 0; i < ImageSizeType::Dimension; ++i)
{
numberOfVoxelInBlock *= m_BlockRadius[i] + 1 + m_BlockRadius[i];
}
// loop thru feature points
for (SizeValueType idx = first, last = first + count; idx < last; ++idx)
{
FeaturePointsPhysicalCoordinates originalLocation = featurePoints->GetPoint(idx);
const auto fixedIndex = fixedImage->TransformPhysicalPointToIndex(originalLocation);
const auto movingIndex = movingImage->TransformPhysicalPointToIndex(originalLocation);
// the block is selected for a minimum similarity metric
SimilaritiesValue similarity{};
// New point location
DisplacementsVector displacement;
// set centers of window and center regions to current location
ImageIndexType start = fixedIndex - this->m_SearchRadius;
window.SetIndex(start);
center.SetIndex(movingIndex);
// iterate over neighborhoods in region window, for each neighborhood: iterate over voxels in blockRadius
ConstNeighborhoodIterator<FixedImageType> windowIterator(m_BlockRadius, fixedImage, window);
// iterate over voxels in neighborhood of current feature point
ConstNeighborhoodIterator<MovingImageType> centerIterator(m_BlockRadius, movingImage, center);
centerIterator.GoToBegin();
// iterate over neighborhoods in region window
for (windowIterator.GoToBegin(); !windowIterator.IsAtEnd(); ++windowIterator)
{
SimilaritiesValue fixedSum{};
SimilaritiesValue fixedSumOfSquares{};
SimilaritiesValue movingSum{};
SimilaritiesValue movingSumOfSquares{};
SimilaritiesValue covariance{};
// iterate over voxels in blockRadius
for (SizeValueType i = 0; i < numberOfVoxelInBlock; ++i) // windowIterator.Size() == numberOfVoxelInBlock
{
const SimilaritiesValue fixedValue = windowIterator.GetPixel(i);
const SimilaritiesValue movingValue = centerIterator.GetPixel(i);
movingSum += movingValue;
fixedSum += fixedValue;
movingSumOfSquares += movingValue * movingValue;
fixedSumOfSquares += fixedValue * fixedValue;
covariance += fixedValue * movingValue;
}
const SimilaritiesValue fixedMean = fixedSum / numberOfVoxelInBlock;
const SimilaritiesValue movingMean = movingSum / numberOfVoxelInBlock;
const SimilaritiesValue fixedVariance = fixedSumOfSquares - numberOfVoxelInBlock * fixedMean * fixedMean;
const SimilaritiesValue movingVariance = movingSumOfSquares - numberOfVoxelInBlock * movingMean * movingMean;
covariance -= numberOfVoxelInBlock * fixedMean * movingMean;
SimilaritiesValue sim{};
if ((fixedVariance * movingVariance) != 0.0)
{
sim = (covariance * covariance) / (fixedVariance * movingVariance);
}
if (sim >= similarity)
{
FeaturePointsPhysicalCoordinates newLocation;
fixedImage->TransformIndexToPhysicalPoint(windowIterator.GetIndex(), newLocation);
displacement = newLocation - originalLocation;
similarity = sim;
}
}
this->m_DisplacementsVectorsArray[idx] = displacement;
this->m_SimilaritiesValuesArray[idx] = similarity;
}
}
} // end namespace itk
#endif
|