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 #include "itkBinShrinkImageFilter.h"
19 #include "itkPhysicalPointImageSource.h"
20 #include "itkVectorImage.h"
21 #include "itkImageRegionConstIterator.h"
22
23 namespace
24 {
25
26 // This function checks that all values in an image are equivalent to
27 // the physical point of the image.
28 template <typename TImageType>
CheckValueIsPhysicalPoint(const TImageType * img)29 bool CheckValueIsPhysicalPoint( const TImageType *img )
30 {
31
32 using IteratorType = itk::ImageRegionConstIterator<TImageType>;
33 IteratorType it(img, img->GetBufferedRegion() );
34
35 bool match = true;
36
37 typename TImageType::PointType pt;
38 img->TransformIndexToPhysicalPoint( it.GetIndex(), pt );
39 while( !it.IsAtEnd() )
40 {
41 for ( unsigned int i = 0; i < TImageType::ImageDimension; ++i )
42 {
43 img->TransformIndexToPhysicalPoint( it.GetIndex(), pt );
44 if ( !itk::Math::FloatAlmostEqual(pt[i], it.Get()[i]) )
45 {
46 typename TImageType::PointType::VectorType diff;
47 for( unsigned int j = 0; j < TImageType::ImageDimension; ++j )
48 {
49 diff[j] = pt[j] - it.Get()[j];
50 }
51
52 std::cout << "Index: " << it.GetIndex() << " Point: " << pt << " Value: " << it.Get() << " Difference:" <<
53 diff << std::endl;
54 match = false;
55 }
56 }
57
58 ++it;
59 }
60 return match;
61 }
62
63 }
64
itkBinShrinkImageFilterTest2(int,char * [])65 int itkBinShrinkImageFilterTest2( int , char *[] )
66 {
67
68 constexpr unsigned int ImageDimension = 2;
69
70 using PixelType = itk::Vector<double, ImageDimension>;
71 using ImageType = itk::Image<PixelType, ImageDimension>;
72
73 using SourceType = itk::PhysicalPointImageSource<ImageType>;
74 SourceType::Pointer source = SourceType::New();
75
76 SourceType::SizeValueType size[] = {512,509};
77 source->SetSize( size );
78
79 float origin[] = {1.1f, 2.22f};
80 source->SetOrigin( origin );
81
82 unsigned int factors[] = {1,1};
83 bool pass = true;
84
85 for( unsigned int xf = 1; xf < 5; ++xf )
86 {
87 factors[0] = xf;
88 for( unsigned int yf = 1; yf < 5; ++yf )
89 {
90 factors[1] = yf;
91
92 std::cout << "Testing with shrink factors:" << xf << " " << yf << std::endl;
93
94 using FilterType = itk::BinShrinkImageFilter<ImageType, ImageType>;
95 FilterType::Pointer shrink = FilterType::New();
96
97 shrink->SetInput(source->GetOutput() );
98
99 std::cout << "Testing with shrink factors:" << xf << " " << yf << std::endl;
100 shrink->SetShrinkFactors( factors );
101 shrink->UpdateLargestPossibleRegion();
102 if ( !CheckValueIsPhysicalPoint( shrink->GetOutput() ) )
103 {
104 pass = false;
105 std::cout << "== Failed with shrink factors " << factors[0] << " " << factors[1] << " == " << std::endl;
106 }
107 }
108 }
109
110 if ( pass )
111 {
112 return EXIT_SUCCESS;
113 }
114 else
115 {
116 return EXIT_FAILURE;
117 }
118
119 }
120