File: BrainModelVolumeBiasCorrection.cxx

package info (click to toggle)
caret 5.6.4~dfsg.1-3
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 31,904 kB
  • ctags: 28,901
  • sloc: cpp: 378,050; python: 6,718; ansic: 5,507; makefile: 333; sh: 46
file content (454 lines) | stat: -rw-r--r-- 15,189 bytes parent folder | download | duplicates (2)
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*LICENSE_START*/
/*
 *  Copyright 1995-2002 Washington University School of Medicine
 *
 *  http://brainmap.wustl.edu
 *
 *  This file is part of CARET.
 *
 *  CARET is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  CARET is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with CARET; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 */
/*LICENSE_END*/

/*=========================================================================

Program:   Insight Segmentation & Registration Toolkit
Module:    $RCSfile: BrainModelVolumeBiasCorrection.cxx,v $
Language:  C++
Date:      $Date: 2009/04/09 15:50:54 $
Version:   $Revision: 1.1.1.1 $

Copyright (c) 2002 Insight 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.

=========================================================================*/

//
// This code adapted from the ITK Application "BiasCorrector.cxx".
//

#include <QString>
#include <vector>
#include <iostream>
#include <fstream>
#include <sstream>

#include "BrainModelVolumeBiasCorrection.h"
#include "DebugControl.h"
#include "StatisticDataGroup.h"
#include "StatisticMeanAndDeviation.h"
#include "VolumeFile.h"

#ifdef HAVE_ITK
#include "itkCompositeValleyFunction.h"
#include "itkMRIBiasFieldCorrectionFilter.h"
#include "itkMinimumMaximumImageCalculator.h"
#include "itkBinaryThresholdImageFilter.h"
#include "itkImageRegionIterator.h"
#include "itkZeroCrossingBasedEdgeDetectionImageFilter.h"
#include "itkBinaryBallStructuringElement.h"
#include "itkBinaryCrossStructuringElement.h" 
#include "itkBinaryDilateImageFilter.h"
#include "itkMaskImageFilter.h"
#include "itkCastImageFilter.h"
#include "itkIndent.h"
#include "itkHistogram.h" 
#include "vnl/vnl_math.h"

typedef unsigned char MaskPixelType;
typedef itk::Image<MaskPixelType, 3> MaskType;
typedef MaskType::Pointer MaskPointer;

typedef itk::MRIBiasFieldCorrectionFilter<VolumeITKImage::ImageTypeFloat3, 
                                          VolumeITKImage::ImageTypeFloat3, MaskType> Corrector ;
#endif // HAVE_ITK

/**
 * constructor.
 */
BrainModelVolumeBiasCorrection::BrainModelVolumeBiasCorrection(VolumeFile* vf,
                                                               const float grayWhiteValuesIn[4],
                                                               const float lowerUpperThresholdsIn[2],
                                                               const int   axisIterationsIn[3])
   : BrainModelAlgorithm(NULL)
{
   volumeFile = vf;
   grayWhiteValues[0] = grayWhiteValuesIn[0];
   grayWhiteValues[1] = grayWhiteValuesIn[1];
   grayWhiteValues[2] = grayWhiteValuesIn[2];
   grayWhiteValues[3] = grayWhiteValuesIn[3];
   
   lowerUpperThresholds[0] = lowerUpperThresholdsIn[0];
   lowerUpperThresholds[1] = lowerUpperThresholdsIn[1];
   
   axisIterations[0] = axisIterationsIn[0];
   axisIterations[1] = axisIterationsIn[1];
   axisIterations[2] = axisIterationsIn[2];
}
                               
/**
 * destructor.
 */
BrainModelVolumeBiasCorrection::~BrainModelVolumeBiasCorrection()
{
}

