File: itkJoinImageFilterTest.cxx

package info (click to toggle)
insighttoolkit5 5.4.3-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 704,384 kB
  • sloc: cpp: 783,592; ansic: 628,724; xml: 44,704; fortran: 34,250; python: 22,874; sh: 4,078; pascal: 2,636; lisp: 2,158; makefile: 464; yacc: 328; asm: 205; perl: 203; lex: 146; tcl: 132; javascript: 98; csh: 81
file content (257 lines) | stat: -rw-r--r-- 8,245 bytes parent folder | download
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
/*=========================================================================
 *
 *  Copyright NumFOCUS
 *
 *  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
 *
 *         https://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 "itkJoinImageFilter.h"
#include "itkRGBAPixel.h"
#include "vnl/vnl_sample.h"
#include "itkImageRegionIterator.h"
#include "itkTestingMacros.h"

int
itkJoinImageFilterTest(int, char *[])
{
  // Define the dimension of the images
  constexpr unsigned int myDimension = 2;

  // Declare the types of the images
  using myImageType1 = itk::Image<char, myDimension>;
  using myImageType2 = itk::Image<itk::Vector<unsigned short, 2>, myDimension>;
  using myImageType3 = itk::Image<itk::RGBAPixel<short>, myDimension>;

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

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

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

  // Create three images
  auto inputImageA = myImageType1::New();
  auto inputImageB = myImageType2::New();
  auto inputImageC = myImageType3::New();

  // Define their size, and start index
  mySizeType size;
  size[0] = 5;
  size[1] = 8;

  myIndexType start;
  start[0] = 0;
  start[1] = 0;

  myRegionType region{ start, size };

  // Initialize Image A
  inputImageA->SetRegions(region);
  inputImageA->Allocate();

  // Initialize Image B
  inputImageB->SetRegions(region);
  inputImageB->Allocate();

  // Initialize Image C
  inputImageC->SetRegions(region);
  inputImageC->Allocate();

  // Declare Iterator types apropriated for each image
  using myIteratorType1 = itk::ImageRegionIterator<myImageType1>;
  using myIteratorType2 = itk::ImageRegionIterator<myImageType2>;
  using myIteratorType3 = itk::ImageRegionIterator<myImageType3>;

  // Create one iterator for Image A (this is a light object)
  myIteratorType1 it1(inputImageA, region);

  // Initialize the content of Image A
  std::cout << "Image #1 " << std::endl;
  while (!it1.IsAtEnd())
  {
    it1.Set(static_cast<char>(vnl_sample_uniform(0, 255)));
    std::cout << static_cast<int>(it1.Get()) << std::endl;
    ++it1;
  }

  // Create one iterator for Image B (this is a light object)
  myIteratorType2 it2(inputImageB, region);

  // Initialize the content of Image B
  std::cout << std::endl;
  std::cout << "Image #2 " << std::endl;
  itk::Vector<unsigned short, 2> vec;
  while (!it2.IsAtEnd())
  {
    vec[0] = static_cast<unsigned short>(vnl_sample_uniform(0, 32765));
    vec[1] = static_cast<unsigned short>(vnl_sample_uniform(0, 32765));
    it2.Set(vec);
    std::cout << it2.Get() << std::endl;
    ++it2;
  }

  // Create one iterator for Image C (this is a light object)
  myIteratorType3 itRGBA(inputImageC, region);

  // Initialize the content of Image C
  std::cout << std::endl;
  std::cout << "Image #3 " << std::endl;
  itk::RGBAPixel<short> rgbaVec;
  while (!itRGBA.IsAtEnd())
  {
    rgbaVec[0] = static_cast<short>(vnl_sample_uniform(0, 255));
    rgbaVec[1] = static_cast<short>(vnl_sample_uniform(0, 255));
    rgbaVec[2] = static_cast<short>(vnl_sample_uniform(0, 255));
    rgbaVec[3] = static_cast<short>(vnl_sample_uniform(0, 255));
    itRGBA.Set(rgbaVec);
    //  std::cout << itRGBA.Get() << std::endl;
    ++itRGBA;
  }

  // Declare the types for the Join Filters
  using myFilterType = itk::JoinImageFilter<myImageType1, myImageType2>;
  using myFilterType1 = itk::JoinImageFilter<myImageType2, myImageType1>;
  using myFilterType2 = itk::JoinImageFilter<myImageType1, myImageType1>;
  using myFilterType3 = itk::JoinImageFilter<myFilterType::OutputImageType, myImageType3>;
  using myFilterType4 = itk::JoinImageFilter<myImageType2, myImageType2>;

  using myOutputIteratorType = itk::ImageRegionIterator<myFilterType::OutputImageType>;
  using myOutputIteratorType1 = itk::ImageRegionIterator<myFilterType1::OutputImageType>;
  using myOutputIteratorType2 = itk::ImageRegionIterator<myFilterType2::OutputImageType>;
  using myOutputIteratorType3 = itk::ImageRegionIterator<myFilterType3::OutputImageType>;
  using myOutputIteratorType4 = itk::ImageRegionIterator<myFilterType4::OutputImageType>;


  //
  // Join image #1 and #2
  //

  // Setup a JoinImageFilter
  auto filter = myFilterType::New();

  ITK_EXERCISE_BASIC_OBJECT_METHODS(filter, JoinImageFilter, BinaryGeneratorImageFilter);


  filter->SetInput1(inputImageA);
  filter->SetInput2(inputImageB);

  //
  // Join image #1#2 and #3
  //

  // Setup a JoinImageFilter
  auto filter123 = myFilterType3::New();
  filter123->SetInput1(filter->GetOutput());
  filter123->SetInput2(inputImageC);
  filter123->Update(); // This Update will force filter to execute, then filter123

  // Create an iterator for going through the image #1#2
  myFilterType::OutputImageType::Pointer outputImage = filter->GetOutput();
  myOutputIteratorType                   it3(outputImage, outputImage->GetRequestedRegion());

  // Print the content of the result image
  std::cout << std::endl;
  std::cout << "Joining #1 and #2 image " << std::endl;
  while (!it3.IsAtEnd())
  {
    std::cout << it3.Get() << std::endl;
    ++it3;
  }

  // Create an iterator for going through the image #1#2#3
  myFilterType3::OutputImageType::Pointer outputImage123 = filter123->GetOutput();
  myOutputIteratorType3                   it123(outputImage123, outputImage123->GetRequestedRegion());

  // Print the content of the result image
  std::cout << std::endl;
  std::cout << "Joining #1#2 and #3 image " << std::endl;
  while (!it123.IsAtEnd())
  {
    std::cout << it123.Get() << std::endl;
    ++it123;
  }

  //
  // Join image #2 and #1
  //

  // Setup a JoinImageFilter
  auto filter1 = myFilterType1::New();
  filter1->SetInput1(inputImageB);
  filter1->SetInput2(inputImageA);
  filter1->Update();

  // Create an iterator for going through the image output
  myFilterType1::OutputImageType::Pointer outputImage1 = filter1->GetOutput();
  myOutputIteratorType1                   it4(outputImage1, outputImage1->GetRequestedRegion());

  // Print the content of the result image
  std::cout << std::endl;
  std::cout << "Joining #2 and #1 image " << std::endl;
  while (!it4.IsAtEnd())
  {
    std::cout << it4.Get() << std::endl;
    ++it4;
  }

  //
  // Join image #1 and #1
  //

  // Setup a JoinImageFilter
  auto filter2 = myFilterType2::New();
  filter2->SetInput1(inputImageA);
  filter2->SetInput2(inputImageA);
  filter2->Update();

  // Create an iterator for going through the image output
  myFilterType2::OutputImageType::Pointer outputImage2 = filter2->GetOutput();
  myOutputIteratorType2                   it5(outputImage2, outputImage2->GetRequestedRegion());

  // Print the content of the result image
  std::cout << "Joining #1 and #1 image " << std::endl;
  while (!it5.IsAtEnd())
  {
    std::cout << static_cast<int>(it5.Get()[0]) << "  " << static_cast<int>(it5.Get()[1]) << std::endl;
    ++it5;
  }

  //
  // Join image #2 and #2
  //

  // Setup a JoinImageFilter
  auto filter4 = myFilterType4::New();
  filter4->SetInput1(inputImageB);
  filter4->SetInput2(inputImageB);
  filter4->Update();

  // Create an iterator for going through the image output
  myFilterType4::OutputImageType::Pointer outputImage4 = filter4->GetOutput();
  myOutputIteratorType4                   it6(outputImage4, outputImage4->GetRequestedRegion());

  // Print the content of the result image
  std::cout << "Joining #2 and #2 image " << std::endl;
  while (!it6.IsAtEnd())
  {
    std::cout << it6.Get() << std::endl;
    ++it6;
  }


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