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 *
20 * This program illustrates the use of Adaptors and
21 * NthElementPixelAccessor
22 *
23 * The example shows how an Adaptor can be used to
24 * get acces only to the Nth component of a vector image
25 * giving the appearance of being just a 'float' image
26 *
27 * That will allow to pass the Nth component of this
28 * image as input or output to any filter that expects
29 * a float image
30 *
31 */
32
33 #include "itkAdaptImageFilter.h"
34 #include "itkImageRegionIteratorWithIndex.h"
35 #include "itkImageRegionIterator.h"
36 #include "itkNthElementPixelAccessor.h"
37 #include "itkTestingMacros.h"
38 #include "itkVariableLengthVector.h"
39 #include "itkVectorImage.h"
40
41 //-------------------------
42 //
43 // Main code
44 //
45 //-------------------------
itkNthElementPixelAccessorTest2(int,char * [])46 int itkNthElementPixelAccessorTest2(int, char* []) {
47
48 // Typedefs for convenience
49 constexpr unsigned int Dimension = 2;
50 constexpr unsigned int VectorLength = 3;
51
52 using PrecisionType = float;
53
54 using VectorImageType = itk::VectorImage< PrecisionType, Dimension >;
55 using PixelType = itk::VariableLengthVector< PrecisionType >;
56 using ScalarImageType = itk::Image< PrecisionType, Dimension >;
57 using AccessorType = itk::NthElementPixelAccessor< PrecisionType, PixelType >;
58 using AdaptorType = itk::AdaptImageFilter< VectorImageType, ScalarImageType, AccessorType >;
59
60 // Test on variable length vector image
61 VectorImageType::SizeType size;
62 size[0] = 1;
63 size[1] = 1;
64
65 VectorImageType::IndexType index;
66 index[0] = 0;
67 index[1] = 0;
68
69 VectorImageType::RegionType region;
70 region.SetIndex( index );
71 region.SetSize( size );
72
73 VectorImageType::Pointer vectorImage = VectorImageType::New();
74
75 vectorImage->SetLargestPossibleRegion( region );
76 vectorImage->SetBufferedRegion( region );
77 vectorImage->SetRequestedRegion( region );
78 vectorImage->SetNumberOfComponentsPerPixel( VectorLength );
79 vectorImage->Allocate();
80 PixelType pixel;
81 pixel.SetSize( VectorLength );
82 pixel.Fill(0);
83 vectorImage->FillBuffer(pixel);
84
85 PixelType referencePixel;
86 referencePixel.SetSize( VectorLength );
87
88 // Value to initialize the pixels
89 referencePixel[0] = 1.0f;
90 referencePixel[1] = 0.5f;
91 referencePixel[2] = 1.5f;
92
93 vectorImage->SetPixel( index, referencePixel );
94
95 // Print values to verify content
96 pixel = vectorImage->GetPixel( index );
97 std::cout << pixel[0] << " ";
98 std::cout << pixel[1] << " ";
99 std::cout << pixel[2] << std::endl;
100
101 // Access values using adaptor
102 AccessorType accessor;
103 accessor.SetElementNumber(0);
104
105 AdaptorType::Pointer adaptor = AdaptorType::New();
106 adaptor->SetInput( vectorImage );
107 adaptor->SetAccessor( accessor );
108 adaptor->Update();
109
110 ScalarImageType::Pointer scalarImage = adaptor->GetOutput();
111
112 TEST_EXPECT_EQUAL( adaptor->GetOutput()->GetPixel(index), referencePixel[0] );
113
114 // Test operator
115 AccessorType accessor1;
116 accessor1.SetElementNumber(1);
117 if (accessor1 != accessor)
118 {
119 accessor = accessor1;
120 adaptor->SetAccessor( accessor);
121 adaptor->Update();
122 scalarImage = adaptor->GetOutput();
123 TEST_EXPECT_EQUAL( adaptor->GetOutput()->GetPixel(index), referencePixel[1] );
124 }
125
126 return EXIT_SUCCESS;
127 }
128