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 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969
|
/*=========================================================================
*
* 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 "itkTimeProbe.h"
#include "itkVectorImageToImageAdaptor.h"
#include "itkNthElementImageAdaptor.h"
#include "itkImageLinearIteratorWithIndex.h"
#include "itkShapedNeighborhoodIterator.h"
#include "itkImageRegionIteratorWithIndex.h"
#include "itkImageFileWriter.h"
#include "itkImageFileReader.h"
#include "itkTestingMacros.h"
#include "itkMath.h"
// This test tests:
// VectorImage
// -----------
// - Iterators on the VectorImage,
// - Timing tests comparing itk::VectorImage to similar itk::Image using
// FixedArray and VariableLengthVector
// - IO support for VectorImage.
//
template< typename TPixel, unsigned int VDimension, typename TAdaptor, unsigned int VVectorLength >
bool testVectorImageAdaptor( typename TAdaptor::Pointer & vectorImageAdaptor,
const typename itk::VectorImage< TPixel, VDimension >::Pointer & vectorImage,
const typename itk::VectorImage< TPixel, VDimension >::RegionType & region,
const unsigned int componentToExtract )
{
std::cout << "---------------------------------------------------------------" << std::endl;
std::cout << "Testing " << typeid(TAdaptor).name();
std::cout << " to extract a component from the vector image" << std::endl;
typedef TPixel PixelType;
const unsigned int Dimension = VDimension;
const unsigned int VectorLength = VVectorLength;
bool failed = false;
typedef itk::Image< PixelType , Dimension > AdaptedImageType;
typename AdaptedImageType::IndexType index;
index.Fill(10);
std::cout << "Before adaptor initialization, vectorImage->GetPixel("
<< index << ")[" << componentToExtract
<< "] = "
<< vectorImage->GetPixel( index )[componentToExtract]
<< std::endl;
typedef TAdaptor AdaptorType;
vectorImageAdaptor->SetImage( vectorImage );
vectorImageAdaptor->Update();
if( (itk::Math::NotExactlyEquals(vectorImageAdaptor->GetPixel(index), vectorImage->GetPixel( index )[componentToExtract]))
|| (itk::Math::NotExactlyEquals(vectorImage->GetPixel( index )[componentToExtract], componentToExtract) ))
{
std::cerr << "[FAILED]" << std::endl;
std::cerr << "vImageToImageAdaptor->GetPixel("
<< index << ") = "
<< vectorImageAdaptor->GetPixel(index)
<< ", vectorImage->GetPixel("
<< index << ")[" << componentToExtract
<< "] = "
<< vectorImage->GetPixel( index )[componentToExtract]
<< std::endl;
failed = true;
}
else
{
std::cout << "[PASSED]" << std::endl;
}
// Test adaptor with iterators:
itk::ImageRegionConstIteratorWithIndex< AdaptorType > adaptIt(vectorImageAdaptor, region);
adaptIt.GoToBegin();
bool itFailed = false;
typedef itk::FixedArray< PixelType, VectorLength > InternalPixelType;
InternalPixelType f;
for( unsigned int i=0; i<VectorLength; i++ ) { f[i] = i; }
while (!adaptIt.IsAtEnd())
{
PixelType pixel = adaptIt.Get();
if (itk::Math::NotExactlyEquals(pixel, f[componentToExtract]))
{
itFailed = true;
std::cout << "adaptIt(" << adaptIt.GetIndex() << ") = " << adaptIt.Get()
<< ", but f[" << componentToExtract << "] = " << f[componentToExtract] << std::endl;
}
++adaptIt;
}
if (itFailed)
{
std::cerr << "ImageRegionConstIteratorWithIndex on VectorImageAdaptor [FAILED]" << std::endl;
failed = true;
}
else
{
std::cout << "ImageRegionConstIteratorWithIndex on VectorImageAdaptor [PASSED]" << std::endl;
}
return failed;
}
template< typename TPixel, unsigned int VDimension >
bool testVectorImageBasicMethods( void )
{
const unsigned int VectorLength = 3 * VDimension;
typedef itk::VectorImage< TPixel, VDimension > VectorImageType;
std::cout << "Testing Get/SetPixel methods." << std::endl;
typename VectorImageType::Pointer image = VectorImageType::New();
typename VectorImageType::SizeType size = {{11, 9, 7}};
typename VectorImageType::RegionType region;
region.SetSize( size );
image->SetRegions( region );
image->SetNumberOfComponentsPerPixel( VectorLength );
image->Allocate();
typename VectorImageType::PixelType f( VectorLength );
f.Fill(3.14f);
image->FillBuffer( f );
typename VectorImageType::IndexType idx = {{1,1,1}};
typename VectorImageType::ConstPointer cimage(image);
// test get methods
TEST_EXPECT_EQUAL(f, image->GetPixel(idx));
TEST_EXPECT_EQUAL(f, cimage->GetPixel(idx));
TEST_EXPECT_EQUAL(f, (*image)[idx]);
TEST_EXPECT_EQUAL(f, (*cimage)[idx]);
// test get by reference methods
typename VectorImageType::PixelType v( VectorLength );
v.Fill(2.22f);
// note: this VectorImage method requires the compiler to perform return value
// optimization to work as expected
image->GetPixel(idx).Fill( 2.22f );
TEST_EXPECT_EQUAL(v, image->GetPixel(idx));
TEST_EXPECT_EQUAL(v, cimage->GetPixel(idx));
TEST_EXPECT_EQUAL(v, (*image)[idx]);
TEST_EXPECT_EQUAL(v, (*cimage)[idx]);
v.Fill(2.23f);
/*
// Cannot be used as an l-value
image->GetPixel(idx) = v;
TEST_EXPECT_EQUAL(v, image->GetPixel(idx));
TEST_EXPECT_EQUAL(v, cimage->GetPixel(idx));
TEST_EXPECT_EQUAL(v, (*image)[idx]);
TEST_EXPECT_EQUAL(v, (*cimage)[idx]);
*/
typename VectorImageType::PixelType temp = image->GetPixel(idx);
temp.Fill(2.24f);
v.Fill(2.24f);
TEST_EXPECT_EQUAL(v, image->GetPixel(idx));
TEST_EXPECT_EQUAL(v, cimage->GetPixel(idx));
TEST_EXPECT_EQUAL(v, (*image)[idx]);
TEST_EXPECT_EQUAL(v, (*cimage)[idx]);
v.Fill(3.33f);
(*image)[idx].Fill( 3.33f );
TEST_EXPECT_EQUAL(v, image->GetPixel(idx));
TEST_EXPECT_EQUAL(v, cimage->GetPixel(idx));
TEST_EXPECT_EQUAL(v, (*image)[idx]);
TEST_EXPECT_EQUAL(v, (*cimage)[idx]);
// test immutable access methods
v.Fill(3.33f);
// comp error
// cimage->GetPixel(idx) = f;
typename VectorImageType::PixelType temp2 = cimage->GetPixel(idx);
// the image actually get modified!!! :(
// The following line modifies the image and is considered a bug in
// the interface design that it works.
//temp2.Fill(3.44f);
TEST_EXPECT_EQUAL(v, image->GetPixel(idx));
TEST_EXPECT_EQUAL(v, cimage->GetPixel(idx));
TEST_EXPECT_EQUAL(v, (*image)[idx]);
TEST_EXPECT_EQUAL(v, (*cimage)[idx]);
// test set method
v.Fill(4.44f);
image->SetPixel(idx, v);
TEST_EXPECT_EQUAL(v, image->GetPixel(idx));
TEST_EXPECT_EQUAL(v, cimage->GetPixel(idx));
TEST_EXPECT_EQUAL(v, (*image)[idx]);
TEST_EXPECT_EQUAL(v, (*cimage)[idx]);
std::cout << "Testing Get/SetPixel methods [PASSED]" << std::endl;
// test Graft method
typename VectorImageType::Pointer imageGraft = VectorImageType::New();
imageGraft->Graft(image);
TEST_EXPECT_EQUAL(image->GetPixelContainer(), imageGraft->GetPixelContainer());
std::cout << "Testing Graft method [PASSED]" << std::endl;
return EXIT_SUCCESS;
}
int itkVectorImageTest( int, char* argv[] )
{
bool failed = false;
const unsigned int Dimension = 3;
const unsigned int VectorLength = 2 * Dimension;
typedef float PixelType;
if ( testVectorImageBasicMethods<double, 3>() == EXIT_FAILURE )
{
std::cout << "Testing basic methods [FAILED]" << std::endl;
failed = true;
}
{
// Test 1.
//
// Create an Image of VariableLengthVector, FixedArray, VectorImage of length 6 and compare
// times.
//
// Three images.. for crude timing analysis.
typedef itk::Image< itk::VariableLengthVector< PixelType >, Dimension >
VariableLengthVectorImageType;
typedef itk::Image< itk::FixedArray< PixelType, VectorLength >,
Dimension > FixedArrayImageType;
typedef itk::VectorImage< PixelType, Dimension > VectorImageType;
// Using image of VariableLengthVector< PixelType >
{
typedef itk::VariableLengthVector< PixelType > InternalPixelType;
itk::TimeProbe clock;
clock.Start();
VariableLengthVectorImageType::Pointer image = VariableLengthVectorImageType::New();
VariableLengthVectorImageType::IndexType start;
InternalPixelType f( VectorLength );
VariableLengthVectorImageType::SizeType size;
for( unsigned int i=0; i<VectorLength; i++ ) { f[i] = i; }
start.Fill(0);
size.Fill(50);
VariableLengthVectorImageType::RegionType region;
region.SetSize( size );
region.SetIndex( start );
image->SetRegions( region );
image->Allocate();
image->FillBuffer( f );
clock.Stop();
double timeTaken = clock.GetMean();
std::cout << "Allocating an image of itk::VariableLengthVector of length " << VectorLength
<< " with image size " << size << " took " << timeTaken << " s." << std::endl;
// Const iterator over the image...
{
clock.Start();
typedef itk::ImageRegionConstIterator< VariableLengthVectorImageType > IteratorType;
IteratorType it( image, image->GetBufferedRegion() );
it.GoToBegin();
while( !it.IsAtEnd() )
{
it.Get();
++it;
}
clock.Stop();
std::cout << "ConstIterator Get() over the entire image took : " <<
clock.GetMean() << " s." << std::endl;
}
}
// Using image of FixedArray< PixelType, VectorLength >
{
itk::TimeProbe clock;
clock.Start();
typedef itk::FixedArray< PixelType, VectorLength > InternalPixelType;
FixedArrayImageType::Pointer image = FixedArrayImageType::New();
FixedArrayImageType::IndexType start;
InternalPixelType f;
FixedArrayImageType::SizeType size;
for( unsigned int i=0; i<VectorLength; i++ ) { f[i] = i; }
start.Fill(0);
size.Fill(50);
FixedArrayImageType::RegionType region;
region.SetSize( size );
region.SetIndex( start );
image->SetRegions( region );
image->Allocate();
image->FillBuffer( f );
clock.Stop();
double timeTaken = clock.GetMean();
std::cout << "Allocating an image of itk::FixedArray of length " << VectorLength
<< " with image size " << size << " took " << timeTaken << " s." << std::endl;
{
// Test and compare times with iterators
//
// First set some pixel
for( unsigned int i=0; i<VectorLength; i++ ) { f[i] = i*0.1; }
FixedArrayImageType::IndexType idx;
for (unsigned int i = 0; i < Dimension; i++)
{
idx[i] = 4;
}
idx[Dimension-1] = 12;
image->SetPixel( idx, f );
}
{
clock.Start();
typedef itk::ImageRegionConstIterator< FixedArrayImageType > IteratorType;
IteratorType it( image, image->GetBufferedRegion() );
it.GoToBegin();
while( !it.IsAtEnd() )
{
it.Get();
++it;
}
clock.Stop();
std::cout << "ConstIterator Get() over the entire image took : " <<
clock.GetMean() << " s." << std::endl;
}
}
// Using VectorImage< PixelType, Dimension >
{
itk::TimeProbe clock;
clock.Start();
VectorImageType::Pointer vectorImage = VectorImageType::New();
VectorImageType::IndexType start;
itk::VariableLengthVector< PixelType > f( VectorLength );
VectorImageType::SizeType size;
for( unsigned int i=0; i<VectorLength; i++ ) { f[i] = i; }
start.Fill(0);
size.Fill(50);
VectorImageType::RegionType region;
region.SetSize( size );
region.SetIndex( start );
vectorImage->SetVectorLength( VectorLength );
vectorImage->SetRegions( region );
vectorImage->Allocate();
vectorImage->FillBuffer( f );
clock.Stop();
double timeTaken = clock.GetMean();
std::cout << "Allocating an image of itk::VectorImage with pixels length " << VectorLength
<< " with image size " << size << " took " << timeTaken << " s." << std::endl;
// Iterator tests on the vector image.
//
// Const iterator over the vector image...
{
clock.Start();
typedef itk::ImageRegionConstIterator< VectorImageType > IteratorType;
IteratorType it( vectorImage, vectorImage->GetBufferedRegion() );
it.GoToBegin();
while( !it.IsAtEnd() )
{
it.Get();
++it;
}
clock.Stop();
std::cout << "ConstIterator Get() over the entire vectorImage took : " <<
clock.GetMean() << " s." << std::endl;
}
const unsigned int componentToExtract = 2 * (Dimension -1);
typedef itk::VectorImageToImageAdaptor< PixelType, Dimension > VectorImageToImageAdaptorType;
VectorImageToImageAdaptorType::Pointer vectorImageToImageAdaptor = VectorImageToImageAdaptorType::New();
vectorImageToImageAdaptor->SetExtractComponentIndex( componentToExtract );
if( vectorImageToImageAdaptor->GetExtractComponentIndex() != componentToExtract )
{
std::cerr << "[FAILED]" << std::endl;
failed = true;
}
bool adaptorTestResult;
adaptorTestResult = testVectorImageAdaptor< PixelType, Dimension, VectorImageToImageAdaptorType, VectorLength >( vectorImageToImageAdaptor,
vectorImage,
region,
componentToExtract );
if( adaptorTestResult )
{
failed = true;
}
typedef itk::NthElementImageAdaptor< VectorImageType, PixelType > NthElementImageAdaptorType;
NthElementImageAdaptorType::Pointer nthElementImageAdaptor = NthElementImageAdaptorType::New();
nthElementImageAdaptor->SelectNthElement( componentToExtract );
adaptorTestResult = testVectorImageAdaptor< PixelType, Dimension, NthElementImageAdaptorType, VectorLength >( nthElementImageAdaptor,
vectorImage,
region,
componentToExtract );
if( adaptorTestResult )
{
failed = true;
}
}
// Test with Region and Linear iterators...
{
// Create a small image
VectorImageType::Pointer vectorImage = VectorImageType::New();
VectorImageType::IndexType start;
itk::VariableLengthVector< PixelType > f( VectorLength );
itk::VariableLengthVector< PixelType > ZeroPixel( VectorLength );
ZeroPixel.Fill( itk::NumericTraits< PixelType >::ZeroValue() );
for( unsigned int i=0; i<VectorLength; i++ ) { f[i] = i; }
start.Fill(0);
VectorImageType::SizeType size;
size.Fill(11);
size[Dimension-1] = 5;
unsigned long midCtr = 1;
for (unsigned int i = 0; i < Dimension; i++) { midCtr *= size[i]; }
VectorImageType::RegionType region( start, size );
vectorImage->SetVectorLength( VectorLength );
vectorImage->SetRegions( region );
vectorImage->Allocate();
vectorImage->FillBuffer( ZeroPixel );
start.Fill(3);
start[Dimension-1] = 2;
size.Fill(4);
size[Dimension-1] = 2;
VectorImageType::RegionType subRegion( start, size );
typedef itk::ImageRegionIterator< VectorImageType > ImageRegionIteratorType;
ImageRegionIteratorType rit( vectorImage, subRegion );
rit.GoToBegin();
while( !rit.IsAtEnd() )
{
rit.Set( f );
++rit;
}
typedef itk::ImageRegionConstIterator< VectorImageType > ConstIteratorType;
ConstIteratorType cit( vectorImage, vectorImage->GetBufferedRegion() );
unsigned long ctr = 0;
cit.GoToBegin();
midCtr /= 2;
while( !cit.IsAtEnd() )
{
itk::VariableLengthVector< PixelType > value = cit.Get();
++cit;
if( ctr == midCtr )
{
if( value != f )
{
std::cerr <<
"ImageRegionConstIteratorTest on VectorImage [FAILED]" << std::endl;
failed = true;
}
}
++ctr;
}
std::cout << "ImageRegionConstIteratorTest on VectorImage [PASSED]" << std::endl;
{
// Test itkImageLinearIteratorWithIndex
typedef itk::ImageLinearConstIteratorWithIndex< VectorImageType > LinearConstIteratorType;
typedef itk::ImageLinearIteratorWithIndex< VectorImageType > LinearIteratorType;
LinearConstIteratorType lcit( vectorImage, vectorImage->GetBufferedRegion() );
lcit.SetDirection( Dimension-1 );
lcit.GoToBegin();
itk::VariableLengthVector< PixelType > value;
while( !lcit.IsAtEnd() )
{
while( !lcit.IsAtEndOfLine() )
{
value = lcit.Get();
if( subRegion.IsInside( lcit.GetIndex() ) )
{
if( value!=f )
{
std::cerr <<
"ImageLinearConstIteratorWithIndex on VectorImage [FAILED]" << std::endl;
failed = true;
}
}
else
{
if( value!=ZeroPixel )
{
std::cerr <<
"ImageLinearConstIteratorWithIndex on VectorImage [FAILED]" << std::endl;
failed = true;
}
}
++lcit;
}
lcit.NextLine();
}
VectorImageType::IndexType idx;
idx.Fill(1);
LinearIteratorType lit( vectorImage, vectorImage->GetBufferedRegion() );
lit.SetIndex( idx );
lit.Set( f );
lcit.SetIndex( idx );
value = lcit.Get();
if( value != f )
{
std::cerr <<
"ImageLinearConstIteratorWithIndex on VectorImage [FAILED]" << std::endl;
failed = true;
}
std::cout << "ImageLinearConstIteratorWithIndex on VectorImage [PASSED]" << std::endl;
std::cout << "ImageLinearIteratorWithIndex on VectorImage [PASSED]" << std::endl;
}
}
}
// Test IO support.
{
// Create an image using itk::Vector
typedef itk::Vector< PixelType, VectorLength > VectorPixelType;
typedef itk::Image< itk::Vector< PixelType, VectorLength >,
Dimension > VectorImageType;
VectorImageType::Pointer image = VectorImageType::New();
VectorImageType::IndexType start;
start.Fill(0);
VectorImageType::SizeType size;
size.Fill(5);
VectorImageType::RegionType region( start, size );
image->SetRegions( region );
image->Allocate();
typedef itk::ImageRegionIteratorWithIndex< VectorImageType > IteratorType;
IteratorType it( image, region );
it.GoToBegin();
while( !it.IsAtEnd() )
{
VectorPixelType f;
for (unsigned int i = 0; i < Dimension; i++)
{
f[i] = it.GetIndex()[i];
f[Dimension+i] = it.GetIndex()[i];
}
it.Set( f );
++it;
}
typedef itk::ImageFileWriter< VectorImageType > WriterType;
WriterType::Pointer writer = WriterType::New();
writer->SetInput( image );
writer->SetFileName( argv[2] );
writer->Update();
writer->SetFileName( argv[1] );
writer->Update();
}
{
// Now read it as a itk::VectorImage.
typedef itk::VectorImage< PixelType, Dimension > VectorImageType;
typedef itk::ImageFileReader< VectorImageType > ReaderType;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName( argv[1] );
reader->Update();
VectorImageType::Pointer vectorImage = reader->GetOutput();
typedef itk::ImageRegionConstIteratorWithIndex< VectorImageType > IteratorType;
IteratorType cit( vectorImage, vectorImage->GetBufferedRegion() );
cit.GoToBegin();
bool failed1 = false;
while( !cit.IsAtEnd() )
{
for (unsigned int i = 0; i < Dimension; i++)
{
if (itk::Math::NotExactlyEquals(cit.Get()[i], cit.GetIndex()[i]) ||
itk::Math::NotExactlyEquals(cit.Get()[i+Dimension], cit.GetIndex()[i]))
{
failed1 = true;
}
}
++cit;
}
if( failed1 )
{
std::cerr << "Read VectorImage [FAILED]" << std::endl;
failed = true;
}
else
{
std::cout << "Read VectorImage [PASSED]" << std::endl;
}
// Now write this out this VectorImage and read it again
typedef itk::ImageFileWriter< VectorImageType > WriterType;
WriterType::Pointer writer = WriterType::New();
writer->SetInput( vectorImage );
writer->SetFileName(argv[1]);
writer->Update();
}
{
// Now read it as a itk::VectorImage.
typedef itk::VectorImage< PixelType, Dimension > VectorImageType;
typedef itk::ImageFileReader< VectorImageType > ReaderType;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName( argv[1] );
reader->Update();
VectorImageType::Pointer vectorImage = reader->GetOutput();
typedef itk::ImageRegionConstIteratorWithIndex< VectorImageType > IteratorType;
IteratorType cit( vectorImage, vectorImage->GetBufferedRegion() );
cit.GoToBegin();
bool failed1 = false;
while( !cit.IsAtEnd() )
{
for (unsigned int i = 0; i < Dimension; i++)
{
if (itk::Math::NotExactlyEquals(cit.Get()[i], cit.GetIndex()[i]) ||
itk::Math::NotExactlyEquals(cit.Get()[i+Dimension], cit.GetIndex()[i]))
{
failed1 = true;
}
}
++cit;
}
if( failed1 )
{
std::cerr << "Write VectorImage [FAILED]" << std::endl;
failed = true;
}
else
{
std::cout << "Write VectorImage [PASSED]" << std::endl;
}
{
// Check support for Neighborhood Iterators
//
// 1. Test ConstNeighborhoodIterator
//
std::cout << "Testing ConstNeighborhoodIterator...." << std::endl;
typedef itk::ConstNeighborhoodIterator< VectorImageType >
ConstNeighborhoodIteratorType;
ConstNeighborhoodIteratorType::RadiusType radius;
radius.Fill(1);
ConstNeighborhoodIteratorType::RegionType region
= vectorImage->GetBufferedRegion();
ConstNeighborhoodIteratorType::SizeType size;
size.Fill(4);
ConstNeighborhoodIteratorType::IndexType index;
index.Fill(1);
region.SetIndex(index);
region.SetSize( size );
ConstNeighborhoodIteratorType cNit(radius, vectorImage, region);
// Move Iterator to a point and see if it reads out the right value
//
unsigned int centerIndex = 1;
for (unsigned int i = 0; i < Dimension; i++)
{ centerIndex *= (radius[i]*2+1); }
centerIndex /= 2;
ConstNeighborhoodIteratorType::IndexType location;
for (unsigned int i = 0; i < Dimension; i++)
{
location[i] = i+1;
}
cNit.SetLocation( location );
if( cNit.GetPixel(centerIndex) != vectorImage->GetPixel( location ) )
{
std::cerr << " SetLocation [FAILED]" << std::endl;
failed=true;
}
// Test GoToBegin()
cNit.GoToBegin();
if( cNit.GetPixel(centerIndex) != vectorImage->GetPixel( index ) )
{
std::cerr << " GoToBegin [FAILED]" << std::endl;
failed=true;
}
// Test GoToEnd()
cNit.GoToEnd();
--cNit;
ConstNeighborhoodIteratorType::IndexType endIndex;
endIndex.Fill(4);
if( cNit.GetPixel(centerIndex) != vectorImage->GetPixel( endIndex ) )
{
std::cerr << " GoToEnd [FAILED]" << std::endl;
failed=true;
}
// Test IsAtEnd()
if( !((++cNit).IsAtEnd()) )
{
std::cerr << " IsAtEnd() [FAILED]" << std::endl;
failed = true;
}
cNit.GoToBegin();
unsigned int numPixelsTraversed = 1;
for (unsigned int i = 0; i < Dimension; i++)
{
numPixelsTraversed *= size[i];
}
while (! cNit.IsAtEnd())
{
++cNit;
--numPixelsTraversed;
}
if( numPixelsTraversed ) { std::cerr << " IsAtEnd() [FAILED]" << std::endl; }
// Test operator-
--cNit;
ConstNeighborhoodIteratorType::OffsetType offset;
offset.Fill(1);
cNit -= offset;
itk::VariableLengthVector< PixelType > pixel = cNit.GetCenterPixel();
itk::VariableLengthVector< PixelType > correctAnswer( VectorLength );
correctAnswer.Fill( 3 );
if( pixel != correctAnswer )
{
std::cerr << " operator- [FAILED]" << std::endl;
failed = true;
}
// Test GetNeighborhood()
cNit.SetLocation( location );
ConstNeighborhoodIteratorType::NeighborhoodType
neighborhood = cNit.GetNeighborhood();
//const unsigned int neighborhoodSize = neighborhood.Size();
//for( unsigned int i=0; i< neighborhoodSize; i++)
// { std::cout << neighborhood[i] << std::endl; }
if( (itk::Math::NotExactlyEquals(neighborhood[0][0], 0)) || (itk::Math::NotExactlyEquals(neighborhood[0][2*Dimension-1], (Dimension-1))))
{
std::cerr << " GetNeighborhood() on ConstNeighborhoodIterator [FAILED]" << std::endl;
failed = true;
}
//
// 2. Test NeighborhoodIterator on VectorImage
//
std::cout << "Testing NeighborhoodIterator..." << std::endl;
typedef itk::NeighborhoodIterator< VectorImageType > NeighborhoodIteratorType;
NeighborhoodIteratorType nit(radius, vectorImage, region);
nit.SetLocation( location );
itk::VariableLengthVector< PixelType > p( VectorLength );
p.Fill( 100.0 );
nit.SetNext( 1, 1, p );
// Test SetNext()
NeighborhoodIteratorType::IndexType index1;
index1 = location; index1[1] = location[1] + 1;
nit.SetLocation( index1 );
if( nit.GetCenterPixel() != p )
{
std::cerr << " SetNext() [FAILED]" << std::endl;
failed = true;
}
for (unsigned i = 0; i < Dimension; i++)
{
p[i] = p[Dimension + i] = (float)index1[i];
}
nit.SetCenterPixel( p );
if( nit.GetCenterPixel() != p )
{
std::cerr << " SetCenterPixel() [FAILED]" << std::endl;
failed = true;
}
// Test SetNeighborhood() and GetPrevious()
nit.SetLocation( index1 );
nit.SetNeighborhood( neighborhood );
for (unsigned i = 0; i < Dimension; i++)
{
p[i] = p[Dimension + i] = i + 1;
}
p[Dimension-1] = p[2*Dimension - 1] = Dimension - 1;
if( nit.GetPrevious( Dimension-1, 1 ) != p )
{
std::cerr << " SetNeighborhood() or GetPrevious() [FAILED]" << std::endl;
failed = true;
}
if (Dimension == 3)
{
//
// 3. Testing ConstShapedNeighborhoodIterator on VectorImage
//
// Go back to original image, where pixel values tell us the indices
std::cout << "Testing ConstShapedNeighborhoodIterator on VectorImage..."
<< std::endl;
reader->SetFileName( "dummy.nrrd");
reader->SetFileName( argv[1] );
reader->Update();
vectorImage = reader->GetOutput();
typedef itk::ConstShapedNeighborhoodIterator< VectorImageType >
ConstShapedNeighborhoodIteratorType;
ConstShapedNeighborhoodIteratorType cSnit( radius, vectorImage, region );
cSnit.SetLocation( location );
ConstShapedNeighborhoodIteratorType::OffsetType offset1;
offset1[0] = 0; offset1[1] = 0; offset1[2] = 0;
cSnit.ActivateOffset( offset1 ); //activate the center
// activate the top plane
offset1[0] = 0; offset1[1] = 0; offset1[2] = -1;
cSnit.ActivateOffset( offset1 );
offset1[0] = 0; offset1[1] = 1; offset1[2] = -1;
cSnit.ActivateOffset( offset1 );
offset1[0] = 0; offset1[1] = -1; offset1[2] = -1;
cSnit.ActivateOffset( offset1 );
offset1[0] = 1; offset1[1] = 0; offset1[2] = -1;
cSnit.ActivateOffset( offset1 );
offset1[0] = 1; offset1[1] = 1; offset1[2] = -1;
cSnit.ActivateOffset( offset1 );
offset1[0] = 1; offset1[1] = -1; offset1[2] = -1;
cSnit.ActivateOffset( offset1 );
offset1[0] = -1; offset1[1] = 0; offset1[2] = -1;
cSnit.ActivateOffset( offset1 );
offset1[0] = -1; offset1[1] = 1; offset1[2] = -1;
cSnit.ActivateOffset( offset1 );
offset1[0] = -1; offset1[1] = -1; offset1[2] = -1;
cSnit.ActivateOffset( offset1 );
ConstShapedNeighborhoodIteratorType::IndexListType l
= cSnit.GetActiveIndexList();
ConstShapedNeighborhoodIteratorType::IndexListType::const_iterator
ali = l.begin();
while (ali != l.end())
{
std::cout << *ali << " ";
++ali;
}
std::cout << std::endl;
ConstShapedNeighborhoodIteratorType::ConstIterator ci = cSnit.Begin();
while (! ci.IsAtEnd())
{
ConstShapedNeighborhoodIteratorType::OffsetType offset2 = ci.GetNeighborhoodOffset();
if( (offset2[0] == -1) && (offset2[1]== -1) && (offset2[2]== -1) )
{
if( ci.GetNeighborhoodIndex() != 0 )
{
failed = true;
std::cerr << "GetNeighborhoodOffset() on ConstShapedNeighborhoodIterato [FAILED]"
<< std::endl;
}
if( (itk::Math::NotExactlyEquals(ci.Get()[0], 0)) || (itk::Math::NotExactlyEquals(ci.Get()[1], 1)) || (itk::Math::NotExactlyEquals(ci.Get()[2] , 2)) )
{
failed=true;
std::cerr
<< "ConstShapedNeighborhoodIterator returned incorrect index [FAILED]"
<< std::endl;
}
}
ci++;
}
//
// 4. Test ShapedNeighborhoodIterator
//
typedef itk::ShapedNeighborhoodIterator< VectorImageType >
ShapedNeighborhoodIteratorType;
ShapedNeighborhoodIteratorType sNit( radius, vectorImage, region );
offset1[0] = 0; offset1[1] = 0; offset1[2] = 0;
sNit.ActivateOffset( offset1 ); //activate the center
// activate the top plane
offset1[0] = 0; offset1[1] = 0; offset1[2] = -1;
sNit.ActivateOffset( offset1 );
offset1[0] = 0; offset1[1] = 1; offset1[2] = -1;
sNit.ActivateOffset( offset1 );
offset1[0] = 0; offset1[1] = -1; offset1[2] = -1;
sNit.ActivateOffset( offset1 );
offset1[0] = 1; offset1[1] = 0; offset1[2] = -1;
sNit.ActivateOffset( offset1 );
offset1[0] = 1; offset1[1] = 1; offset1[2] = -1;
sNit.ActivateOffset( offset1 );
offset1[0] = 1; offset1[1] = -1; offset1[2] = -1;
sNit.ActivateOffset( offset1 );
offset1[0] = -1; offset1[1] = 0; offset1[2] = -1;
sNit.ActivateOffset( offset1 );
offset1[0] = -1; offset1[1] = 1; offset1[2] = -1;
sNit.ActivateOffset( offset1 );
offset1[0] = -1; offset1[1] = -1; offset1[2] = -1;
sNit.ActivateOffset( offset1 );
sNit.SetLocation( location );
ShapedNeighborhoodIteratorType::Iterator shit = sNit.Begin();
p[0] = p[3] = 10; p[1] = p[4] = 20; p[2] = p[5] = 30;
shit.Set( p );
index[0]=location[0]-1; index[1]=location[1]-1; index[2]=location[2]-1;
cNit.SetLocation( index );
if( cNit.GetCenterPixel() != p )
{
std::cerr << "ShapedNeighborhoodIterator Set() [FAILED]" << std::endl;
failed=true;
}
}
} // End Testing Neighborhood Iterators on VectorImage
}
if( failed )
{
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
|