1 /*=========================================================================
2 Program: vv http://www.creatis.insa-lyon.fr/rio/vv
5 - University of LYON http://www.universite-lyon.fr/
6 - Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr
7 - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr
9 This software is distributed WITHOUT ANY WARRANTY; without even
10 the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
11 PURPOSE. See the copyright notices for more information.
13 It is distributed under dual licence
15 - BSD See included LICENSE.txt file
16 - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
17 ======================================================================-====*/
19 #ifndef __clitkOptNormalizedCorrelationImageToImageMetric_txx
20 #define __clitkOptNormalizedCorrelationImageToImageMetric_txx
22 #include "clitkOptNormalizedCorrelationImageToImageMetric.h"
23 #include "itkCovariantVector.h"
24 #include "itkImageRandomConstIteratorWithIndex.h"
25 #include "itkImageRegionConstIterator.h"
26 #include "itkImageRegionIterator.h"
27 #include "itkImageIterator.h"
28 #include "vnl/vnl_math.h"
36 template < class TFixedImage, class TMovingImage >
37 NormalizedCorrelationImageToImageMetric<TFixedImage,TMovingImage>
38 ::NormalizedCorrelationImageToImageMetric()
40 this->SetComputeGradient(true);
47 m_ThreaderDerivativeF = NULL;
48 m_ThreaderDerivativeM = NULL;
49 this->m_WithinThreadPreProcess = false;
50 this->m_WithinThreadPostProcess = false;
52 // For backward compatibility, the default behavior is to use all the pixels
53 // in the fixed image.
54 this->UseAllPixelsOn();
60 template < class TFixedImage, class TMovingImage >
61 NormalizedCorrelationImageToImageMetric<TFixedImage,TMovingImage>
62 ::~NormalizedCorrelationImageToImageMetric()
64 if(m_ThreaderSFF != NULL) {
65 delete [] m_ThreaderSFF;
69 if(m_ThreaderSMM != NULL) {
70 delete [] m_ThreaderSMM;
74 if(m_ThreaderSFM != NULL) {
75 delete [] m_ThreaderSFM;
79 if(m_ThreaderSF != NULL) {
80 delete [] m_ThreaderSF;
84 if(m_ThreaderSM != NULL) {
85 delete [] m_ThreaderSM;
89 if(m_ThreaderDerivativeF != NULL) {
90 delete [] m_ThreaderDerivativeF;
92 m_ThreaderDerivativeF = NULL;
94 if(m_ThreaderDerivativeM != NULL) {
95 delete [] m_ThreaderDerivativeM;
97 m_ThreaderDerivativeM = NULL;
101 * Print out internal information about this class
103 template < class TFixedImage, class TMovingImage >
105 NormalizedCorrelationImageToImageMetric<TFixedImage,TMovingImage>
106 ::PrintSelf(std::ostream& os, itk::Indent indent) const
109 Superclass::PrintSelf(os, indent);
117 template <class TFixedImage, class TMovingImage>
119 NormalizedCorrelationImageToImageMetric<TFixedImage,TMovingImage>
120 ::Initialize(void) throw ( itk::ExceptionObject )
123 this->Superclass::Initialize();
124 this->Superclass::MultiThreadingInitialize();
128 * Allocate memory for the accumulators (set to zero in GetValue)
130 if(m_ThreaderSFF != NULL) {
131 delete [] m_ThreaderSFF;
133 m_ThreaderSFF = new double[this->m_NumberOfThreads];
136 if(m_ThreaderSMM != NULL) {
137 delete [] m_ThreaderSMM;
139 m_ThreaderSMM = new double[this->m_NumberOfThreads];
141 if(m_ThreaderSFM != NULL) {
142 delete [] m_ThreaderSFM;
144 m_ThreaderSFM = new double[this->m_NumberOfThreads];
146 if(this->m_SubtractMean) {
147 if(m_ThreaderSF != NULL) {
148 delete [] m_ThreaderSF;
150 m_ThreaderSF = new double[this->m_NumberOfThreads];
152 if(m_ThreaderSM != NULL) {
153 delete [] m_ThreaderSM;
155 m_ThreaderSM = new double[this->m_NumberOfThreads];
158 if(m_ThreaderDerivativeF != NULL) {
159 delete [] m_ThreaderDerivativeF;
161 m_ThreaderDerivativeF = new DerivativeType[this->m_NumberOfThreads];
162 for(unsigned int threadID=0; threadID<this->m_NumberOfThreads; threadID++) {
163 m_ThreaderDerivativeF[threadID].SetSize( this->m_NumberOfParameters );
166 if(m_ThreaderDerivativeM != NULL) {
167 delete [] m_ThreaderDerivativeM;
169 m_ThreaderDerivativeM = new DerivativeType[this->m_NumberOfThreads];
170 for(unsigned int threadID=0; threadID<this->m_NumberOfThreads; threadID++) {
171 m_ThreaderDerivativeM[threadID].SetSize( this->m_NumberOfParameters );
176 template < class TFixedImage, class TMovingImage >
178 NormalizedCorrelationImageToImageMetric<TFixedImage,TMovingImage>
179 ::GetValueThreadProcessSample(
180 unsigned int threadID,
181 unsigned long fixedImageSample,
182 const MovingImagePointType & itkNotUsed(mappedPoint),
183 double movingImageValue) const
185 const RealType fixedImageValue= this->m_FixedImageSamples[fixedImageSample].value;
186 m_ThreaderSFF[threadID] += fixedImageValue * fixedImageValue;
187 m_ThreaderSMM[threadID] += movingImageValue * movingImageValue;
188 m_ThreaderSFM[threadID] += fixedImageValue * movingImageValue;
189 if ( this->m_SubtractMean ) {
190 m_ThreaderSF[threadID] += fixedImageValue;
191 m_ThreaderSM[threadID] += movingImageValue;
197 template < class TFixedImage, class TMovingImage >
198 typename NormalizedCorrelationImageToImageMetric<TFixedImage,TMovingImage>
200 NormalizedCorrelationImageToImageMetric<TFixedImage,TMovingImage>
201 ::GetValue( const ParametersType & parameters ) const
203 itkDebugMacro("GetValue( " << parameters << " ) ");
205 if( !this->m_FixedImage ) {
206 itkExceptionMacro( << "Fixed image has not been assigned" );
210 //Reset the accumulators
211 memset( m_ThreaderSFF, 0, this->m_NumberOfThreads * sizeof(AccumulateType) );
212 memset( m_ThreaderSMM, 0, this->m_NumberOfThreads * sizeof(AccumulateType) );
213 memset( m_ThreaderSFM, 0, this->m_NumberOfThreads * sizeof(AccumulateType) );
214 if(this->m_SubtractMean) {
215 memset( m_ThreaderSF, 0, this->m_NumberOfThreads * sizeof(AccumulateType) );
216 memset( m_ThreaderSM, 0, this->m_NumberOfThreads * sizeof(AccumulateType) );
220 // Set up the parameters in the transform
221 this->m_Transform->SetParameters( parameters );
222 this->m_Parameters = parameters;
224 // MUST BE CALLED TO INITIATE PROCESSING
225 this->GetValueMultiThreadedInitiate();
227 itkDebugMacro( "Ratio of voxels mapping into moving image buffer: "
228 << this->m_NumberOfPixelsCounted << " / "
229 << this->m_NumberOfFixedImageSamples
232 if( this->m_NumberOfPixelsCounted <
233 this->m_NumberOfFixedImageSamples / 4 ) {
234 itkExceptionMacro( "Too many samples map outside moving image buffer: "
235 << this->m_NumberOfPixelsCounted << " / "
236 << this->m_NumberOfFixedImageSamples
240 // Accumulate the threads
241 AccumulateType sff, smm, sfm, sf, sm;
242 sff = m_ThreaderSFF[0];
243 smm = m_ThreaderSMM[0];
244 sfm = m_ThreaderSFM[0];
245 sf = m_ThreaderSF[0];
246 sm = m_ThreaderSM[0];
248 for(unsigned int t=1; t<this->m_NumberOfThreads; t++) {
249 sff += m_ThreaderSFF[t];
250 smm += m_ThreaderSMM[t];
251 sfm += m_ThreaderSFM[t];
252 if ( this->m_SubtractMean ) {
253 sf += m_ThreaderSF[t];
254 sm += m_ThreaderSM[t];
258 if ( this->m_SubtractMean && this->m_NumberOfPixelsCounted > 0 ) {
259 sff -= ( sf * sf / this->m_NumberOfPixelsCounted );
260 smm -= ( sm * sm / this->m_NumberOfPixelsCounted );
261 sfm -= ( sf * sm / this->m_NumberOfPixelsCounted );
265 const RealType denom = -1.0 * vcl_sqrt(sff * smm );
267 if( this->m_NumberOfPixelsCounted > 0 && denom != 0.0) {
268 measure = sfm / denom;
270 measure = itk::NumericTraits< MeasureType >::Zero;
278 template < class TFixedImage, class TMovingImage >
279 typename NormalizedCorrelationImageToImageMetric<TFixedImage,TMovingImage>
281 NormalizedCorrelationImageToImageMetric<TFixedImage,TMovingImage>
282 ::ComputeSums( const ParametersType & parameters ) const
284 //No checking for the fixed image, done in the caller
285 //Reset the accumulators
286 memset( m_ThreaderSFF, 0, this->m_NumberOfThreads * sizeof(AccumulateType) );
287 memset( m_ThreaderSMM, 0, this->m_NumberOfThreads * sizeof(AccumulateType) );
288 memset( m_ThreaderSFM, 0, this->m_NumberOfThreads * sizeof(AccumulateType) );
289 if(this->m_SubtractMean) {
290 memset( m_ThreaderSF, 0, this->m_NumberOfThreads * sizeof(AccumulateType) );
291 memset( m_ThreaderSM, 0, this->m_NumberOfThreads * sizeof(AccumulateType) );
295 // Set up the parameters in the transform
296 this->m_Transform->SetParameters( parameters );
297 this->m_Parameters = parameters;
299 // MUST BE CALLED TO INITIATE PROCESSING
300 this->GetValueMultiThreadedInitiate();
302 itkDebugMacro( "Ratio of voxels mapping into moving image buffer: "
303 << this->m_NumberOfPixelsCounted << " / "
304 << this->m_NumberOfFixedImageSamples
307 if( this->m_NumberOfPixelsCounted <
308 this->m_NumberOfFixedImageSamples / 4 ) {
309 itkExceptionMacro( "Too many samples map outside moving image buffer: "
310 << this->m_NumberOfPixelsCounted << " / "
311 << this->m_NumberOfFixedImageSamples
315 // Accumulate the threads
316 m_SFF = m_ThreaderSFF[0];
317 m_SMM = m_ThreaderSMM[0];
318 m_SFM = m_ThreaderSFM[0];
319 m_SF = m_ThreaderSF[0];
320 m_SM = m_ThreaderSM[0];
322 for(unsigned int t=1; t<this->m_NumberOfThreads; t++) {
323 m_SFF += m_ThreaderSFF[t];
324 m_SMM += m_ThreaderSMM[t];
325 m_SFM += m_ThreaderSFM[t];
326 if ( this->m_SubtractMean ) {
327 m_SF += m_ThreaderSF[t];
328 m_SM += m_ThreaderSM[t];
332 if ( this->m_SubtractMean && this->m_NumberOfPixelsCounted > 0 ) {
333 m_SFF -= ( m_SF * m_SF / this->m_NumberOfPixelsCounted );
334 m_SMM -= ( m_SM * m_SM / this->m_NumberOfPixelsCounted );
335 m_SFM -= ( m_SF * m_SM / this->m_NumberOfPixelsCounted );
336 m_FixedMean=m_SF / this->m_NumberOfPixelsCounted;
337 m_MovingMean=m_SM / this->m_NumberOfPixelsCounted;
341 m_Denom = -1.0 * vcl_sqrt(m_SFF * m_SMM );
343 if( this->m_NumberOfPixelsCounted > 0 && m_Denom != 0.0) {
344 measure = m_SFM / m_Denom;
346 measure = itk::NumericTraits< MeasureType >::Zero;
353 template < class TFixedImage, class TMovingImage >
355 NormalizedCorrelationImageToImageMetric<TFixedImage,TMovingImage>
356 ::GetValueAndDerivativeThreadProcessSample(
357 unsigned int threadID,
358 unsigned long fixedImageSample,
359 const MovingImagePointType & itkNotUsed(mappedPoint),
360 double movingImageValue,
361 const ImageDerivativesType &
362 movingImageGradientValue
366 const RealType fixedImageValue=this->m_FixedImageSamples[fixedImageSample].value;
367 const FixedImagePointType fixedImagePoint = this->m_FixedImageSamples[fixedImageSample].point;
369 // Need to use one of the threader transforms if we're
372 // Use a raw pointer here to avoid the overhead of smart pointers.
373 // For instance, Register and UnRegister have mutex locks around
374 // the reference counts.
375 TransformType* transform;
378 transform = this->m_ThreaderTransform[threadID - 1];
380 transform = this->m_Transform;
383 // Jacobian should be evaluated at the unmapped (fixed image) point.
384 const TransformJacobianType & jacobian = transform
385 ->GetJacobian( fixedImagePoint );
387 for(unsigned int par=0; par<this->m_NumberOfParameters; par++) {
388 RealType sumF = itk::NumericTraits< RealType >::Zero;
389 RealType sumM = itk::NumericTraits< RealType >::Zero;
390 for(unsigned int dim=0; dim<MovingImageDimension; dim++) {
391 const RealType differential = jacobian( dim, par ) * movingImageGradientValue[dim];
392 if ( this->m_SubtractMean && this->m_NumberOfPixelsCounted > 0 ) {
393 sumF += (fixedImageValue-m_FixedMean) * differential;
394 sumM += (movingImageValue-m_MovingMean) * differential;
396 sumF += differential * fixedImageValue;
397 sumM += differential * movingImageValue;
400 m_ThreaderDerivativeF[threadID][par] += sumF;
401 m_ThreaderDerivativeM[threadID][par] += sumM;
409 * Get the both Value and Derivative Measure
411 template < class TFixedImage, class TMovingImage >
413 NormalizedCorrelationImageToImageMetric<TFixedImage,TMovingImage>
414 ::GetValueAndDerivative( const ParametersType & parameters,
416 DerivativeType & derivative) const
419 if( !this->m_FixedImage ) {
420 itkExceptionMacro( << "Fixed image has not been assigned" );
423 // Set up the parameters in the transform
424 this->m_Transform->SetParameters( parameters );
425 this->m_Parameters = parameters;
427 //We need the sums and the value to be calculated first
428 value=this->ComputeSums(parameters);
430 //Set output values to zero
431 if(derivative.GetSize() != this->m_NumberOfParameters) {
432 derivative = DerivativeType( this->m_NumberOfParameters );
434 memset( derivative.data_block(),
436 this->m_NumberOfParameters * sizeof(ITK_TYPENAME DerivativeType::ValueType) );
438 for( unsigned int threadID = 0; threadID<this->m_NumberOfThreads; threadID++ ) {
439 memset( m_ThreaderDerivativeF[threadID].data_block(),
441 this->m_NumberOfParameters * sizeof(ITK_TYPENAME DerivativeType::ValueType) );
443 memset( m_ThreaderDerivativeM[threadID].data_block(),
445 this->m_NumberOfParameters * sizeof(ITK_TYPENAME DerivativeType::ValueType) );
448 // MUST BE CALLED TO INITIATE PROCESSING
449 this->GetValueAndDerivativeMultiThreadedInitiate();
451 // Accumulate over the threads
452 DerivativeType derivativeF(this->m_NumberOfParameters), derivativeM(this->m_NumberOfParameters);
453 for(unsigned int t=0; t<this->m_NumberOfThreads; t++) {
454 for(unsigned int parameter = 0; parameter < this->m_NumberOfParameters; parameter++) {
455 derivativeF[parameter] += m_ThreaderDerivativeF[t][parameter];
456 derivativeM[parameter] += m_ThreaderDerivativeM[t][parameter];
460 //Compute derivatives
461 if( this->m_NumberOfPixelsCounted > 0 && m_Denom != 0.0) {
462 for(unsigned int i=0; i<this->m_NumberOfParameters; i++) {
463 derivative[i] = ( derivativeF[i] - (m_SFM/m_SMM)* derivativeM[i] ) / m_Denom;
466 for(unsigned int i=0; i<this->m_NumberOfParameters; i++) {
467 derivative[i] = itk::NumericTraits< MeasureType >::Zero;
475 * Get the match measure derivative
477 template < class TFixedImage, class TMovingImage >
479 NormalizedCorrelationImageToImageMetric<TFixedImage,TMovingImage>
480 ::GetDerivative( const ParametersType & parameters,
481 DerivativeType & derivative ) const
483 if( !this->m_FixedImage ) {
484 itkExceptionMacro( << "Fixed image has not been assigned" );
488 // call the combined version
489 this->GetValueAndDerivative( parameters, value, derivative );
492 } // end namespace clitk