File: itkScalarImageKmeansImageFilter3DTest.cxx

package info (click to toggle)
insighttoolkit4 4.13.3withdata-dfsg2-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 491,256 kB
  • sloc: cpp: 557,600; ansic: 180,546; fortran: 34,788; python: 16,572; sh: 2,187; lisp: 2,070; tcl: 993; java: 362; perl: 200; makefile: 133; csh: 81; pascal: 69; xml: 19; ruby: 10
file content (343 lines) | stat: -rw-r--r-- 14,561 bytes parent folder | download | duplicates (6)
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
/*=========================================================================
 *
 *  Copyright Insight Software Consortium
 *
 *  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
 *
 *         http://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.
 *
 *=========================================================================*/
#include <iostream>
#include <fstream>

#include "itkImageFileWriter.h"
#include "itkImageFileReader.h"
#include "itkLabelStatisticsImageFilter.h"
#include "itkThresholdImageFilter.h"
#include "itkMaskImageFilter.h"
#include "itkNotImageFilter.h"
#include "itkScalarImageKmeansImageFilter.h"

int itkScalarImageKmeansImageFilter3DTest (int argc, char *argv[])
{

  if( argc < 4 )
    {
    std::cerr << "Usage: " << std::endl;
    std::cerr << argv[0];
    std::cerr << " inputVolume input3DSkullStripVolume outputLabelMapVolume " << std::endl;
    return EXIT_FAILURE;
    }

  std::string inputVolume(argv[1]);
  std::string input3DSkullStripVolume(argv[2]);
  std::string outputLabelMapVolume(argv[3]);
  float numberOfStdDeviations = 10.0;


  bool debug=true;
  if (debug)
    {
    std::cout << "Input T1 Image: " <<  inputVolume << std::endl;
    std::cout << "Input 3DSkullStrip Mask Image: " <<  input3DSkullStripVolume << std::endl;
    std::cout << "Number of Standard Deviations: " <<  numberOfStdDeviations << std::endl;
    std::cout << "Output K-Means LabelMap Image: " <<  outputLabelMapVolume << std::endl;
    }

  bool violated=false;
  if (inputVolume.size() == 0)
    {
    violated = true; std::cout << "  --inputVolume Required! "  << std::endl;
    }
  if (input3DSkullStripVolume.size() == 0)
    {
    violated = true; std::cout << "  --input3DSkullStripVolume Required! "  << std::endl;
    }
  if (outputLabelMapVolume.size() == 0)
    {
    violated = true; std::cout << "  --outputLabelMapVolume Required! "  << std::endl;
    }
  if (violated)
    {
    exit(1);
    }

  typedef signed short       PixelType;
  const unsigned int          Dimension = 3;

  typedef itk::Image<PixelType, Dimension > ImageType;
  typedef itk::ImageFileReader< ImageType > ReaderType;

  ReaderType::Pointer T1Reader = ReaderType::New();
  T1Reader->SetFileName( inputVolume );

  ReaderType::Pointer maskReader = ReaderType::New();
  maskReader->SetFileName( input3DSkullStripVolume );

  const PixelType imageExclusion = -32000;
  const PixelType maskThresholdBelow = 5;     // someday with more generality?

  /* The Threshold Image Filter is used to produce the brain clipping mask from a 3DSkullStrip result image. */
  typedef itk::ThresholdImageFilter< ImageType > ThresholdFilterType;
  ThresholdFilterType::Pointer brainMaskFilter = ThresholdFilterType::New();
  brainMaskFilter->SetInput( maskReader->GetOutput() );
  brainMaskFilter->ThresholdBelow( maskThresholdBelow );
  brainMaskFilter->Update();

  /* The Not Image Filter is used to produce the other clipping mask. */
  typedef itk::NotImageFilter< ImageType, ImageType > NotFilterType;
  NotFilterType::Pointer nonBrainMaskFilter = NotFilterType::New();
  nonBrainMaskFilter->SetInput( maskReader->GetOutput() );
  nonBrainMaskFilter->Update();

  /* The Statistics Image Filter lets us find the initial cluster means.
     Should this be limited to the excluded region of the clipped T1 image?  */
  typedef itk::LabelStatisticsImageFilter< ImageType, ImageType > LabelStatisticsFilterType;
  typedef LabelStatisticsFilterType::RealType StatisticRealType;
  LabelStatisticsFilterType::Pointer statisticsFilter = LabelStatisticsFilterType::New();
  statisticsFilter->SetInput( T1Reader->GetOutput() );
  statisticsFilter->SetLabelInput( maskReader->GetOutput() );
  statisticsFilter->Update();

  const PixelType imageMin = static_cast<PixelType> ( statisticsFilter->GetMinimum(6) );
  const PixelType imageMax = static_cast<PixelType> ( statisticsFilter->GetMaximum(6) );
  const StatisticRealType imageMean = statisticsFilter->GetMean(6);
  const StatisticRealType imageSigma = statisticsFilter->GetSigma(6);

  std::cout << "Brain Minimum == " << imageMin << std::endl;
  std::cout << "Brain Maximum == " << imageMax << std::endl;
  std::cout << "Brain Mean == " << imageMean << std::endl;
  std::cout << "Brain Sigma == " << imageSigma << std::endl;


  /* The Statistics Image Filter lets us find the initial cluster means.
     Should this be limited to the excluded region of the clipped T1 image?  */
  LabelStatisticsFilterType::Pointer nonBrainStatisticsFilter = LabelStatisticsFilterType::New();
  nonBrainStatisticsFilter->SetInput( T1Reader->GetOutput() );
  nonBrainStatisticsFilter->SetLabelInput( nonBrainMaskFilter->GetOutput() );
  nonBrainStatisticsFilter->Update();

  const PixelType nonBrainImageMin = static_cast<PixelType> ( nonBrainStatisticsFilter->GetMinimum(1) );
  const PixelType nonBrainImageMax = static_cast<PixelType> ( nonBrainStatisticsFilter->GetMaximum(1) );
  const StatisticRealType nonBrainImageMean = nonBrainStatisticsFilter->GetMean(1);
  const StatisticRealType nonBrainImageSigma = nonBrainStatisticsFilter->GetSigma(1);

  //std::cout << "Background Minimum == " << nonBrainImageMin << std::endl;
  std::cout << "Background Maximum == " << nonBrainImageMax << std::endl;
  std::cout << "Background Minimum == " << nonBrainImageMin << std::endl;
  std::cout << "Background Mean == " << nonBrainImageMean << std::endl;
  std::cout << "Background Sigma == " << nonBrainImageSigma << std::endl;


  /* The Mask Image Filter applies the clipping mask by stepping
     on the excluded region with the imageExclusion value. */
  typedef itk::MaskImageFilter< ImageType, ImageType > MaskFilterType;
  MaskFilterType::Pointer clippedBrainT1Filter = MaskFilterType::New();
  clippedBrainT1Filter->SetInput1( T1Reader->GetOutput() );
  clippedBrainT1Filter->SetInput2( brainMaskFilter->GetOutput() );
  clippedBrainT1Filter->SetOutsideValue( imageExclusion );
  std::cout << "clippedBrainT1Filter->Update " << std::endl;
  clippedBrainT1Filter->Update();

  ImageType::Pointer clippedBrainT1Pointer;

  if (numberOfStdDeviations > 0.0 )
    {
    ThresholdFilterType::Pointer clipArterialBloodFilter = ThresholdFilterType::New();
    clipArterialBloodFilter->SetInput( clippedBrainT1Filter->GetOutput() );
    clipArterialBloodFilter->ThresholdAbove( static_cast<PixelType>(imageMean+numberOfStdDeviations*imageSigma ));
    clipArterialBloodFilter->SetOutsideValue( imageExclusion );
    std::cout << "clipArterialBloodFilter->Update " << std::endl;
    clipArterialBloodFilter->Update();
    clippedBrainT1Pointer = clipArterialBloodFilter->GetOutput();
    }
  else
    {
    clippedBrainT1Pointer = clippedBrainT1Filter->GetOutput();
    }

  /* The Mask Image Filter applies the clipping mask by stepping
     on the excluded region with the imageExclusion value. */
  MaskFilterType::Pointer clippedNonBrainT1Filter = MaskFilterType::New();
  clippedNonBrainT1Filter->SetInput1( T1Reader->GetOutput() );
  clippedNonBrainT1Filter->SetInput2( nonBrainMaskFilter->GetOutput() );
  clippedNonBrainT1Filter->SetOutsideValue( imageExclusion );
  std::cout << "clippedNonBrainT1Filter->Update " << std::endl;
  clippedNonBrainT1Filter->Update();

  /* The Scalar Image Kmeans Image Filter will find a code image in 3 classes
     for the interior of the mask, plus a code for the exterior of the mask. */
  typedef itk::ScalarImageKmeansImageFilter< ImageType > KMeansFilterType;
  typedef KMeansFilterType::RealPixelType                RealPixelType;
  KMeansFilterType::Pointer kmeansFilter = KMeansFilterType::New();
  kmeansFilter->SetInput( clippedBrainT1Pointer );

  const unsigned int useNonContiguousLabels = 1;

  RealPixelType backgroundInitialMean = imageExclusion;
  //  RealPixelType bloodInitialMean = imageMax;    // ARTERIAL blood.
  const RealPixelType csfInitialMean = imageMin;
  const RealPixelType whiteInitialMean = imageMean + imageSigma;
  const RealPixelType grayInitialMean = imageMean - imageSigma;

  std::cout << "kmeansFilter InitialMeans  " << backgroundInitialMean << ";  " << csfInitialMean << ";  " << grayInitialMean << ";  " << whiteInitialMean << std::endl;
  kmeansFilter->AddClassWithInitialMean( backgroundInitialMean );
  kmeansFilter->AddClassWithInitialMean( csfInitialMean );
  kmeansFilter->AddClassWithInitialMean( grayInitialMean );
  kmeansFilter->AddClassWithInitialMean( whiteInitialMean );
  //kmeansFilter->AddClassWithInitialMean( bloodInitialMean );

  kmeansFilter->SetUseNonContiguousLabels( useNonContiguousLabels );

  std::cout << "kmeansFilter->Update [[Watch out for infinite loop here!]] " << std::endl;
  try
    {
    kmeansFilter->Update();
    }
  catch( itk::ExceptionObject & excp )
    {
    std::cerr << "Problem encountered while running K-means segmentation ";
    std::cerr << excp << std::endl;
    return EXIT_FAILURE;
    }

  KMeansFilterType::ParametersType estimatedMeans =
                                            kmeansFilter->GetFinalMeans();

  unsigned int numberOfClasses = estimatedMeans.Size();

  for ( unsigned int i = 0; i < numberOfClasses; ++i )
    {
    std::cout << "Brain cluster[" << i << "] ";
    std::cout << "    estimated mean : " << estimatedMeans[i] << std::endl;
    }


  /* The Scalar Image Kmeans Image Filter will find a code image in 3 classes
     for the interior of the mask, plus a code for the exterior of the mask. */
  KMeansFilterType::Pointer kmeansNonBrainFilter = KMeansFilterType::New();
  kmeansNonBrainFilter->SetInput( clippedNonBrainT1Filter->GetOutput() );

  backgroundInitialMean = imageExclusion;
  const RealPixelType airInitialMean = imageMin;
  const RealPixelType fatInitialMean = imageMax;
  const RealPixelType muscleInitialMean = imageMean;
   // Why are these the brain region versions, and not the background region versions?  Seems to work, though.

  std::cout << "kmeansNonBrainFilter InitialMeans  " << backgroundInitialMean << ";  " << airInitialMean << ";  " << muscleInitialMean << ";  " << fatInitialMean << std::endl;
  kmeansNonBrainFilter->AddClassWithInitialMean( backgroundInitialMean );
  kmeansNonBrainFilter->AddClassWithInitialMean( airInitialMean );
  kmeansNonBrainFilter->AddClassWithInitialMean( muscleInitialMean );
  kmeansNonBrainFilter->AddClassWithInitialMean( fatInitialMean );
  kmeansNonBrainFilter->SetUseNonContiguousLabels( useNonContiguousLabels );

  std::cout << "kmeansNonBrainFilter->Update " << std::endl;
  try
    {
    kmeansNonBrainFilter->Update();
    }
  catch( itk::ExceptionObject & excp )
    {
    std::cerr << "Problem encountered while Background K-Means segmentation ";
    std::cerr << excp << std::endl;
    return EXIT_FAILURE;
    }

  estimatedMeans = kmeansNonBrainFilter->GetFinalMeans();

  numberOfClasses = estimatedMeans.Size();

  for ( unsigned int i = 0; i < numberOfClasses; ++i )
    {
    std::cout << "Background cluster[" << i << "] ";
    std::cout << "    estimated mean : " << estimatedMeans[i] << std::endl;
    }

  /* Now remap the labels - background first followed by brain */
  typedef KMeansFilterType::OutputImageType LabelImageType;
  LabelImageType::Pointer kmeansLabelImage = LabelImageType::New();
  kmeansLabelImage->SetRegions( maskReader->GetOutput()->GetLargestPossibleRegion() );
  kmeansLabelImage->SetSpacing( maskReader->GetOutput()->GetSpacing() );
  kmeansLabelImage->SetDirection( maskReader->GetOutput()->GetDirection() );
  kmeansLabelImage->SetOrigin( maskReader->GetOutput()->GetOrigin() );
  kmeansLabelImage->Allocate(true);

  typedef itk::LabelStatisticsImageFilter< LabelImageType, LabelImageType > LabelMapStatisticsFilterType;
  LabelMapStatisticsFilterType::Pointer statisticsNonBrainFilter = LabelMapStatisticsFilterType::New();
  statisticsNonBrainFilter->SetInput( kmeansNonBrainFilter->GetOutput() );
  statisticsNonBrainFilter->SetLabelInput( kmeansNonBrainFilter->GetOutput() );
  std::cout << "statisticsNonBrainFilter->Update " << std::endl;
  statisticsNonBrainFilter->Update();

  /* Background Tissues are Lower Label values */
  unsigned char currentLabel = 0;
  for (unsigned int i=1; i<256; i++)
    {
    if ( statisticsNonBrainFilter->HasLabel( static_cast<unsigned char> ( i ) ) )
      {
      currentLabel++;
      LabelImageType::RegionType labelRegion = statisticsNonBrainFilter->GetRegion( static_cast<unsigned char> ( i ) );
      itk::ImageRegionIterator<LabelImageType> it( kmeansNonBrainFilter->GetOutput(), labelRegion );

      it.GoToBegin();
      while( !it.IsAtEnd() )
        {
        if ( it.Get() == static_cast<unsigned char> ( i ) )
          {
          // Set Output Image
          kmeansLabelImage->SetPixel(it.GetIndex(), currentLabel);
          }
        ++it;
        }
      }
    }

  /* Brain Tissues are Higher Label values */
  LabelMapStatisticsFilterType::Pointer statisticsBrainFilter = LabelMapStatisticsFilterType::New();
  statisticsBrainFilter->SetInput( kmeansFilter->GetOutput() );
  statisticsBrainFilter->SetLabelInput( kmeansFilter->GetOutput() );
  std::cout << "statisticsBrainFilter->Update " << std::endl;
  statisticsBrainFilter->Update();

  for (unsigned int i=1; i<256; i++)
    {
    if ( statisticsBrainFilter->HasLabel( static_cast<unsigned char> ( i ) ) )
      {
      currentLabel++;
      LabelImageType::RegionType labelRegion = statisticsBrainFilter->GetRegion( static_cast<unsigned char> ( i ) );
      itk::ImageRegionIterator<LabelImageType> it( kmeansFilter->GetOutput(), labelRegion );

      it.GoToBegin();
      while( !it.IsAtEnd() )
        {
        if ( it.Get() == static_cast<unsigned char> ( i ) )
          {
          // Set Output Image
          kmeansLabelImage->SetPixel(it.GetIndex(), currentLabel);
          }
        ++it;
        }
      }
    }

  /* Write out the resulting Label Image */
  typedef itk::ImageFileWriter<LabelImageType> WriterType;
  WriterType::Pointer labelWriter = WriterType::New();
  labelWriter->SetInput( kmeansLabelImage );
  labelWriter->SetFileName( outputLabelMapVolume );
  std::cout << "labelWriter->Update " << std::endl;
  labelWriter->Update( );


  return EXIT_SUCCESS;


}