/usr/include/InsightToolkit/Review/itkOptMatchCardinalityImageToImageMetric.txx is in libinsighttoolkit3-dev 3.20.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | /*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: itkOptMatchCardinalityImageToImageMetric.txx
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm 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 __itkOptMatchCardinalityImageToImageMetric_txx
#define __itkOptMatchCardinalityImageToImageMetric_txx
#include "itkOptMatchCardinalityImageToImageMetric.h"
#include "itkImageRegionConstIteratorWithIndex.h"
namespace itk
{
/**
* Constructor
*/
template <class TFixedImage, class TMovingImage>
MatchCardinalityImageToImageMetric<TFixedImage,TMovingImage>
::MatchCardinalityImageToImageMetric()
{
itkDebugMacro("Constructor");
this->SetComputeGradient(false); // don't use the default gradients
m_MeasureMatches = true; // default to measure percentage of pixel matches
m_Threader = MultiThreader::New();
}
/*
* Get the match Measure
*/
template <class TFixedImage, class TMovingImage>
typename MatchCardinalityImageToImageMetric<TFixedImage,TMovingImage>::MeasureType
MatchCardinalityImageToImageMetric<TFixedImage,TMovingImage>
::GetValue( const TransformParametersType & parameters ) const
{
return const_cast<Self*>(this)->GetNonconstValue(parameters);
}
/**
* Get the match Measure (non const version. spawns threads).
*/
template <class TFixedImage, class TMovingImage>
typename MatchCardinalityImageToImageMetric<TFixedImage,TMovingImage>::MeasureType
MatchCardinalityImageToImageMetric<TFixedImage,TMovingImage>
::GetNonconstValue( const TransformParametersType & parameters )
{
itkDebugMacro("GetValue( " << parameters << " ) ");
FixedImageConstPointer fixedImage = this->m_FixedImage;
if( !fixedImage )
{
itkExceptionMacro( << "Fixed image has not been assigned" );
}
// Initialize some variables before spawning threads
//
//
MeasureType measure = NumericTraits< MeasureType >::Zero;
this->m_NumberOfPixelsCounted = 0;
m_ThreadMatches.clear();
m_ThreadCounts.clear();
m_ThreadMatches.resize( this->GetNumberOfThreads() );
m_ThreadCounts.resize( this->GetNumberOfThreads() );
typename std::vector<MeasureType>::iterator mIt;
std::vector<unsigned long>::iterator cIt;
for (mIt = m_ThreadMatches.begin(), cIt = m_ThreadCounts.begin();
mIt != m_ThreadMatches.end(); ++mIt, ++cIt)
{
*mIt = NumericTraits< MeasureType >::Zero;
*cIt = 0;
}
// store the parameters in the transform so all threads can access them
this->SetTransformParameters( parameters );
// Set up the multithreaded processing
//
//
ThreadStruct str;
str.Metric = this;
this->GetMultiThreader()->SetNumberOfThreads(this->GetNumberOfThreads());
this->GetMultiThreader()->SetSingleMethod(this->ThreaderCallback, &str);
// multithread the execution
//
//
this->GetMultiThreader()->SingleMethodExecute();
// Collect the contribution to the metric for each thread
//
//
for (mIt = m_ThreadMatches.begin(), cIt = m_ThreadCounts.begin();
mIt != m_ThreadMatches.end(); ++mIt, ++cIt)
{
measure += *mIt;
this->m_NumberOfPixelsCounted += *cIt;
}
if( !this->m_NumberOfPixelsCounted )
{
itkExceptionMacro(<<"All the points mapped to outside of the moving image");
}
else
{
measure /= this->m_NumberOfPixelsCounted;
}
return measure;
}
template <class TFixedImage, class TMovingImage>
void
MatchCardinalityImageToImageMetric<TFixedImage,TMovingImage>
::ThreadedGetValue( const FixedImageRegionType ®ionForThread,
int threadId )
{
FixedImageConstPointer fixedImage = this->GetFixedImage();
if( !fixedImage )
{
itkExceptionMacro( << "Fixed image has not been assigned" );
}
typedef ImageRegionConstIteratorWithIndex<FixedImageType> FixedIteratorType;
typename FixedImageType::IndexType index;
FixedIteratorType ti( fixedImage, regionForThread );
MeasureType threadMeasure = NumericTraits< MeasureType >::Zero;
unsigned long threadNumberOfPixelsCounted = 0;
while(!ti.IsAtEnd())
{
index = ti.GetIndex();
typename Superclass::InputPointType inputPoint;
fixedImage->TransformIndexToPhysicalPoint( index, inputPoint );
if( this->GetFixedImageMask() && !this->GetFixedImageMask()->IsInside( inputPoint ) )
{
++ti;
continue;
}
typename Superclass::OutputPointType
transformedPoint = this->GetTransform()->TransformPoint( inputPoint );
if( this->GetMovingImageMask() && !this->GetMovingImageMask()->IsInside( transformedPoint ) )
{
++ti;
continue;
}
if( this->GetInterpolator()->IsInsideBuffer( transformedPoint ) )
{
const RealType movingValue= this->GetInterpolator()->Evaluate( transformedPoint );
const RealType fixedValue = ti.Get();
RealType diff;
threadNumberOfPixelsCounted++;
if (m_MeasureMatches)
{
diff = (movingValue == fixedValue); // count matches
}
else
{
diff = (movingValue != fixedValue); // count mismatches
}
threadMeasure += diff;
}
++ti;
}
m_ThreadMatches[threadId] = threadMeasure;
m_ThreadCounts[threadId] = threadNumberOfPixelsCounted;
}
//----------------------------------------------------------------------------
template <class TFixedImage, class TMovingImage>
int
MatchCardinalityImageToImageMetric<TFixedImage, TMovingImage>
::SplitFixedRegion(int i, int num, FixedImageRegionType& splitRegion)
{
// Get the output pointer
const typename FixedImageRegionType::SizeType& fixedRegionSize
= this->GetFixedImageRegion().GetSize();
int splitAxis;
typename FixedImageRegionType::IndexType splitIndex;
typename FixedImageRegionType::SizeType splitSize;
// Initialize the splitRegion to the output requested region
splitRegion = this->GetFixedImageRegion();
splitIndex = splitRegion.GetIndex();
splitSize = splitRegion.GetSize();
// split on the outermost dimension available
splitAxis = this->GetFixedImage()->GetImageDimension() - 1;
while (fixedRegionSize[splitAxis] == 1)
{
--splitAxis;
if (splitAxis < 0)
{ // cannot split
itkDebugMacro(" Cannot Split");
return 1;
}
}
// determine the actual number of pieces that will be generated
typename FixedImageRegionType::SizeType::SizeValueType range = fixedRegionSize[splitAxis];
int valuesPerThread = Math::Ceil<int>(range/(double)num);
int maxThreadIdUsed = Math::Ceil<int>(range/(double)valuesPerThread) - 1;
// Split the region
if (i < maxThreadIdUsed)
{
splitIndex[splitAxis] += i*valuesPerThread;
splitSize[splitAxis] = valuesPerThread;
}
if (i == maxThreadIdUsed)
{
splitIndex[splitAxis] += i*valuesPerThread;
// last thread needs to process the "rest" dimension being split
splitSize[splitAxis] = splitSize[splitAxis] - i*valuesPerThread;
}
// set the split region ivars
splitRegion.SetIndex( splitIndex );
splitRegion.SetSize( splitSize );
itkDebugMacro(" Split Piece: " << splitRegion );
return maxThreadIdUsed + 1;
}
// Callback routine used by the threading library. This routine just calls
// the ThreadedGenerateData method after setting the correct region for this
// thread.
template <class TFixedImage, class TMovingImage>
ITK_THREAD_RETURN_TYPE
MatchCardinalityImageToImageMetric<TFixedImage, TMovingImage>
::ThreaderCallback( void *arg )
{
ThreadStruct *str;
int total, threadId, threadCount;
threadId = ((MultiThreader::ThreadInfoStruct *)(arg))->ThreadID;
threadCount = ((MultiThreader::ThreadInfoStruct *)(arg))->NumberOfThreads;
str = (ThreadStruct *)(((MultiThreader::ThreadInfoStruct *)(arg))->UserData);
// execute the actual method with appropriate computation region
// first find out how many pieces extent can be split into.
FixedImageRegionType splitRegion;
total = str->Metric->SplitFixedRegion(threadId, threadCount, splitRegion);
if (threadId < total)
{
str->Metric->ThreadedGetValue(splitRegion, threadId);
}
// else
// {
// otherwise don't use this thread. Sometimes the threads dont
// break up very well and it is just as efficient to leave a
// few threads idle.
// }
return ITK_THREAD_RETURN_VALUE;
}
/**
* PrintSelf
*/
template <class TFixedImage, class TMovingImage>
void
MatchCardinalityImageToImageMetric<TFixedImage,TMovingImage>
::PrintSelf(std::ostream& os, Indent indent) const
{
Superclass::PrintSelf( os, indent );
os << indent << "MeasureMatches: " << (m_MeasureMatches ? "On" : "Off") << std::endl;
}
} // end namespace itk
#endif
|