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
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkImagePCADecompositionCalculatorTest.cxx,v $
Language: C++
Date: $Date: 2005-03-21 23:04:04 $
Version: $Revision: 1.6 $
Copyright (c) Insight Software Consortium. All rights reserved.
See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif
// Insight classes
#include "itkImage.h"
#include "itkImageRegionIterator.h"
#include "itkLightProcessObject.h"
#include "itkTextOutput.h"
#include "itkImagePCADecompositionCalculator.h"
// class to support progress feeback
class ShowProgressObject
{
public:
ShowProgressObject(itk::LightProcessObject * o)
{m_Process = o;}
void ShowProgress()
{std::cout << "Progress " << m_Process->GetProgress() << std::endl;}
itk::LightProcessObject::Pointer m_Process;
};
int itkImagePCADecompositionCalculatorTest(int, char* [] )
{
itk::OutputWindow::SetInstance(itk::TextOutput::New().GetPointer());
//Data definitions
const unsigned int IMGWIDTH = 2;
const unsigned int IMGHEIGHT = 2;
const unsigned int NDIMENSION = 2;
//------------------------------------------------------
//Create 3 simple test images with
//------------------------------------------------------
typedef itk::Image<double,NDIMENSION> InputImageType;
typedef
itk::ImageRegionIterator< InputImageType > InputImageIterator;
InputImageType::Pointer image1 = InputImageType::New();
InputImageType::Pointer image2 = InputImageType::New();
InputImageType::Pointer image3 = InputImageType::New();
InputImageType::Pointer image4 = InputImageType::New();
InputImageType::Pointer image5 = InputImageType::New();
InputImageType::Pointer image6 = InputImageType::New();
InputImageType::Pointer image7 = InputImageType::New();
InputImageType::Pointer image8 = InputImageType::New();
InputImageType::SizeType inputImageSize = {{ IMGWIDTH, IMGHEIGHT }};
InputImageType::IndexType index;
index.Fill(0);
InputImageType::RegionType region;
region.SetSize( inputImageSize );
region.SetIndex( index );
//--------------------------------------------------------------------------
// Set up Image 1 first
//--------------------------------------------------------------------------
image1->SetRegions( region );
image1->Allocate();
// setup the iterators
InputImageIterator image1It( image1, image1->GetBufferedRegion() );
//--------------------------------------------------------------------------
// Set up Image 2 first
//--------------------------------------------------------------------------
image2->SetRegions( region );
image2->Allocate();
// setup the iterators
InputImageIterator image2It( image2, image2->GetBufferedRegion() );
//--------------------------------------------------------------------------
// Set up Image 3 first
//--------------------------------------------------------------------------
image3->SetRegions( region );
image3->Allocate();
// setup the iterators
InputImageIterator image3It( image3, image3->GetBufferedRegion() );
//--------------------------------------------------------------------------
// Set up Image 4 first
//--------------------------------------------------------------------------
image4->SetRegions( region );
image4->Allocate();
// setup the iterators
InputImageIterator image4It( image4, image4->GetBufferedRegion() );
//--------------------------------------------------------------------------
// Set up Image 5 first
//--------------------------------------------------------------------------
image5->SetRegions( region );
image5->Allocate();
// setup the iterators
InputImageIterator image5It( image5, image5->GetBufferedRegion() );
//--------------------------------------------------------------------------
// Set up Image 6 first
//--------------------------------------------------------------------------
image6->SetRegions( region );
image6->Allocate();
// setup the iterators
InputImageIterator image6It( image6, image6->GetBufferedRegion() );
//--------------------------------------------------------------------------
// Set up Image 7 first
//--------------------------------------------------------------------------
image7->SetRegions( region );
image7->Allocate();
// setup the iterators
InputImageIterator image7It( image7, image7->GetBufferedRegion() );
//--------------------------------------------------------------------------
// Set up Image 8 first
//--------------------------------------------------------------------------
image8->SetRegions( region );
image8->Allocate();
// setup the iterators
InputImageIterator image8It( image8, image8->GetBufferedRegion() );
//--------------------------------------------------------------------------
//Manually create and store each vector
//--------------------------------------------------------------------------
// The first two vectors are the first two principal components of the data:
// [1 1 1 1] , [2 0 0 2], [0 3 3 0]
// The second two vectors are some of those data, which we will project down
// to the PC-space
// The next three vectors are a new basis set to project down into, so we can
// test changing bases mid-stream.
// The last image is the "mean image," here set to zero.
//Image no. 1
image1It.Set( -0.3853 ); ++image1It;
image1It.Set( 0.5929 ); ++image1It;
image1It.Set( 0.5929 ); ++image1It;
image1It.Set( -0.3853 ); ++image1It;
//Image no. 2
image2It.Set( -0.5929 ); ++image2It;
image2It.Set( -0.3853 ); ++image2It;
image2It.Set( -0.3853 ); ++image2It;
image2It.Set( -0.5929 ); ++image2It;
//Image no. 3
image3It.Set( 2 ); ++image3It;
image3It.Set( 0 ); ++image3It;
image3It.Set( 0 ); ++image3It;
image3It.Set( 2 ); ++image3It;
//Image no. 4
image4It.Set( 0 ); ++image4It;
image4It.Set( 3 ); ++image4It;
image4It.Set( 3 ); ++image4It;
image4It.Set( 0 ); ++image4It;
//Image no. 5
image5It.Set( 0.70710678 ); ++image5It; // 1/sqrt(2)
image5It.Set( 0.70710678 ); ++image5It;
image5It.Set( 0 ); ++image5It;
image5It.Set( 0 ); ++image5It;
//Image no. 6
image6It.Set( -0.70710678 ); ++image6It;
image6It.Set( 0.70710678 ); ++image6It;
image6It.Set( 0 ); ++image6It;
image6It.Set( 0 ); ++image6It;
//Image no. 7
image7It.Set( 0 ); ++image7It;
image7It.Set( 0 ); ++image7It;
image7It.Set( 1 ); ++image7It;
image7It.Set( 0 ); ++image7It;
//Image no. 8
image8It.Set( 0 ); ++image8It;
image8It.Set( 0 ); ++image8It;
image8It.Set( 0 ); ++image8It;
image8It.Set( 0 ); ++image8It;
//----------------------------------------------------------------------
// Test code for the Decomposition Calculator
//----------------------------------------------------------------------
//----------------------------------------------------------------------
//Set the image Decomposition Calculator
//----------------------------------------------------------------------
typedef itk::ImagePCADecompositionCalculator<InputImageType>
ImagePCAShapeModelEstimatorType;
ImagePCAShapeModelEstimatorType::Pointer
decomposer = ImagePCAShapeModelEstimatorType::New();
//----------------------------------------------------------------------
//Set the parameters of the clusterer
//----------------------------------------------------------------------
// add the first two vectors to the projection basis
ImagePCAShapeModelEstimatorType::BasisImagePointerVector basis;
basis.push_back(image1);
basis.push_back(image2);
decomposer->SetBasisImages(basis);
// compute some projections!
ImagePCAShapeModelEstimatorType::BasisVectorType proj3, proj4;
decomposer->SetImage(image3);
decomposer->Compute();
proj3 = decomposer->GetProjection();
decomposer->SetImage(image4);
decomposer->Compute();
proj4 = decomposer->GetProjection();
// get the basis images
ImagePCAShapeModelEstimatorType::BasisImagePointerVector basis_check;
basis_check = decomposer->GetBasisImages();
basis.clear();
basis.push_back(image5);
basis.push_back(image6);
basis.push_back(image7);
decomposer->SetBasisImages(basis);
ImagePCAShapeModelEstimatorType::BasisVectorType proj3_2, proj4_2;
//decomposer->SetImage(image4); // DON'T set image4 -- it should still
// be cached with the decomposer. Test that this works between basis changes.
decomposer->Compute();
proj4_2 = decomposer->GetProjection();
decomposer->SetImage(image3);
decomposer->Compute();
proj3_2 = decomposer->GetProjection();
ImagePCAShapeModelEstimatorType::BasisVectorType proj3_3, proj4_3;
// now test it with a mean image set
decomposer->SetMeanImage(image8);
decomposer->SetImage(image3);
decomposer->Compute();
proj3_3 = decomposer->GetProjection();
decomposer->SetImage(image4);
decomposer->Compute();
proj4_3 = decomposer->GetProjection();
// get the basis images
ImagePCAShapeModelEstimatorType::BasisImagePointerVector basis_check_2;
basis_check_2 = decomposer->GetBasisImages();
//Test the printself function to increase coverage
decomposer->Print(std::cout);
// Print the basis and projections: first the PCA basis
std::cout << "The basis of projection is: " << std::endl;
for (ImagePCAShapeModelEstimatorType::BasisImagePointerVector::const_iterator
basis_it = basis_check.begin(); basis_it != basis_check.end(); ++basis_it)
{
std::cout << "[";
InputImageIterator basisImage_it( *basis_it, (*basis_it)->GetBufferedRegion() );
for (basisImage_it.GoToBegin(); !basisImage_it.IsAtEnd(); ++basisImage_it)
{
std::cout << basisImage_it.Get() << " ";
}
std::cout << "]" << std::endl;
}
//Print the projections
std::cout << "The projection of [0 2 2 0] is [" << proj3 << "]" << std::endl;
std::cout << "this should be approx [-1.5412 -2.3716]" << std::endl;
std::cout << "The projection of [0 3 3 0] is [" << proj4 << "]" << std::endl;
std::cout << "this should be approx [3.5574 -2.3119]" << std::endl;
// Print the basis and projections: now the new basis
std::cout << std::endl;
std::cout << "Now the basis of projection is: " << std::endl;
for (ImagePCAShapeModelEstimatorType::BasisImagePointerVector::const_iterator
basis_it = basis_check_2.begin(); basis_it != basis_check_2.end(); ++basis_it)
{
std::cout << "[";
InputImageIterator basisImage_it( *basis_it, (*basis_it)->GetBufferedRegion() );
for (basisImage_it.GoToBegin(); !basisImage_it.IsAtEnd(); ++basisImage_it)
{
std::cout << basisImage_it.Get() << " ";
}
std::cout << "]" << std::endl;
}
//Print the projections
std::cout << "The projection of [0 2 2 0] is [" << proj3_2 << "]" << std::endl;
std::cout << "this should be approx [1.4142 -1.4142 0]" << std::endl;
std::cout << "The projection of [0 3 3 0] is [" << proj4_2 << "]" << std::endl;
std::cout << "this should be approx [2.1213 2.1213 3.000]" << std::endl;
std::cout << "The projection of [0 2 2 0] is (mean of zero set) [" << proj3_3 << "]" << std::endl;
std::cout << "this should be approx [1.4142 -1.4142 0]" << std::endl;
std::cout << "The projection of [0 3 3 0] is (mean of zero set) [" << proj4_3 << "]" << std::endl;
std::cout << "this should be approx [2.1213 2.1213 3.000]" << std::endl;
//Test for the eigen values for the test case precomputed using Matlab
std::cout << "" << std::endl;
if( proj3[0] < -1.54 && proj3[0] > -1.55 && proj4[1] < -2.31 && proj4[1] > -2.32 &&
proj3_2[1] < -1.414 && proj3_2[1] > -1.415 && proj4_2[2] < 3.01 && proj4_2[2] > 2.99 &&
proj3_3 == proj3_2 && proj4_3 == proj4_2)
{
std::cerr << "Test Passed" << std::endl;
return EXIT_SUCCESS;
}
else
{
std::cerr << "Test failed" << std::endl;
std::cerr << "The project is out of the range of Matlab precomputed values" << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
|