#ifdef HAVE_ITK
void 
BrainModelVolumeBiasCorrection::remove_edges(VolumeITKImage::ImagePointer input, 
                                             VolumeITKImage::ImagePointer& NoEdgeImage)
{
  const int Dimension = 3;
  typedef itk::ZeroCrossingBasedEdgeDetectionImageFilter<VolumeITKImage::ImageTypeFloat3, 
                                          VolumeITKImage::ImageTypeFloat3> zeroCrossFilterType;
  typedef itk::CastImageFilter< VolumeITKImage::ImageTypeFloat3,  MaskType> castFilterType;
  typedef itk::MaskImageFilter< VolumeITKImage::ImageTypeFloat3,MaskType,
                                VolumeITKImage::ImageTypeFloat3 > maskFilterType;
  //typedef itk::BinaryBallStructuringElement<MaskPixelType,Dimension> StructuringElementType;
  typedef itk::BinaryCrossStructuringElement<MaskPixelType,Dimension> StructuringElementType;
  typedef itk::BinaryDilateImageFilter<MaskType, MaskType, StructuringElementType> dilateFilterType;
  typedef itk::BinaryThresholdImageFilter< MaskType,  MaskType> BinaryThresholdType;

  double variance[3];
  variance[0] = 2.5;
  variance[1] = 2.5;
  variance[2] = 2.5;
  
  //std::cout << " edge detection" << std::endl;
  zeroCrossFilterType::Pointer edgeFilter = zeroCrossFilterType::New();
  edgeFilter->SetInput(input);
  edgeFilter->SetVariance(variance);
  edgeFilter->Update();

  castFilterType::Pointer convFilter = castFilterType::New();
  convFilter->SetInput(edgeFilter->GetOutput());
  convFilter->Update();
 
  StructuringElementType structuringElement;
  int ballSize = 1;
  structuringElement.SetRadius( ballSize ); 
  structuringElement.CreateStructuringElement( );
  
  //   //std::cout << "dilate  edge image" << std::endl;
  dilateFilterType::Pointer dilateFilter = dilateFilterType::New();  
  dilateFilter->SetInput(convFilter->GetOutput());
  dilateFilter->SetDilateValue (1);
  dilateFilter->SetKernel( structuringElement );
  dilateFilter->Update();

  const int MaskValue = 1;
  const int NoMaskValue = 0;

  //std::cout << "invert edge image" << std::endl;
  // Not Operation on Image -> select places where there is NO Edge
  BinaryThresholdType::Pointer NotFilter = BinaryThresholdType::New();
  //NotFilter->SetInput(convFilter->GetOutput());
  NotFilter->SetInput(dilateFilter->GetOutput());
  NotFilter->SetOutsideValue(MaskValue);
  NotFilter->SetInsideValue(NoMaskValue);
  NotFilter->SetUpperThreshold(255);
  NotFilter->SetLowerThreshold(1);
  NotFilter->Update();


  //std::cout << "mask with inverted edge image" << std::endl;
  /** masking of the input image with the inverted&dilated edge image */
  maskFilterType::Pointer maskFilter = maskFilterType::New();
  maskFilter->SetInput1(input);
  maskFilter->SetInput2(NotFilter->GetOutput());
  maskFilter->Update();
  
  NoEdgeImage = maskFilter->GetOutput();
}
#endif // HAVE_ITK

