1 /*=========================================================================
2 *
3 * Copyright Insight Software Consortium
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0.txt
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 *=========================================================================*/
18
19 #include <iostream>
20
21 #include "itkImageRandomIteratorWithIndex.h"
22 #include "itkImageFileWriter.h"
23 #include "itkImageFileReader.h"
24 #include "itkTestingComparisonImageFilter.h"
25
26
itkImageRandomIteratorTest2(int argc,char * argv[])27 int itkImageRandomIteratorTest2( int argc, char * argv [] )
28 {
29 if( argc < 2 )
30 {
31 std::cerr << "Missing arguments " << std::endl;
32 std::cerr << "Usage: " << std::endl;
33 std::cerr << argv[0] << " outputImageFile" << std::endl;
34 std::cerr << "[baselineImage differenceImage]" << std::endl;
35 return EXIT_FAILURE;
36 }
37
38 constexpr unsigned int ImageDimension = 2;
39
40 using PixelType = unsigned long;
41
42 using ImageType = itk::Image< PixelType, ImageDimension >;
43 using WriterType = itk::ImageFileWriter< ImageType >;
44
45 ImageType::Pointer image = ImageType::New();
46
47 WriterType::Pointer writer = WriterType::New();
48
49 ImageType::SizeType size;
50
51 size[0] = 1000;
52 size[1] = 1000;
53
54 unsigned long numberOfSamples = size[0] * size[1];
55
56 ImageType::IndexType start;
57 start.Fill(0);
58
59 ImageType::RegionType region;
60 region.SetIndex( start );
61 region.SetSize( size );
62
63 image->SetRegions( region );
64 image->Allocate(true); // initialize buffer to zero
65
66 using RandomIteratorType = itk::ImageRandomIteratorWithIndex< ImageType >;
67
68 RandomIteratorType it( image, region );
69
70 it.SetNumberOfSamples( numberOfSamples );
71
72 it.GoToBegin();
73
74 PixelType counter = 0;
75
76 //
77 // Write down the order in which pixels are visited
78 //
79 while( !it.IsAtEnd() )
80 {
81 it.Set( counter );
82 ++it;
83 ++counter;
84 }
85
86 writer->SetInput( image );
87 writer->SetFileName( argv[1] );
88 writer->Update();
89
90 if( argc > 4 )
91 {
92
93 using ReaderType = itk::ImageFileReader< ImageType >;
94
95 ReaderType::Pointer reader = ReaderType::New();
96
97 reader->SetFileName( argv[2] );
98
99 using DifferencePixelType = signed long;
100 using DifferenceImageType = itk::Image< DifferencePixelType, ImageDimension >;
101
102 using DifferenceFilterType = itk::Testing::ComparisonImageFilter<
103 ImageType, DifferenceImageType >;
104
105 DifferenceFilterType::Pointer difference = DifferenceFilterType::New();
106
107 difference->SetValidInput( image );
108 difference->SetTestInput( reader->GetOutput() );
109 difference->SetToleranceRadius( 0 );
110 difference->SetDifferenceThreshold( 0 );
111
112 using DifferenceWriterType = itk::ImageFileWriter< DifferenceImageType >;
113 DifferenceWriterType::Pointer writer2 = DifferenceWriterType::New();
114
115 writer2->SetInput( difference->GetOutput() );
116
117 try
118 {
119 writer2->Update();
120 }
121 catch( itk::ExceptionObject & excp )
122 {
123 std::cerr << excp << std::endl;
124 return EXIT_FAILURE;
125 }
126
127 std::cout << "Number of pixels with differences = ";
128 std::cout << difference->GetNumberOfPixelsWithDifferences() << std::endl;
129 }
130
131 return EXIT_SUCCESS;
132
133 }
134