File: itkObjectMorphologyImageFilterTest.cxx

package info (click to toggle)
insighttoolkit 3.6.0-3
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 94,956 kB
  • ctags: 74,981
  • sloc: cpp: 355,621; ansic: 195,070; fortran: 28,713; python: 3,802; tcl: 1,996; sh: 1,175; java: 583; makefile: 415; csh: 184; perl: 175
file content (345 lines) | stat: -rw-r--r-- 9,945 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
/*=========================================================================

  Program:   Insight Segmentation & Registration Toolkit
  Module:    $RCSfile: itkObjectMorphologyImageFilterTest.cxx,v $
  Language:  C++
  Date:      $Date: 2007-08-10 14:34:02 $
  Version:   $Revision: 1.10 $

  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.

=========================================================================*/
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif

#include <stdlib.h>
#include <time.h>

#include <itkImage.h>
#include <itkIndex.h>
#include "itkDilateObjectMorphologyImageFilter.h"
#include "itkErodeObjectMorphologyImageFilter.h"
#include "itkBinaryErodeImageFilter.h"
#include "itkBinaryDilateImageFilter.h"
#include <itkBinaryBallStructuringElement.h>
#include <itkImageRegionIterator.h>
#include <itkExceptionObject.h>

int itkObjectMorphologyImageFilterTest(int, char* [] ) 
{
  // Define the dimension of the images
  const unsigned int myDimension = 3;

  // Define the values of the input images
  const unsigned short fgValue = 1;
  const unsigned short bgValue = 0;

  // Declare the types of the images
  typedef itk::Image<unsigned short, myDimension>  myImageType;

  // Declare the type of the index to access images
  typedef itk::Index<myDimension>         myIndexType;

  // Declare the type of the size 
  typedef itk::Size<myDimension>          mySizeType;

  // Declare the type of the Region
  typedef itk::ImageRegion<myDimension>        myRegionType;

  // Create an image
  myImageType::Pointer inputImage  = myImageType::New();
  
  // Define their size, and start index
  mySizeType size;
  size[0] = 20;
  size[1] = 20;
  size[2] = 20;

  myIndexType index;
  index[0] = 0;
  index[1] = 0;
  index[2] = 0;

  myRegionType region;
  region.SetIndex( index );
  region.SetSize( size );

  // Initialize Image
  inputImage->SetRegions( region );
  inputImage->Allocate();

  // Declare Iterator types apropriated for each image 
  typedef itk::ImageRegionIterator<myImageType>  myIteratorType;

  // Initialize the content of Image
  inputImage->FillBuffer(bgValue);

  myImageType::IndexType ind;
  ind[0] = 10;
  ind[1] = 10;
  ind[2] = 10;
  inputImage->SetPixel(ind, fgValue);
  ind[0] = 2;
  ind[1] = 2;
  ind[2] = 8;
  inputImage->SetPixel(ind, fgValue);

  ind[0] = 9;
  ind[1] = 10;
  ind[2] = 5;
  inputImage->SetPixel(ind, fgValue);

  ind[0] = 9;
  ind[1] = 0;
  ind[2] = 15;
  inputImage->SetPixel(ind, fgValue);

  ind[0] = 9;
  ind[1] = 9;
  ind[2] = 7;
  inputImage->SetPixel(ind, fgValue);

  ind[0] = 0;
  ind[1] = 4;
  ind[2] = 17;
  inputImage->SetPixel(ind, fgValue);

  // Declare the type for the structuring element
  typedef itk::BinaryBallStructuringElement<unsigned short, myDimension>
    myKernelType;
  
  // Declare the type for the morphology Filter
  typedef itk::DilateObjectMorphologyImageFilter<myImageType, myImageType,
                                                 myKernelType>
    myDilateFilterType;
  typedef itk::BinaryDilateImageFilter<myImageType, myImageType,
                                                 myKernelType>
    binDilateFilterType;


  typedef itk::ErodeObjectMorphologyImageFilter<myImageType, myImageType,
                                                 myKernelType>
    myErodeFilterType;

  typedef itk::BinaryErodeImageFilter<myImageType, myImageType,
                                                 myKernelType>
    binErodeFilterType;

  // Create the filter
  myDilateFilterType::Pointer dilateFilter = myDilateFilterType::New();
  myErodeFilterType::Pointer erodeFilter = myErodeFilterType::New();
  binDilateFilterType::Pointer binDilateFilter = binDilateFilterType::New();
  binErodeFilterType::Pointer binErodeFilter = binErodeFilterType::New();

  // Create the structuring element
  myKernelType ball;
  myKernelType::SizeType ballSize;
  ballSize[0] = 5;
  ballSize[1] = 4;
  ballSize[2] = 3;
  ball.SetRadius(ballSize);
  ball.CreateStructuringElement();
  
  // Connect the input image
  dilateFilter->SetInput( inputImage );
  dilateFilter->SetKernel( ball );
  dilateFilter->SetObjectValue( fgValue );
  myImageType::Pointer outputImage = dilateFilter->GetOutput();

  clock_t start, end;
  double elapsedTime;

  // Execute the filter
  try
    {
    std::cout << "Object Dilate..." << std::endl;
    start = clock();
    dilateFilter->Update();
    end = clock();

    elapsedTime = (end - start) / (double) CLOCKS_PER_SEC;

    //  Print the content of the result image
    std::cout << "  Success: " << std::endl;
    std::cout << "    Time = " << elapsedTime << std::endl;
    }
  catch (itk::ExceptionObject& e)
    {
    std::cerr << "Exception caught during dilate filter Update\n"  << e;
    return -1;
    }

  binDilateFilter->SetInput( inputImage );
  binDilateFilter->SetKernel( ball );
  binDilateFilter->SetDilateValue( fgValue );
  myImageType::Pointer outputBinImage = binDilateFilter->GetOutput();
  try
    {
    std::cout << "Binary Dilate..." << std::endl;

    start = clock();
    binDilateFilter->Update();
    end = clock();

    elapsedTime = (end - start) / (double) CLOCKS_PER_SEC;

    //  Print the content of the result image
    std::cout << "  Success: " << std::endl;
    std::cout << "    Time = " << elapsedTime << std::endl;
    }
  catch (itk::ExceptionObject& e)
    {
    std::cerr << "Exception caught during dilate filter Update\n"  << e;
    return -1;
    }

  // Create an iterator for going through the image output
  myIteratorType itObj(outputImage, outputImage->GetBufferedRegion());
  myIteratorType itBin(outputBinImage, outputBinImage->GetBufferedRegion());
  std::cout << "Test for Dilate equality..." << std::endl;
  start = clock();
  itObj.GoToBegin();
  itBin.GoToBegin();
  int count = 0;
  while( !itObj.IsAtEnd() && !itBin.IsAtEnd() ) 
    {
    if(itObj.Get() != itBin.Get())
      {
      std::cerr << "Error: Dilated images differ!" << std::endl;
      std::cerr << "   Slice = " << count/(size[1]*size[0]) << std::endl;
      unsigned int x, y;
      itk::Index<3> i;
      i[2] = count/(size[1]*size[0]);
      for(y=0; y<size[1]; y++)
        {
        i[1] = y;
        for(x=0; x<size[0]; x++)
          {
          i[0] = x;
          std::cerr << outputImage->GetPixel(i)
                    << outputBinImage->GetPixel(i) << " ";
          }
        std::cerr << std::endl;
        }
      return -1;
      }
    ++itObj;
    ++itBin;
    ++count;
    }
  end = clock();
  elapsedTime = (end - start) / (double) CLOCKS_PER_SEC;
  std::cout << "  Success: " << std::endl;
  std::cout << "    Time = " << elapsedTime << std::endl;
   
  ballSize[0] = 2;
  ballSize[1] = 2;
  ballSize[2] = 2;
  ball.SetRadius(ballSize);
  ball.CreateStructuringElement();
  
  // Connect the input image
  erodeFilter->SetInput( outputImage );
  erodeFilter->SetKernel( ball );
  erodeFilter->SetObjectValue( fgValue );
  erodeFilter->SetBackgroundValue( bgValue );
  myImageType::Pointer output2Image = erodeFilter->GetOutput();

  // Execute the filter
  try
    {
    std::cout << "Object Erode..." << std::endl;
    start = clock();
    erodeFilter->Update();
    end = clock();

    elapsedTime = (end - start) / (double) CLOCKS_PER_SEC;

    //  Print the content of the result image
    std::cout << "  Success: " << std::endl;
    std::cout << "    Time = " << elapsedTime << std::endl;
    }
  catch (itk::ExceptionObject& e)
    {
    std::cerr << "Exception caught during erode filter Update\n"  << e;
    return -1;
    }

  binErodeFilter->SetInput( outputImage );
  binErodeFilter->SetKernel( ball );
  binErodeFilter->SetErodeValue( fgValue );
  myImageType::Pointer outputBin2Image = binErodeFilter->GetOutput();

  // Execute the filter
  try
    {
    std::cout << "Binary Erode..." << std::endl;
    start = clock();
    binErodeFilter->Update();
    end = clock();

    elapsedTime = (end - start) / (double) CLOCKS_PER_SEC;

    //  Print the content of the result image
    std::cout << "  Success: " << std::endl;
    std::cout << "    Time = " << elapsedTime << std::endl;
    }
  catch (itk::ExceptionObject& e)
    {
    std::cerr << "Exception caught during erode filter Update\n"  << e;
    return -1;
    }

  // Create an iterator for going through the image output
  myIteratorType it2Obj(output2Image, output2Image->GetBufferedRegion());
  myIteratorType it2Bin(outputBin2Image, outputBin2Image->GetBufferedRegion());
  std::cout << "Test for Erode equality..." << std::endl;
  start = clock();
  count = 0;
  while( !it2Obj.IsAtEnd() ) 
    {
    if(it2Obj.Get() != it2Bin.Get())
      {
      std::cout << "As expected: Error: Eroded images differ!" << std::endl;
      std::cout << "  Please see documentation - ErodeObject and BinaryErode";
      std::cout << std::endl << "    produce different results" << std::endl;
      std::cout << "   Slice = " << count/(size[1]*size[0]) << std::endl;
      unsigned int x, y;
      itk::Index<3> i;
      i[2] = count/(size[1]*size[0]);
      for(y=0; y<size[1]; y++)
        {
        i[1] = y;
        for(x=0; x<size[0]; x++)
          {
          i[0] = x;
          std::cout << output2Image->GetPixel(i)
                    << outputBin2Image->GetPixel(i) << " ";
          }
        std::cout << std::endl;
        }
      break;
      }
    ++it2Obj;
    ++it2Bin;
    ++count;
    }
  end = clock();
  elapsedTime = (end - start) / (double) CLOCKS_PER_SEC;
  std::cout << "  Success: " << std::endl;
  std::cout << "    Time = " << elapsedTime << std::endl;

  // All objects should be automatically destroyed at this point
  return EXIT_SUCCESS;

}