#ifdef HAVE_ITK
void
BrainModelVolumeBiasCorrection::biasCorrection(VolumeITKImage::ImagePointer& input,
                                               VolumeITKImage::ImagePointer& output,
                                               const int sliceDirection,
                                               const bool useLog)
{
  // load images
  MaskPointer inputMask ;
  MaskPointer outputMask ;

  typedef itk::BinaryThresholdImageFilter< VolumeITKImage::ImageTypeFloat3,  MaskType> threshFilterType;
  static const int TRESH_VAL = 1;
  static const int BG_VAL = 0;

  int degree = 3;
  std::vector<double> InclassMeans ;
  std::vector<double> InclassSigmas ;
  int volumeMaximumIteration = axisIterations[sliceDirection];  // jwh 200; 
  int interSliceMaximumIteration = 200; // jwh 200; 
  double initialRadius = 1.01;
  double growth = 1.05;
  double shrink = pow(growth, -0.25);

  bool usingSlabIdentification = false;
  bool useIntersliceCorrection = false;

      
   //
   // Bias corrector filter
   //
   Corrector::Pointer filter = Corrector::New() ;
   filter->SetInput(input) ;

     //
     // Create Output Mask
     //
     threshFilterType::Pointer threshFilter1 = threshFilterType::New();
     threshFilter1->SetInput(input);
     threshFilter1->SetUpperThreshold(lowerUpperThresholds[1]); // 32000);
     threshFilter1->SetLowerThreshold(lowerUpperThresholds[0]); // percVal + 1);
     threshFilter1->SetOutsideValue( BG_VAL );
     threshFilter1->SetInsideValue( TRESH_VAL );
     threshFilter1->Update();        
     outputMask = threshFilter1->GetOutput() ;
      
   //
   // Create input mask
   //
   VolumeITKImage::ImagePointer NoEdgeImage ;
   remove_edges(input, NoEdgeImage);
   threshFilterType::Pointer threshFilter = threshFilterType::New();
   threshFilter->SetInput(NoEdgeImage);
   threshFilter->SetUpperThreshold(lowerUpperThresholds[1]); // 32000 );
   threshFilter->SetLowerThreshold(lowerUpperThresholds[0]); // percVal + 1 );
   threshFilter->SetOutsideValue( BG_VAL );
   threshFilter->SetInsideValue( TRESH_VAL );
   threshFilter->Update();
   inputMask = threshFilter->GetOutput() ;
      
   //
   // Set intputs and run the filter
   //
   if (DebugControl::getDebugOn()) {
      filter->DebugOn() ;
   }
   filter->SetOutputMask(outputMask) ;
   filter->SetInputMask(inputMask) ;
   filter->IsBiasFieldMultiplicative(useLog) ;
   filter->SetTissueClassStatistics(classMeans, classSigmas) ;
   filter->SetOptimizerGrowthFactor(growth) ;
   filter->SetOptimizerShrinkFactor(shrink) ;
   filter->SetVolumeCorrectionMaximumIteration(volumeMaximumIteration) ;
   filter->SetInterSliceCorrectionMaximumIteration(interSliceMaximumIteration) ;
   filter->SetOptimizerInitialRadius(initialRadius) ;
   filter->SetBiasFieldDegree(degree) ;
   filter->SetUsingSlabIdentification(usingSlabIdentification) ;
   filter->SetUsingInterSliceIntensityCorrection(useIntersliceCorrection) ;
   filter->SetSlicingDirection(sliceDirection) ;
   filter->SetUsingBiasFieldCorrection(true) ;
   filter->SetGeneratingOutput(true) ;
   filter->Update() ;

   //
   // Get output of filter
   //
   output = filter->GetOutput();

   if (DebugControl::getDebugOn()) {
      std::cout << " coefficients :" ;
      Corrector::BiasFieldType::CoefficientArrayType coefficients = 
          filter->GetEstimatedBiasFieldCoefficients() ;

      Corrector::BiasFieldType::CoefficientArrayType::iterator iter =
          coefficients.begin() ;

      while (iter != coefficients.end()) {
         std::cout << *iter << " " ;
         iter++ ;
      } 
      std::cout << std::endl ;
   }
}
#endif // HAVE_ITK

#ifdef HAVE_ITK
/**
 * Do energy function
 */
void
BrainModelVolumeBiasCorrection::energyFunctions() const
{
   if (DebugControl::getDebugOn()) {
      typedef itk::CompositeValleyFunction EnergyFunction ;
      int interval = 100;
     
      EnergyFunction energy(classMeans, classSigmas) ;
     
      double higher = energy.GetUpperBound() ;
      double lower = energy.GetLowerBound() ;
      long noOfSamples = energy.GetNumberOfSamples() ; 
      double TableInc = (double) ((higher - lower) / 
                                 (noOfSamples - 1));

      std::ofstream fout("energy.txt");
      
      double d = lower;
      std::cout << "intensity\tenergy" << std::endl ;
      int i = 0 ;
      while(i < noOfSamples) {
          std::cout << d << "\t" << energy(d) << std::endl ;
          fout  << d << "," << energy(d) << std::endl ;
          i += interval ;
          d += TableInc * interval ;
      }
      
      fout.close();
   }
}
#endif // HAVE_ITK

#ifdef HAVE_ITK
/**
 * determine means and variance.
 */
