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 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
|
/*=========================================================================
*
* 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 "itkBinShrinkImageFilter.h"
#include "itkShrinkImageFilter.h"
#include "itkPipelineMonitorImageFilter.h"
#include "itkImageRegionIteratorWithIndex.h"
#include "itkImageRegionConstIteratorWithIndex.h"
#include "itkTestingMacros.h"
int itkBinShrinkImageFilterTest1( int , char *[] )
{
// typedefs to simplify the syntax
typedef itk::Image< int, 2 > InputImageType;
typedef itk::Image< long int, 2 > OutputImageType;
InputImageType::Pointer sourceImage = InputImageType::New();
// fill in an image
InputImageType::IndexType index = {{100, 100}};
InputImageType::SizeType size = {{12, 20}};
InputImageType::RegionType region;
region.SetSize( size );
region.SetIndex( index );
sourceImage->SetRegions( region );
sourceImage->Allocate();
{
itk::ImageRegionIteratorWithIndex<InputImageType> outIt(sourceImage, region);
for ( outIt.GoToBegin(); !outIt.IsAtEnd(); ++outIt )
{
// we multiply by ten so for more precision
outIt.Set( outIt.GetIndex()[0]*10 );
}
}
// assemple pipeline
typedef itk::PipelineMonitorImageFilter<InputImageType>
InputMonitorFilterType;
InputMonitorFilterType::Pointer monitor1 = InputMonitorFilterType::New();
monitor1->SetInput( sourceImage );
typedef itk::BinShrinkImageFilter< InputImageType, OutputImageType >
BinShrinkFilterType;
BinShrinkFilterType::Pointer bin = BinShrinkFilterType::New();
// Exercise some methods for coverage
EXERCISE_BASIC_OBJECT_METHODS( bin, BinShrinkImageFilter,
ImageToImageFilter );
bin->SetInput( monitor1->GetOutput() );
itk::ModifiedTimeType t = bin->GetMTime();
std::cout << bin;
TEST_EXPECT_EQUAL(bin->GetShrinkFactors()[0], 1 );
// test no change
bin->SetShrinkFactors(1);
TEST_EXPECT_EQUAL(bin->GetShrinkFactors()[0], 1 );
TEST_EXPECT_EQUAL(t, bin->GetMTime());
// test zero value
bin->SetShrinkFactors(0);
TEST_EXPECT_EQUAL(bin->GetShrinkFactors()[0], 1 );
TEST_EXPECT_TRUE( t != bin->GetMTime() );
t = bin->GetMTime();
// no change
bin->SetShrinkFactor(0,1);
TEST_EXPECT_EQUAL(bin->GetShrinkFactors()[0], 1 );
TEST_EXPECT_EQUAL(t, bin->GetMTime());
bin->SetShrinkFactor(0,2);
TEST_EXPECT_EQUAL(bin->GetShrinkFactors()[0], 2 );
TEST_EXPECT_TRUE( t != bin->GetMTime() );
typedef itk::PipelineMonitorImageFilter<OutputImageType> OutputMonitorFilterType;
OutputMonitorFilterType::Pointer monitor2 = OutputMonitorFilterType::New();
monitor2->SetInput(bin->GetOutput());
bool failed = false;
try
{
// update with 1,1 shrink factor
unsigned int factors[2] = { 1, 1 };
std::cout << "== Testing with shrink factors " << factors[0] << " " << factors[1] << " == " << std::endl;
bin->SetShrinkFactors(factors);
monitor2->Update();
// check values
itk::ImageRegionConstIteratorWithIndex<OutputImageType> inIt( monitor2->GetOutput(),
monitor2->GetOutput()->GetLargestPossibleRegion() );
for (inIt.GoToBegin(); !inIt.IsAtEnd(); ++inIt)
{
if (inIt.Get() != inIt.GetIndex()[0]*10)
{
std::cout << "Wrong pixel value at " << inIt.GetIndex() << " of " << inIt.Get() << std::endl;
failed = true;
}
}
}
catch (itk::ExceptionObject &e)
{
std::cout << "Exception: " << e << std::endl;
failed = true;
}
try
{
// update with 2,1 shrink factor
unsigned int factors[2] = { 2, 1 };
std::cout << "== Testing with shrink factors " << factors[0] << " " << factors[1] << " == " << std::endl;
bin->SetShrinkFactors(factors);
monitor2->UpdateLargestPossibleRegion();
// check values
itk::ImageRegionConstIteratorWithIndex<OutputImageType> inIt( monitor2->GetOutput(),
monitor2->GetOutput()->GetLargestPossibleRegion() );
for (inIt.GoToBegin(); !inIt.IsAtEnd(); ++inIt)
{
if (inIt.Get() != static_cast< int >( inIt.GetIndex()[0]*factors[0] )*10 + 5 )
{
std::cout << "Wrong pixel value at " << inIt.GetIndex() << " of " << inIt.Get() << std::endl;
failed = true;
}
}
}
catch (itk::ExceptionObject &e)
{
std::cout << "Exception: " << e << std::endl;
failed = true;
}
try
{
// update with 2,2 shrink factor
unsigned int factors[2] = { 2, 2 };
std::cout << "== Testing with shrink factors " << factors[0] << " " << factors[1] << " == " << std::endl;
bin->SetShrinkFactors(factors);
monitor2->UpdateLargestPossibleRegion();
// check values
itk::ImageRegionConstIteratorWithIndex<OutputImageType> inIt( monitor2->GetOutput(),
monitor2->GetOutput()->GetLargestPossibleRegion() );
for (inIt.GoToBegin(); !inIt.IsAtEnd(); ++inIt)
{
if (inIt.Get() != static_cast< int >( inIt.GetIndex()[0]*factors[0] )*10 + 5 )
{
std::cout << "Wrong pixel value at " << inIt.GetIndex() << " of " << inIt.Get() << std::endl;
failed = true;
}
}
}
catch (itk::ExceptionObject &e)
{
std::cout << "Exception: " << e << std::endl;
failed = true;
}
try
{
// update with 5,2 shrink factor
unsigned int factors[2] = { 5, 2 };
std::cout << "== Testing with shrink factors " << factors[0] << " " << factors[1] << " == " << std::endl;
bin->SetShrinkFactors(factors);
monitor2->UpdateLargestPossibleRegion();
// check values
itk::ImageRegionConstIteratorWithIndex<OutputImageType> inIt( monitor2->GetOutput(),
monitor2->GetOutput()->GetLargestPossibleRegion() );
for (inIt.GoToBegin(); !inIt.IsAtEnd(); ++inIt)
{
if (inIt.Get() != static_cast< int >( inIt.GetIndex()[0]*factors[0]+2)*10 )
{
std::cout << "Wrong pixel value at " << inIt.GetIndex() << " of " << inIt.Get() << std::endl;
failed = true;
}
}
}
catch (itk::ExceptionObject &e)
{
std::cout << "Exception: " << e << std::endl;
failed = true;
}
std::cout << "-- Changing starting index to odd numer --" << std::endl;
// set to odd index then reallocate and set values
index[0] = 101;
region.SetIndex( index );
sourceImage->SetRegions( region );
sourceImage->Allocate();
{
itk::ImageRegionIteratorWithIndex<InputImageType> outIt(sourceImage, region);
for ( outIt.GoToBegin(); !outIt.IsAtEnd(); ++outIt )
{
// we multiply by ten for more precision
outIt.Set( outIt.GetIndex()[0]*10 );
}
}
monitor1->SetInput( sourceImage );
try
{
// update with 2,1 shrink factor
unsigned int factors[2] = { 2, 1 };
std::cout << "== Testing with shrink factors " << factors[0] << " " << factors[1] << " == " << std::endl;
bin->SetShrinkFactors(factors);
monitor2->UpdateLargestPossibleRegion();
// check values
itk::ImageRegionConstIteratorWithIndex<OutputImageType> inIt( monitor2->GetOutput(),
monitor2->GetOutput()->GetLargestPossibleRegion() );
for (inIt.GoToBegin(); !inIt.IsAtEnd(); ++inIt)
{
if (inIt.Get() != static_cast< int >( inIt.GetIndex()[0]*factors[0] )*10 + 5 )
{
std::cout << "Wrong pixel value at " << inIt.GetIndex() << " of " << inIt.Get() << std::endl;
failed = true;
}
}
}
catch (itk::ExceptionObject &e)
{
std::cout << "Exception: " << e << std::endl;
failed = true;
}
try
{
// update with 3,1 shrink factor
unsigned int factors[2] = { 3, 1 };
std::cout << "== Testing with shrink factors " << factors[0] << " " << factors[1] << " == " << std::endl;
bin->SetShrinkFactors(factors);
monitor2->UpdateLargestPossibleRegion();
// check values
itk::ImageRegionConstIteratorWithIndex<OutputImageType> inIt( monitor2->GetOutput(),
monitor2->GetOutput()->GetLargestPossibleRegion() );
for (inIt.GoToBegin(); !inIt.IsAtEnd(); ++inIt)
{
if (inIt.Get() != static_cast< int >(inIt.GetIndex()[0]*factors[0]+1 )*10 )
{
std::cout << "Wrong pixel value at " << inIt.GetIndex() << " of " << inIt.Get() << std::endl;
failed = true;
}
}
}
catch (itk::ExceptionObject &e)
{
std::cout << "Exception: " << e << std::endl;
failed = true;
}
index[0] = 0;
index[1] = 3;
region.SetIndex( index );
for (unsigned int shrink = 1; shrink < 12; ++shrink)
{
// update with 3,1 shrink factor
unsigned int factors[2] = { 1, shrink };
std::cout << "== Testing with shrink factors " << factors[0] << " " << factors[1] << " == " << std::endl;
for (unsigned int x = 1; x < 10; ++x )
for (unsigned int y = 2*shrink; y < 20; ++y )
{
// fill in an image
size[0] = x;
size[1] = y;
region.SetSize( size );
sourceImage->SetRegions( region );
sourceImage->Allocate();
std::cout << "--Resolution " << x << " " << y << "--" << std::endl;
itk::ImageRegionIteratorWithIndex<InputImageType> outIt(sourceImage, region);
for ( outIt.GoToBegin(); !outIt.IsAtEnd(); ++outIt )
{
// we multiply by ten so for more precision
outIt.Set( outIt.GetIndex()[0]*10 );
}
try
{
bin->SetShrinkFactors(factors);
monitor2->UpdateLargestPossibleRegion();
// check values
itk::ImageRegionConstIteratorWithIndex<OutputImageType> inIt( monitor2->GetOutput(),
monitor2->GetOutput()->GetLargestPossibleRegion() );
bool lfailed = false;
for (inIt.GoToBegin(); !inIt.IsAtEnd(); ++inIt)
{
int expectedValue = inIt.GetIndex()[0]*10;
if (inIt.Get() != expectedValue )
{
if (!lfailed)
{
std::cout << "--Resolution " << x << " " << y << "--" << std::endl;
}
std::cout << "Wrong pixel value at " << inIt.GetIndex() << " of " << inIt.Get() << ", expected: " <<
expectedValue << std::endl;
lfailed = failed = true;
}
}
if (lfailed)
{
monitor2->GetOutput()->Print(std::cout);
exit(1);
}
}
catch (itk::ExceptionObject &e)
{
std::cout << "Exception: " << e << std::endl;
failed = true;
}
}
}
if ( failed )
{
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
|