Hello listserve,<br><br>I have a question about the filter SpatialObjectToImageStatisticsCalculator,<br><a href="http://www.itk.org/Doxygen/html/classitk_1_1SpatialObjectToImageStatisticsCalculator.html">http://www.itk.org/Doxygen/html/classitk_1_1SpatialObjectToImageStatisticsCalculator.html</a> .<br>
<br>I was wondering about the GetCovarianceMatrix() function. If I call this function with a 2-D image mask spatial object it gives me back a 1 by 1 matrix whose only entry seems to be the variance of the pixels within my spatial object. But, this is just the trace of the covariance matrix and not a covariance matrix. I was wondering why the funciton is named the way it is, and how I would have to use it to get out a covariance matrix, and what would be the covarying?<br>
<br>Here is some example code to illustrate what I am talking about:<br><br><br>#include "itkImage.h"<br>#include <itkImageMaskSpatialObject.h><br>#include "itkSpatialObjectToImageStatisticsCalculator.h"<br>
#include "itkRandomImageSource.h"<br><br>int main( int argc, char *argv[])<br>{<br><br> <br> //Define image types <br> typedef itk::Image< float, 2 > FloatImageType;<br> typedef itk::Image< unsigned char, 2 > MaskImageType;<br>
<br> //Make random image <br> itk::Size<2> size;<br> size.Fill(100);<br> <br> itk::RandomImageSource<FloatImageType>::Pointer randomImageSource =<br> itk::RandomImageSource<FloatImageType>::New();<br>
randomImageSource->SetSize(size);<br> randomImageSource->SetNumberOfThreads(2);<br> randomImageSource->SetMax(1.0);<br> randomImageSource->SetMin(0.0);<br> double spacing[2];<br> spacing[0] = .5;<br> spacing[1] = 1.0;<br>
randomImageSource->SetSpacing(spacing);<br> randomImageSource->Update();<br> <br> //Make mask image <br> MaskImageType::Pointer maskImage = MaskImageType::New(); <br> maskImage->SetSpacing(randomImageSource->GetOutput()->GetSpacing());<br>
maskImage->SetOrigin(randomImageSource->GetOutput()->GetOrigin());<br> maskImage->SetRegions(randomImageSource->GetOutput()->GetLargestPossibleRegion());<br><br> maskImage->Allocate();<br> maskImage->FillBuffer(0);<br>
<br> <br> //Create a 20 by 20 mask region<br> for(int r = 0 ; r < 20; r++)<br> {<br> for(int c = 0; c < 20 ; c++)<br> {<br> MaskImageType::IndexType pixelIndex;<br> pixelIndex[0] = r;<br>
pixelIndex[1] = c;<br> <br> maskImage->SetPixel(pixelIndex, 255);<br> }<br> }<br><br> <br> //Create a spatial object to store the mask<br> typedef itk::ImageMaskSpatialObject<2> maskObjectType;<br>
maskObjectType::Pointer maskObject = maskObjectType::New();<br> maskObject->SetImage(maskImage);<br> maskObject->Update();<br> <br><br> ///////////////////////////////////////////////////////////////////// <br>
/////////////COMPUTE MEAN AND STANDARD DEVIATION INSIDE MASK///////// <br> ///////////////////////////////////////////////////////////////////// <br> <br> typedef itk::SpatialObjectToImageStatisticsCalculator<FloatImageType, maskObjectType > CalculatorType;<br>
<br> CalculatorType::Pointer calculator = CalculatorType::New();<br> <br> calculator->SetImage(randomImageSource->GetOutput());<br> calculator->SetSpatialObject( maskObject);<br> calculator->Update();<br>
std::cout << "Number of pixels inside maske are: " << calculator->GetNumberOfPixels() << std::endl;<br> std::cout << "Sample mean inside:" << std::endl << calculator->GetMean() << std::endl ;<br>
CalculatorType::MatrixType matrix;<br> matrix = calculator->GetCovarianceMatrix();<br> std::cout << "The variance of the pixels in the mask are: " << matrix(0,0) << std::endl;<br> std::cout << "Expected variance of uniform distribution on (0,1) is 1/12 or .0833 \n" << std::endl; <br>
<br> return 0;<br>}<br><br>