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
|
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkSpatialObjectToImageStatisticsCalculatorTest.cxx,v $
Language: C++
Date: $Date: 2005-05-27 21:03:17 $
Version: $Revision: 1.8 $
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
#include <iostream>
#include "itkImage.h"
#include "itkSpatialObjectToImageStatisticsCalculator.h"
#include "itkSpatialObjectToImageFilter.h"
#include "itkFloodFilledSpatialFunctionConditionalIterator.h"
#include "itkEllipseSpatialObject.h"
#include "itkImageSliceIteratorWithIndex.h"
int itkSpatialObjectToImageStatisticsCalculatorTest(int, char * [] )
{
typedef unsigned char PixelType;
typedef itk::Image<PixelType,2> ImageType;
typedef itk::EllipseSpatialObject<2> EllipseType;
typedef itk::FloodFilledSpatialFunctionConditionalIterator<ImageType,
EllipseType> IteratorType;
// Image Definition
ImageType::RegionType region;
ImageType::SizeType size;
size.Fill(50);
// Circle definition
EllipseType::Pointer ellipse = EllipseType::New();
ellipse->SetRadius(10);
EllipseType::VectorType offset;
offset.Fill(25);
ellipse->GetIndexToObjectTransform()->SetOffset(offset);
ellipse->ComputeObjectToParentTransform();
// Create a test image
typedef itk::SpatialObjectToImageFilter<EllipseType,ImageType> ImageFilterType;
ImageFilterType::Pointer filter = ImageFilterType::New();
filter->SetInput(ellipse);
filter->SetSize(size);
filter->SetInsideValue(255);
filter->Update();
ImageType::Pointer image = filter->GetOutput();
offset.Fill(25);
ellipse->GetIndexToObjectTransform()->SetOffset(offset);
ellipse->ComputeObjectToParentTransform();
typedef itk::SpatialObjectToImageStatisticsCalculator<ImageType,EllipseType> CalculatorType;
CalculatorType::Pointer calculator = CalculatorType::New();
calculator->SetImage(image);
calculator->SetSpatialObject(ellipse);
calculator->Update();
std::cout << " --- Ellipse and Image perfectly aligned --- " << std::endl;
std::cout << "Sample mean = " << calculator->GetMean() << std::endl ;
std::cout << "Sample covariance = " << calculator->GetCovarianceMatrix();
if(calculator->GetMean() != 255
|| calculator->GetCovarianceMatrix()[0][0] != 0
)
{
std::cout << "[FAILED]" << std::endl;
return EXIT_FAILURE;
}
std::cout << "[PASSED]" << std::endl;
offset.Fill(20);
ellipse->GetIndexToObjectTransform()->SetOffset(offset);
ellipse->ComputeObjectToParentTransform();
ellipse->Update();
calculator->Update();
std::cout << " --- Ellipse and Image mismatched left --- " << std::endl;
std::cout << "Sample mean = " << calculator->GetMean() << std::endl ;
std::cout << "Sample covariance = " << calculator->GetCovarianceMatrix();
if( (fabs(calculator->GetMean()[0]-140.0)>1.0)
|| (fabs(calculator->GetCovarianceMatrix()[0][0]-16141.0)>1.0)
)
{
std::cout << "[FAILED]" << std::endl;
return EXIT_FAILURE;
}
std::cout << "[PASSED]" << std::endl;
std::cout << " --- Ellipse and Image mismatched right --- " << std::endl;
offset.Fill(30);
ellipse->GetIndexToObjectTransform()->SetOffset(offset);
ellipse->ComputeObjectToParentTransform();
ellipse->Update();
calculator->Update();
std::cout << "Sample mean = " << calculator->GetMean() << std::endl ;
std::cout << "Sample covariance = " << calculator->GetCovarianceMatrix();
if( (fabs(calculator->GetMean()[0]-140.0)>1.0)
|| (fabs(calculator->GetCovarianceMatrix()[0][0]-16141.0)>1.0)
)
{
std::cout << "[FAILED]" << std::endl;
return EXIT_FAILURE;
}
std::cout << "[PASSED]" << std::endl;
std::cout << " --- Testing higher dimensionality --- " << std::endl;
// Create a new 3D image
typedef itk::Image<PixelType,3> Image3DType;
Image3DType::Pointer image3D = Image3DType::New();
typedef Image3DType::RegionType RegionType;
typedef Image3DType::SizeType SizeType;
typedef Image3DType::IndexType IndexType;
SizeType size3D;
size3D[0]=50;
size3D[1]=50;
size3D[2]=3;
IndexType start;
start.Fill( 0 );
RegionType region3D;
region3D.SetIndex( start );
region3D.SetSize( size3D );
image3D->SetRegions( region3D );
image3D->Allocate();
image3D->FillBuffer(255);
// Fill the image
std::cout << "Allocating image." << std::endl;
typedef itk::ImageSliceIteratorWithIndex< Image3DType > SliceIteratorType;
SliceIteratorType it( image3D, region3D );
it.GoToBegin();
it.SetFirstDirection( 0 ); // 0=x, 1=y, 2=z
it.SetSecondDirection( 1 ); // 0=x, 1=y, 2=z
unsigned int value = 0;
while( !it.IsAtEnd() )
{
while( !it.IsAtEndOfSlice() )
{
while( !it.IsAtEndOfLine() )
{
it.Set( value );
++it;
}
it.NextLine();
}
it.NextSlice();
value++;
}
std::cout << "Allocating spatial object." << std::endl;
typedef itk::EllipseSpatialObject<3> Ellipse3DType;
Ellipse3DType::Pointer ellipse3D = Ellipse3DType::New();
double radius[3];
radius[0] = 10;
radius[1] = 10;
radius[2] = 0;
ellipse3D->SetRadius(radius);
Ellipse3DType::VectorType offset3D;
offset3D.Fill(25);
offset3D[2]=0; // first slice
ellipse3D->GetIndexToObjectTransform()->SetOffset(offset3D);
ellipse3D->ComputeObjectToParentTransform();
// Create a new calculator with a sample size of 3
std::cout << "Updating calculator." << std::endl;
typedef itk::SpatialObjectToImageStatisticsCalculator<Image3DType,Ellipse3DType,3> Calculator3DType;
Calculator3DType::Pointer calculator3D = Calculator3DType::New();
calculator3D->SetImage(image3D);
calculator3D->SetSpatialObject(ellipse3D);
calculator3D->Update();
std::cout << "Sample mean = " << calculator3D->GetMean() << std::endl ;
std::cout << "Sample covariance = " << calculator3D->GetCovarianceMatrix();
if( (fabs(calculator3D->GetMean()[0]-0.0)>1.0)
|| (fabs(calculator3D->GetMean()[1]-1.0)>1.0)
|| (fabs(calculator3D->GetMean()[2]-2.0)>1.0)
)
{
std::cout << "[FAILED]" << std::endl;
return EXIT_FAILURE;
}
std::cout << "Number of pixels = " << calculator3D->GetNumberOfPixels() << std::endl;
if(calculator3D->GetNumberOfPixels() != 305)
{
std::cout << "[FAILED]" << std::endl;
return EXIT_FAILURE;
}
std::cout << "Sum = " << calculator3D->GetSum() << std::endl;
if(calculator3D->GetSum() != 915)
{
std::cout << "[FAILED]" << std::endl;
return EXIT_FAILURE;
}
std::cout << " [PASSED]" << std::endl;
return EXIT_SUCCESS;
}
|