[Insight-users] SpatialObjectToImageStatisticsCalculator standard deviation

Nick Rubert ncrubert at gmail.com
Sat Oct 8 19:12:40 EDT 2011


Hello listserve,

I have a question about the filter SpatialObjectToImageStatisticsCalculator,
http://www.itk.org/Doxygen/html/classitk_1_1SpatialObjectToImageStatisticsCalculator.html.

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?

Here is some example code to illustrate what I am talking about:


#include "itkImage.h"
#include <itkImageMaskSpatialObject.h>
#include "itkSpatialObjectToImageStatisticsCalculator.h"
#include "itkRandomImageSource.h"

int main( int argc, char *argv[])
{


  //Define image types
  typedef itk::Image< float, 2 >  FloatImageType;
  typedef itk::Image< unsigned char, 2 > MaskImageType;

  //Make random image
  itk::Size<2> size;
  size.Fill(100);

  itk::RandomImageSource<FloatImageType>::Pointer randomImageSource =
    itk::RandomImageSource<FloatImageType>::New();
  randomImageSource->SetSize(size);
  randomImageSource->SetNumberOfThreads(2);
  randomImageSource->SetMax(1.0);
  randomImageSource->SetMin(0.0);
  double  spacing[2];
  spacing[0] = .5;
  spacing[1] = 1.0;
  randomImageSource->SetSpacing(spacing);
  randomImageSource->Update();

  //Make mask image
  MaskImageType::Pointer maskImage = MaskImageType::New();
  maskImage->SetSpacing(randomImageSource->GetOutput()->GetSpacing());
  maskImage->SetOrigin(randomImageSource->GetOutput()->GetOrigin());

maskImage->SetRegions(randomImageSource->GetOutput()->GetLargestPossibleRegion());

  maskImage->Allocate();
  maskImage->FillBuffer(0);


  //Create a 20 by 20 mask region
  for(int r = 0 ; r < 20; r++)
       {
         for(int c = 0; c < 20 ; c++)
         {
                  MaskImageType::IndexType pixelIndex;
                  pixelIndex[0] = r;
                  pixelIndex[1] = c;

                  maskImage->SetPixel(pixelIndex, 255);
        }
  }


  //Create a spatial object to store the mask
  typedef itk::ImageMaskSpatialObject<2> maskObjectType;
  maskObjectType::Pointer maskObject = maskObjectType::New();
  maskObject->SetImage(maskImage);
  maskObject->Update();


  /////////////////////////////////////////////////////////////////////
  /////////////COMPUTE MEAN AND STANDARD DEVIATION INSIDE MASK/////////
  /////////////////////////////////////////////////////////////////////

  typedef itk::SpatialObjectToImageStatisticsCalculator<FloatImageType,
maskObjectType > CalculatorType;

  CalculatorType::Pointer calculator = CalculatorType::New();

  calculator->SetImage(randomImageSource->GetOutput());
  calculator->SetSpatialObject( maskObject);
  calculator->Update();
  std::cout << "Number of pixels inside maske are: " <<
calculator->GetNumberOfPixels() << std::endl;
  std::cout << "Sample mean inside:" << std::endl <<  calculator->GetMean()
<< std::endl ;
  CalculatorType::MatrixType matrix;
  matrix =  calculator->GetCovarianceMatrix();
  std::cout << "The variance of the pixels in the mask are: " << matrix(0,0)
<< std::endl;
  std::cout << "Expected variance of uniform distribution on (0,1) is 1/12
or .0833 \n" << std::endl;

  return 0;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20111008/0d88eb26/attachment.htm>


More information about the Insight-users mailing list