void 
BrainModelVolumeBiasCorrection::meansVariance(const bool useLog) 
{
   std::vector<float> grayVoxels, whiteVoxels;
   const int numVoxels = volumeFile->getTotalNumberOfVoxels();
   for (int m = 0; m < numVoxels; m++) {
      const float v = volumeFile->getVoxelWithFlatIndex(m);
      if ((v >= grayWhiteValues[0]) && (v <= grayWhiteValues[1])) {
         grayVoxels.push_back(v);
      }
      if ((v >= grayWhiteValues[2]) && (v <= grayWhiteValues[3])) {
         whiteVoxels.push_back(v);
      }
   }
     
   //
   // Mean and deviation of gray
   //
   StatisticDataGroup sdgGray(&grayVoxels, StatisticDataGroup::DATA_STORAGE_MODE_POINT);
   StatisticMeanAndDeviation smdGray;
   smdGray.addDataGroup(&sdgGray);
   try {
      smdGray.execute();
   }
   catch (StatisticException&) {
   }
   const float grayMean = smdGray.getMean();
   const float grayVar  = smdGray.getStandardDeviation();

   //
   // Mean and deviation of white
   //
   StatisticDataGroup sdgWhite(&whiteVoxels, StatisticDataGroup::DATA_STORAGE_MODE_POINT);
   StatisticMeanAndDeviation smdWhite;
   smdWhite.addDataGroup(&sdgWhite);
   try {
      smdWhite.execute();
   }
   catch (StatisticException&) {
   }
   const float whiteMean = smdWhite.getMean();
   const float whiteVar  = smdWhite.getStandardDeviation();

   if (DebugControl::getDebugOn()) {
      std::cout << "Gray (mean, var): " << grayMean << ", " << grayVar << std::endl;
      std::cout << "White (mean, var): " << whiteMean << ", " << whiteVar << std::endl;
   }
   
   classMeans.SetSize(2);
   classSigmas.SetSize(2);
   classMeans[0] = grayMean;
   classMeans[1] = whiteMean;
   classSigmas[0] = grayVar;
   classSigmas[1] = whiteVar;
   
   if (useLog) { 
      for (unsigned int i = 0 ; i < classMeans.size() ; i++) {
         classSigmas[i] = log(1.0 + classSigmas[i] / (classMeans[i] + 1.0)) ;
         classMeans[i] = log(classMeans[i] + 1.0) ;
      }
   }

}
#endif // HAVE_ITK

/**
 * execute the algorithm.
 */
void 
BrainModelVolumeBiasCorrection::execute() throw (BrainModelAlgorithmException)
{
#ifdef HAVE_ITK
   if (volumeFile == NULL) {
      throw BrainModelAlgorithmException("Input volume is invalid (NULL).");
   }
   
   try {
      const bool useLog = false;
      meansVariance(useLog);
      
      energyFunctions();
      
      VolumeITKImage image;
      volumeFile->convertToITKImage(image);
      
      if (axisIterations[2] > 0) {
         VolumeITKImage imageOut;
         biasCorrection(image.image, imageOut.image, 2, useLog);
         image.image = imageOut.image;
      }
      
      if (axisIterations[0] > 0) {
         VolumeITKImage imageOut;
         biasCorrection(image.image, imageOut.image, 0, useLog);
         image.image = imageOut.image;
      }
      
      if (axisIterations[1] > 0) {
         VolumeITKImage imageOut;
         biasCorrection(image.image, imageOut.image, 1, useLog);
         image.image = imageOut.image;
      }
      
      volumeFile->convertFromITKImage(image);
      volumeFile->stretchVoxelValues();
      
      std::ostringstream str;
      str << "\n"
          << "Bias Correction:\n"
          << "   Iterations (X, Y, Z): "
          << axisIterations[0] << ", " << axisIterations[1] << ", " << axisIterations[2] << "\n"
          << "   Thresholds (Low, High): "
          << lowerUpperThresholds[0] << ", " << lowerUpperThresholds[1] << "\n"
          << "   Gray Range: "
          << grayWhiteValues[0] << ", " << grayWhiteValues[1] << "\n"
          << "   White Range: "
          << grayWhiteValues[2] << ", " << grayWhiteValues[3] << "\n"
          << "\n";
      volumeFile->appendToFileComment(str.str().c_str());
   }
   catch (itk::ExceptionObject& e) {
      throw BrainModelAlgorithmException(e.what());
   }
#else  // HAVE_ITK
   throw FileException("Bias Correction unavailable.  ITK library not available at compile time.");
#endif // HAVE_ITK
}