File: itkScalarImageToCooccurrenceMatrixFilterTest.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 (422 lines) | stat: -rw-r--r-- 12,849 bytes parent folder | download | duplicates (5)
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
/*=========================================================================
 *
 *  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.
 *
 *=========================================================================*/

// Insight classes
#include "itkImageRegionIterator.h"
#include "itkMath.h"

#include "itkScalarImageToCooccurrenceMatrixFilter.h"

int itkScalarImageToCooccurrenceMatrixFilterTest(int, char* [] )
{

  //Data definitions
  const unsigned int  IMGWIDTH         =  5;
  const unsigned int  IMGHEIGHT        =  5;
  const unsigned int  NDIMENSION       =  2;


  //------------------------------------------------------
  //Create a simple test images
  //------------------------------------------------------
  typedef itk::Image<unsigned char, NDIMENSION> InputImageType;

  typedef itk::ImageRegionIterator< InputImageType > InputImageIterator;


  InputImageType::Pointer image = InputImageType::New();

  InputImageType::SizeType inputImageSize = {{ IMGWIDTH, IMGHEIGHT }};

  InputImageType::IndexType index;
  index.Fill(0);
  InputImageType::RegionType region;

  region.SetSize( inputImageSize );
  region.SetIndex( index );

  //--------------------------------------------------------------------------
  // Set up the image first. It looks like:
  //  1 2 1 2 1
  //  1 2 1 2 1
  //  1 2 1 2 1
  //  1 2 1 2 1
  //  1 2 1 2 1
  //--------------------------------------------------------------------------

  image->SetRegions( region );
  image->Allocate();

  // setup the iterator
  InputImageIterator imageIt( image, image->GetBufferedRegion() );

  imageIt.GoToBegin();

  for(unsigned int i = 0; i < 5; i++)
    {
    for(unsigned int j = 0; j < 5; j++, ++imageIt)
      {
      imageIt.Set(j % 2 + 1);
      }
    }


  //--------------------------------------------------------------------------
  // Generate the histogram. The un-normalized histogram should look like this:
  //
  //     0 1  2 ...
  //     ------
  //  0 |0 0  0
  //  1 |0 24 20
  //  2 |0 20 16
  //  3 |0 0  0
  //  .
  //  .
  // with zeroes elsewhere.
  //--------------------------------------------------------------------------

  bool passed = true;

  try
    {

    typedef itk::Statistics::ScalarImageToCooccurrenceMatrixFilter< InputImageType> FilterType;

    FilterType::Pointer filter = FilterType::New();

    //Invoke update before adding an input. An exception should be
    //thrown.
    try
      {
      filter->Update();
      passed = false;
      std::cerr << "Failed to throw expected exception due to ITK_NULLPTR input: " << std::endl;
      return EXIT_FAILURE;
      }
    catch ( itk::ExceptionObject & excp )
      {
      std::cout << "Expected exception caught: " << excp << std::endl;
      }

    filter->ResetPipeline();

    if ( filter->GetInput() != ITK_NULLPTR )
      {
      std::cerr << "GetInput() should return ITK_NULLPTR since the input is\
                    not set yet " << std::endl;
      passed = false;
      }

    if ( filter->GetMaskImage() != ITK_NULLPTR )
      {
      std::cerr << "GetMaskImage() should return ITK_NULLPTR since the mask image is\
                    not set yet " << std::endl;
      passed = false;
      }


    //Invoke update with a ITK_NULLPTR input. An exception should be
    //thrown.
    filter->SetInput( ITK_NULLPTR );
    try
      {
      filter->Update();
      passed = false;
      std::cerr << "Failed to throw expected exception due to ITK_NULLPTR input: " << std::endl;
      return EXIT_FAILURE;
      }
    catch ( itk::ExceptionObject & excp )
      {
      std::cout << "Expected exception caught: " << excp << std::endl;
      }

    filter->ResetPipeline();

    if ( filter->GetInput() != ITK_NULLPTR )
      {
      passed = false;
      }


    filter->SetInput(image);

    InputImageType::OffsetType offset1 = {{0, 1}};
    InputImageType::OffsetType offset2 = {{1, 0}};
    FilterType::OffsetVectorPointer offsetV =
    FilterType::OffsetVector::New();
    offsetV->push_back(offset1);
    offsetV->push_back(offset2);

    filter->SetOffsets(offsetV);


    filter->Update();


    const FilterType::HistogramType * hist = filter->GetOutput();

    //--------------------------------------------------------------------------
    // Test the histogram.
    //--------------------------------------------------------------------------

    // First make sure the bins are sized properly:

    float max = hist->GetBinMax(0,255);
    float min = hist->GetBinMin(0,255);

    if(itk::Math::NotAlmostEquals(max, 256)
    || itk::Math::NotAlmostEquals(min, 255))
      {
      std::cerr << "Error" << std::endl;
      std::cerr << "The calculated bin sizes are incorrect" << std::endl;
      std::cerr << "Expected [255, 256), got [" << min << ", " << max << ")" << std::endl << std::endl;
      passed = false;
      }

    // Now make sure the contents of the bins are correct:
    typedef FilterType::HistogramType::IndexType IndexType;
    IndexType one_one( hist->GetMeasurementVectorSize() );
    IndexType one_two( hist->GetMeasurementVectorSize() );
    IndexType two_one( hist->GetMeasurementVectorSize() );
    IndexType two_two( hist->GetMeasurementVectorSize() );

    one_one[0] = 1;
    one_one[1] = 1;

    one_two[0] = 1;
    one_two[1] = 2;

    two_one[0] = 2;
    two_one[1] = 1;

    two_two[0] = 2;
    two_two[1] = 2;

    float ooF, otF, toF, ttF, totalF;
    ooF = hist->GetFrequency(one_one);
    otF = hist->GetFrequency(one_two);
    toF = hist->GetFrequency(two_one);
    ttF = hist->GetFrequency(two_two);
    totalF = hist->GetTotalFrequency();

    if( itk::Math::NotAlmostEquals(ooF, 24.0f)
     || itk::Math::NotAlmostEquals(ttF, 16.0f)
     || itk::Math::NotAlmostEquals(otF, 20.0f)
     || itk::Math::NotAlmostEquals(toF, 20.0f)
     || itk::Math::NotAlmostEquals(ooF + ttF + otF + toF, totalF) )
      {
      std::cerr << "Error:" << std::endl;
      std::cerr << "The histogram was calculated incorrectly" << std::endl;
      std::cerr << "Expected 24, 16, 20, 20, 80 got " << ooF << ", " << ttF  << ", " <<
      otF  << ", " << toF  << ", " << totalF << std::endl << std::endl;
      passed = false;
      }

    //--------------------------------------------------------------------------
    // Test the histogram with normalization
    //--------------------------------------------------------------------------


    FilterType::Pointer filter0 = FilterType::New();

    filter0->SetInput(image);
    filter0->SetOffsets(offsetV);
    filter0->NormalizeOn();

    if ( filter0->GetNormalize() != true )
      {
      std::cerr << "Normalize boolean is not set correctly";
      passed = false;
      }

    filter0->Update();
    const FilterType::HistogramType * hist0 = filter0->GetOutput();

    ooF = hist0->GetFrequency(one_one);
    otF = hist0->GetFrequency(one_two);
    toF = hist0->GetFrequency(two_one);
    ttF = hist0->GetFrequency(two_two);

    if( (ooF - 24/80.) > 0.001 || (ttF - 16/80.) > 0.001 || (otF - 20/80.) > 0.001 ||
        (toF - 20/80.) > 0.001 || (ooF + ttF + otF + toF - 1) > 0.001 )
      {
      std::cerr << "Error:" << std::endl;
      std::cerr << "The histogram was calculated incorrectly" << std::endl;
      std::cerr << "Expected 0.3, 0.2, 0.25, 0.25 got " << ooF << ", " << ttF  << ", " <<
      otF  << ", " << toF << std::endl << std::endl;
      passed = false;
      }


    //--------------------------------------------------------------------------
    // Generate some variant histograms and test them
    //--------------------------------------------------------------------------

    // First a histogram with 2 bins per axis
    FilterType::Pointer filter2 = FilterType::New();

    filter2->SetInput(image);
    InputImageType::OffsetType offset3 = {{0, 1}};

    filter2->SetOffset(offset3);
    filter2->SetNumberOfBinsPerAxis( 2 );

    if ( filter2->GetNumberOfBinsPerAxis() != 2 )
      {
      std::cerr << "GetNumberOfBinsPerAxis() is not returning the expected value" << std::endl;
      passed = false;
      }

    filter2->Update();
    const FilterType::HistogramType * hist2 = filter2->GetOutput();

    IndexType zero_zero( hist2->GetMeasurementVectorSize() );
    zero_zero[0] = 0;
    zero_zero[1] = 0;

    float zzF;
    zzF = hist2->GetFrequency(zero_zero);
    totalF = hist2->GetTotalFrequency();

    if( itk::Math::NotAlmostEquals(zzF, 40.0f) || itk::Math::NotAlmostEquals(zzF, totalF))
      {
      std::cerr << "Error:" << std::endl;
      std::cerr << "The degenerate histogram was calculated incorrectly" << std::endl;
      std::cerr << "Expected 40, 40 got " << zzF  << ", " << totalF << std::endl << std::endl;
      passed = false;
      }


    // Next a histogram with a smaller range.
    FilterType::Pointer filter3 = FilterType::New();

    filter3->SetInput(image);
    InputImageType::OffsetType offset4 = {{1, 1}};

    filter3->SetOffset(offset4);

    filter3->SetPixelValueMinMax(1, 2);

    if ( filter3->GetMin() != 1 )
      {
      std::cerr << "Error: " << std::endl;
      std::cerr << "GetMin() is not returning the value that is expected: 1" << std::endl;
      passed = false;
      }

    if ( filter3->GetMax() != 2 )
      {
      std::cerr << "Error: " << std::endl;
      std::cerr << "GetMin() is not returning the value that is expected: 2" << std::endl;
      passed = false;
      }


    filter3->SetNumberOfBinsPerAxis( 2 );

    filter3->Update();
    const FilterType::HistogramType * hist3 = filter3->GetOutput();

    IndexType zero_one( hist3->GetMeasurementVectorSize() );
    IndexType one_zero( hist3->GetMeasurementVectorSize() );

    zero_one[0] = 0;
    zero_one[1] = 1;

    one_zero[0] = 1;
    one_zero[1] = 0;

    float zoF, ozF;
    zzF = hist3->GetFrequency(zero_zero);
    zoF = hist3->GetFrequency(zero_one);
    ozF = hist3->GetFrequency(one_zero);
    ooF = hist3->GetFrequency(one_one);
    totalF = hist3->GetTotalFrequency();

    if( itk::Math::NotAlmostEquals(zzF, 0.0f)
     || itk::Math::NotAlmostEquals(zoF, 16.0f)
     || itk::Math::NotAlmostEquals(ozF, 16.0f)
     || itk::Math::NotAlmostEquals(ooF, 0.0f)
     || itk::Math::NotAlmostEquals(zzF + zoF + ozF + ooF, totalF) )
      {
      std::cerr << "Error:" << std::endl;
      std::cerr << "The small size histogram was calculated incorrectly" << std::endl;
      std::cerr << "Expected 0, 16, 16, 0, 32 got " << zzF << ", " << zoF  << ", " <<
      ozF  << ", " << ooF  << ", " << totalF << std::endl << std::endl;
      passed = false;
      }

    // Next a histogram with a truncated range.
    FilterType::Pointer filter4 = FilterType::New();

    filter4->SetInput(image);
    filter4->SetOffsets(offsetV);

    if ( filter4->GetOffsets() != offsetV )
      {
      std::cerr << "Error: " << std::endl;
      std::cerr << "GetOffsets() is not returning the expected offset vector: " << std::endl;
      passed = false;
      }

    filter4->SetPixelValueMinMax(0, 1);
    filter4->SetNumberOfBinsPerAxis( 2 );

    filter4->Update();
    const FilterType::HistogramType * hist4 = filter4->GetOutput();

    zzF = hist4->GetFrequency(zero_zero);
    zoF = hist4->GetFrequency(zero_one);
    ozF = hist4->GetFrequency(one_zero);
    ooF = hist4->GetFrequency(one_one);
    totalF = hist4->GetTotalFrequency();

    if( itk::Math::NotAlmostEquals(zzF, 0.0f)
     || itk::Math::NotAlmostEquals(zoF, 0.0f)
     || itk::Math::NotAlmostEquals(ozF, 0.0f)
     || itk::Math::NotAlmostEquals(ooF, 24.0f)
     || itk::Math::NotAlmostEquals(zzF + zoF + ozF + ooF, totalF) )
      {
      std::cerr << "Error:" << std::endl;
      std::cerr << "The truncated range histogram was calculated incorrectly" << std::endl;
      std::cerr << "Expected 0, 0, 0, 24, 24 got " << zzF << ", " << zoF  << ", " <<
      ozF  << ", " << ooF  << ", " << totalF << std::endl << std::endl;
      passed = false;
      }


    if (!passed)
      {
      std::cerr << "Test failed" << std::endl;
      return EXIT_FAILURE;
      }
    else
      {
      std::cerr << "Test succeeded" << std::endl;
      return EXIT_SUCCESS;
      }

    }
  catch( itk::ExceptionObject & err )
    {
    std::cerr << "ExceptionObject caught !" << std::endl;
    std::cerr << err << std::endl;
    std::cerr << "Test failed" << std::endl;
    return EXIT_FAILURE;
    }
}