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
|
/*=========================================================================
*
* 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.
*
*=========================================================================*/
#include <iostream>
#include "itkConstantPadImageFilter.h"
#include "itkStreamingImageFilter.h"
#include "itkFilterWatcher.h"
#include "itkMath.h"
int itkConstantPadImageTest(int, char* [] )
{
// typedefs to simplify the syntax
typedef itk::Image< short, 2 > ShortImage;
typedef itk::Image< float, 2 > FloatImage;
// Test the creation of an image with native type
ShortImage::Pointer image = ShortImage::New();
// fill in an image
ShortImage::IndexType index = {{0, 0}};
ShortImage::SizeType size = {{8, 12}};
ShortImage::RegionType region;
int row, column;
region.SetSize( size );
region.SetIndex( index );
image->SetLargestPossibleRegion( region );
image->SetBufferedRegion( region );
image->Allocate();
itk::ImageRegionIterator< ShortImage > iterator(image, region);
short i=0;
for (; !iterator.IsAtEnd(); ++iterator, ++i)
{
iterator.Set( i );
}
// Create a filter
typedef itk::ConstantPadImageFilter< ShortImage, FloatImage > PadFilterType;
PadFilterType::Pointer constantPad = PadFilterType::New();
FilterWatcher watch( constantPad );
constantPad->SetInput( image );
typedef ShortImage::SizeValueType SizeValueType;
typedef ShortImage::IndexValueType IndexValueType;
SizeValueType upperFactors[2] = { 0, 0 };
SizeValueType lowerFactors[2] = { 0, 0 };
float constant = 13.3f;
constantPad->SetConstant( constant );
// check the method using the SizeType rather than the simple table type.
ShortImage::SizeType stfactors;
stfactors.Fill( 0 );
constantPad->SetPadLowerBound( stfactors );
constantPad->SetPadUpperBound( stfactors );
constantPad->SetPadBound( stfactors );
constantPad->UpdateLargestPossibleRegion();
std::cout << constantPad << std::endl;
std::cout << "Input spacing: " << image->GetSpacing()[0] << ", "
<< image->GetSpacing()[1] << std::endl;
std::cout << "Output spacing: " << constantPad->GetOutput()->GetSpacing()[0]
<< ", "
<< constantPad->GetOutput()->GetSpacing()[1] << std::endl;
ShortImage::RegionType requestedRegion;
bool passed;
// CASE 1
lowerFactors[0] = 1; lowerFactors[1] = 2;
upperFactors[0] = 3; upperFactors[1] = 4;
constantPad->SetPadLowerBound( lowerFactors );
constantPad->SetPadUpperBound( upperFactors );
constantPad->UpdateLargestPossibleRegion();
requestedRegion = constantPad->GetOutput()->GetRequestedRegion();
itk::ImageRegionIterator< FloatImage >
iteratorIn1( constantPad->GetOutput(), requestedRegion );
passed = true;
size = requestedRegion.GetSize();
index = requestedRegion.GetIndex();
if ((index[0] != (0 - (IndexValueType) lowerFactors[0]))
|| (index[1] != (0 - (IndexValueType) lowerFactors[1]))
|| (size[0] != (8 + lowerFactors[0] + upperFactors[0]))
|| (size[1] != (12 + lowerFactors[1] + upperFactors[1])))
{
passed = false;
}
else
{
for (; !iteratorIn1.IsAtEnd(); ++iteratorIn1)
{
row = iteratorIn1.GetIndex()[0];
column = iteratorIn1.GetIndex()[1];
if ((row < 0) || (row>7) || (column < 0) || (column > 11))
{
if ( itk::Math::NotExactlyEquals(iteratorIn1.Get(), constant) )
{
passed = false;
}
}
else
{
int nextVal = 8*column+row;
if (itk::Math::NotExactlyEquals(iteratorIn1.Get(), nextVal))
{
std::cout << "Error: (" << row << ", " << column
<< "), expected " << nextVal << " got "
<< iteratorIn1.Get() << std::endl;
passed = false;
}
}
}
}
if (passed)
{
std::cout << "constantPadImageFilter case 1 passed." << std::endl;
}
else
{
std::cout << "constantPadImageFilter case 1 failed." << std::endl;
return EXIT_FAILURE;
}
// CASE 2
lowerFactors[0] = 10;
upperFactors[1] = 15;
constantPad->SetPadLowerBound( lowerFactors );
constantPad->SetPadUpperBound( upperFactors );
// Create a stream
typedef itk::StreamingImageFilter< FloatImage, FloatImage > StreamingFilter;
StreamingFilter::Pointer stream = StreamingFilter::New();
stream->SetInput( constantPad->GetOutput() );
stream->SetNumberOfStreamDivisions( 2 );
if ((constantPad->GetPadUpperBound()[0] != upperFactors[0])
|| (constantPad->GetPadUpperBound()[1] != upperFactors[1])
|| (constantPad->GetPadLowerBound()[0] != lowerFactors[0])
|| (constantPad->GetPadLowerBound()[1] != lowerFactors[1]))
{
passed = false;
}
else
{
stream->UpdateLargestPossibleRegion();
requestedRegion = stream->GetOutput()->GetRequestedRegion();
itk::ImageRegionIterator< FloatImage >
iteratorIn2(stream->GetOutput(), requestedRegion);
passed = true;
size = requestedRegion.GetSize();
index = requestedRegion.GetIndex();
if ((index[0] != (0 - (IndexValueType) lowerFactors[0]))
|| (index[1] != (0 - (IndexValueType) lowerFactors[1]))
|| (size[0] != (8 + lowerFactors[0] + upperFactors[0]))
|| (size[1] != (12 + lowerFactors[1] + upperFactors[1])))
{
passed = false;
}
else
{
for (; !iteratorIn2.IsAtEnd(); ++iteratorIn2)
{
row = iteratorIn2.GetIndex()[0];
column = iteratorIn2.GetIndex()[1];
if ((row < 0) || (row>7) || (column < 0) || (column > 11))
{
if ( itk::Math::NotExactlyEquals(iteratorIn2.Get(), constant) )
{
passed = false;
}
}
else
{
int nextVal = 8*column+row;
if (itk::Math::NotExactlyEquals(iteratorIn2.Get(), nextVal))
{
std::cout << "Error: (" << row << ", " << column
<< "), expected " << nextVal << " got "
<< iteratorIn2.Get() << std::endl;
passed = false;
}
}
}
}
}
if ( passed )
{
std::cout << "constantPadImageFilter case 2 passed." << std::endl;
}
else
{
std::cout << "constantPadImageFilter case 2 failed." << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
|