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://www.centreleonberard.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 __clitkOptNormalizedCorrelationImageToImageMetricFor3DBLUTFFD_txx
20 #define __clitkOptNormalizedCorrelationImageToImageMetricFor3DBLUTFFD_txx
22 #include "clitkOptNormalizedCorrelationImageToImageMetricFor3DBLUTFFD.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 NormalizedCorrelationImageToImageMetricFor3DBLUTFFD<TFixedImage,TMovingImage>
38 ::NormalizedCorrelationImageToImageMetricFor3DBLUTFFD()
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 NormalizedCorrelationImageToImageMetricFor3DBLUTFFD<TFixedImage,TMovingImage>
62 ::~NormalizedCorrelationImageToImageMetricFor3DBLUTFFD()
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 NormalizedCorrelationImageToImageMetricFor3DBLUTFFD<TFixedImage,TMovingImage>
106 ::PrintSelf(std::ostream& os, itk::Indent indent) const
109 Superclass::PrintSelf(os, indent);
117 template <class TFixedImage, class TMovingImage>
119 NormalizedCorrelationImageToImageMetricFor3DBLUTFFD<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 NormalizedCorrelationImageToImageMetricFor3DBLUTFFD<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 NormalizedCorrelationImageToImageMetricFor3DBLUTFFD<TFixedImage,TMovingImage>
200 NormalizedCorrelationImageToImageMetricFor3DBLUTFFD<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 );
223 // MUST BE CALLED TO INITIATE PROCESSING
224 this->GetValueMultiThreadedInitiate();
226 itkDebugMacro( "Ratio of voxels mapping into moving image buffer: "
227 << this->m_NumberOfPixelsCounted << " / "
228 << this->m_NumberOfFixedImageSamples
231 if( this->m_NumberOfPixelsCounted <
232 this->m_NumberOfFixedImageSamples / 4 ) {
233 itkExceptionMacro( "Too many samples map outside moving image buffer: "
234 << this->m_NumberOfPixelsCounted << " / "
235 << this->m_NumberOfFixedImageSamples
239 // Accumulate the threads
240 AccumulateType sff, smm, sfm, sf, sm;
241 sff = m_ThreaderSFF[0];
242 smm = m_ThreaderSMM[0];
243 sfm = m_ThreaderSFM[0];
244 sf = m_ThreaderSF[0];
245 sm = m_ThreaderSM[0];
247 for(unsigned int t=1; t<this->m_NumberOfThreads; t++) {
248 sff += m_ThreaderSFF[t];
249 smm += m_ThreaderSMM[t];
250 sfm += m_ThreaderSFM[t];
251 if ( this->m_SubtractMean ) {
252 sf += m_ThreaderSF[t];
253 sm += m_ThreaderSM[t];
257 if ( this->m_SubtractMean && this->m_NumberOfPixelsCounted > 0 ) {
258 sff -= ( sf * sf / this->m_NumberOfPixelsCounted );
259 smm -= ( sm * sm / this->m_NumberOfPixelsCounted );
260 sfm -= ( sf * sm / this->m_NumberOfPixelsCounted );
264 const RealType denom = -1.0 * vcl_sqrt(sff * smm );
266 if( this->m_NumberOfPixelsCounted > 0 && denom != 0.0) {
267 measure = sfm / denom;
269 measure = itk::NumericTraits< MeasureType >::Zero;
277 template < class TFixedImage, class TMovingImage >
278 typename NormalizedCorrelationImageToImageMetricFor3DBLUTFFD<TFixedImage,TMovingImage>
280 NormalizedCorrelationImageToImageMetricFor3DBLUTFFD<TFixedImage,TMovingImage>
281 ::ComputeSums( const ParametersType & parameters ) const
283 //No checking for the fixed image, done in the caller
284 //Reset the accumulators
285 memset( m_ThreaderSFF, 0, this->m_NumberOfThreads * sizeof(AccumulateType) );
286 memset( m_ThreaderSMM, 0, this->m_NumberOfThreads * sizeof(AccumulateType) );
287 memset( m_ThreaderSFM, 0, this->m_NumberOfThreads * sizeof(AccumulateType) );
288 if(this->m_SubtractMean) {
289 memset( m_ThreaderSF, 0, this->m_NumberOfThreads * sizeof(AccumulateType) );
290 memset( m_ThreaderSM, 0, this->m_NumberOfThreads * sizeof(AccumulateType) );
294 // Set up the parameters in the transform
295 this->m_Transform->SetParameters( parameters );
297 // MUST BE CALLED TO INITIATE PROCESSING
298 this->GetValueMultiThreadedInitiate();
300 itkDebugMacro( "Ratio of voxels mapping into moving image buffer: "
301 << this->m_NumberOfPixelsCounted << " / "
302 << this->m_NumberOfFixedImageSamples
305 if( this->m_NumberOfPixelsCounted <
306 this->m_NumberOfFixedImageSamples / 4 ) {
307 itkExceptionMacro( "Too many samples map outside moving image buffer: "
308 << this->m_NumberOfPixelsCounted << " / "
309 << this->m_NumberOfFixedImageSamples
313 // Accumulate the threads
314 m_SFF = m_ThreaderSFF[0];
315 m_SMM = m_ThreaderSMM[0];
316 m_SFM = m_ThreaderSFM[0];
317 m_SF = m_ThreaderSF[0];
318 m_SM = m_ThreaderSM[0];
320 for(unsigned int t=1; t<this->m_NumberOfThreads; t++) {
321 m_SFF += m_ThreaderSFF[t];
322 m_SMM += m_ThreaderSMM[t];
323 m_SFM += m_ThreaderSFM[t];
324 if ( this->m_SubtractMean ) {
325 m_SF += m_ThreaderSF[t];
326 m_SM += m_ThreaderSM[t];
330 if ( this->m_SubtractMean && this->m_NumberOfPixelsCounted > 0 ) {
331 m_SFF -= ( m_SF * m_SF / this->m_NumberOfPixelsCounted );
332 m_SMM -= ( m_SM * m_SM / this->m_NumberOfPixelsCounted );
333 m_SFM -= ( m_SF * m_SM / this->m_NumberOfPixelsCounted );
334 m_FixedMean=m_SF / this->m_NumberOfPixelsCounted;
335 m_MovingMean=m_SM / this->m_NumberOfPixelsCounted;
339 m_Denom = -1.0 * vcl_sqrt(m_SFF * m_SMM );
341 if( this->m_NumberOfPixelsCounted > 0 && m_Denom != 0.0) {
342 measure = m_SFM / m_Denom;
344 measure = itk::NumericTraits< MeasureType >::Zero;
351 template < class TFixedImage, class TMovingImage >
353 NormalizedCorrelationImageToImageMetricFor3DBLUTFFD<TFixedImage,TMovingImage>
354 ::GetValueAndDerivativeThreadProcessSample(
355 unsigned int threadID,
356 unsigned long fixedImageSample,
357 const MovingImagePointType & itkNotUsed(mappedPoint),
358 double movingImageValue,
359 const ImageDerivativesType &
360 movingImageGradientValue
364 const RealType fixedImageValue=this->m_FixedImageSamples[fixedImageSample].value;
365 const FixedImagePointType fixedImagePoint = this->m_FixedImageSamples[fixedImageSample].point;
367 // Need to use one of the threader transforms if we're
370 // Use a raw pointer here to avoid the overhead of smart pointers.
371 // For instance, Register and UnRegister have mutex locks around
372 // the reference counts.
373 TransformType* transform;
376 transform = this->m_ThreaderTransform[threadID - 1];
378 transform = this->m_Transform;
381 // Jacobian should be evaluated at the unmapped (fixed image) point.
382 TransformJacobianType jacobian;
383 transform->ComputeJacobianWithRespectToParameters( fixedImagePoint, jacobian );
385 // for(unsigned int par=0; par<this->m_NumberOfParameters; par++)
387 // RealType sumF = itk::NumericTraits< RealType >::Zero;
388 // RealType sumM = itk::NumericTraits< RealType >::Zero;
389 // 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 )
394 // sumF += (fixedImageValue-m_FixedMean) * differential;
395 // sumM += (movingImageValue-m_MovingMean) * differential;
399 // sumF += differential * fixedImageValue;
400 // sumM += differential * movingImageValue;
403 // m_ThreaderDerivativeF[threadID][par] += sumF;
404 // m_ThreaderDerivativeM[threadID][par] += sumM;
408 unsigned int par, dim;
409 RealType differential;
410 for( par=0; par<this->m_NumberOfParameters; par+=3) {
411 // JV only for 3D Space BLUT FFD: if J(0, par)=0, then J(1,par+1)=0 & ...
412 if (jacobian( 0, par ) ) {
413 for(dim=0; dim<3; dim++) {
414 differential = jacobian( dim, par+dim ) * movingImageGradientValue[dim];
416 if ( this->m_SubtractMean && this->m_NumberOfPixelsCounted > 0 ) {
417 m_ThreaderDerivativeF[threadID][par+dim]+= (fixedImageValue-m_FixedMean) * differential;
418 m_ThreaderDerivativeM[threadID][par+dim]+= (movingImageValue-m_MovingMean) * differential;
420 m_ThreaderDerivativeF[threadID][par+dim]+= differential * fixedImageValue;
421 m_ThreaderDerivativeM[threadID][par+dim]+= differential * movingImageValue;
432 * Get the both Value and Derivative Measure
434 template < class TFixedImage, class TMovingImage >
436 NormalizedCorrelationImageToImageMetricFor3DBLUTFFD<TFixedImage,TMovingImage>
437 ::GetValueAndDerivative( const ParametersType & parameters,
439 DerivativeType & derivative) const
442 if( !this->m_FixedImage ) {
443 itkExceptionMacro( << "Fixed image has not been assigned" );
446 // Set up the parameters in the transform
447 this->m_Transform->SetParameters( parameters );
449 //We need the sums and the value to be calculated first
450 value=this->ComputeSums(parameters);
452 //Set output values to zero
453 if(derivative.GetSize() != this->m_NumberOfParameters) {
454 derivative = DerivativeType( this->m_NumberOfParameters );
456 memset( derivative.data_block(),
458 this->m_NumberOfParameters * sizeof(typename DerivativeType::ValueType) );
460 for( unsigned int threadID = 0; threadID<this->m_NumberOfThreads; threadID++ ) {
461 memset( m_ThreaderDerivativeF[threadID].data_block(),
463 this->m_NumberOfParameters * sizeof(typename DerivativeType::ValueType) );
465 memset( m_ThreaderDerivativeM[threadID].data_block(),
467 this->m_NumberOfParameters * sizeof(typename DerivativeType::ValueType) );
470 // MUST BE CALLED TO INITIATE PROCESSING
471 this->GetValueAndDerivativeMultiThreadedInitiate();
473 // Accumulate over the threads
474 DerivativeType derivativeF(this->m_NumberOfParameters), derivativeM(this->m_NumberOfParameters);
475 for(unsigned int t=0; t<this->m_NumberOfThreads; t++) {
476 for(unsigned int parameter = 0; parameter < this->m_NumberOfParameters; parameter++) {
477 derivativeF[parameter] += m_ThreaderDerivativeF[t][parameter];
478 derivativeM[parameter] += m_ThreaderDerivativeM[t][parameter];
482 //Compute derivatives
483 if( this->m_NumberOfPixelsCounted > 0 && m_Denom != 0.0) {
484 for(unsigned int i=0; i<this->m_NumberOfParameters; i++) {
485 derivative[i] = ( derivativeF[i] - (m_SFM/m_SMM)* derivativeM[i] ) / m_Denom;
488 for(unsigned int i=0; i<this->m_NumberOfParameters; i++) {
489 derivative[i] = itk::NumericTraits< MeasureType >::Zero;
497 * Get the match measure derivative
499 template < class TFixedImage, class TMovingImage >
501 NormalizedCorrelationImageToImageMetricFor3DBLUTFFD<TFixedImage,TMovingImage>
502 ::GetDerivative( const ParametersType & parameters,
503 DerivativeType & derivative ) const
505 if( !this->m_FixedImage ) {
506 itkExceptionMacro( << "Fixed image has not been assigned" );
510 // call the combined version
511 this->GetValueAndDerivative( parameters, value, derivative );
514 } // end namespace clitk