[Insight-users] Fwd: Average of multiple image files

Kannan U V kannanuv at cshl.edu
Mon Aug 20 12:38:43 EDT 2012


---------- Forwarded message ----------
From: Kannan U V <kannanuv at cshl.edu>
Date: Mon, Aug 20, 2012 at 12:20 PM
Subject: Average of multiple image files
To: insight-users at itk.org


Hi,
I want to create an average image of multiple files. I am reading all files
and create a dividend image that is a sum of all images and then use the
divide image filter and divide the dividend float image with a constant
image having pixel values equal to the number of images summed up. I am
having issues adding multiple images to the dividend image. Below is the
function I have written. I see that SetRequestedRegion is a virtual
function.

nt tvMosaicParam::createAverageTile() {
  typedef itk::Image<TilePixelType, 2> TileImageType;
  typedef itk::Image<AverageTilePixelType, 2> AverageTileType;
  typedef itk::Image<TilePixelType, 2> AverageImageOutputType;
  typedef itk::ImageFileWriter<AverageTileType> AverageImageWriterType;
  typedef itk::CastImageFilter<TileImageType, AverageTileType>
CastFilterType;
  typedef itk::CastImageFilter<AverageTileType, AverageTileType>
NullFilterType;
  typedef itk::CastImageFilter<AverageTileType, TileImageType>
AverageImageOutputFilterType;
  typedef itk::ImageFileReader<TileImageType> TileImageReaderType;
  typedef itk::AddImageFilter<AverageTileType, AverageTileType,
AverageTileType> AddImageFilterType;
  typedef itk::DivideImageFilter <AverageTileType, AverageTileType,
TileImageType> DivideImageFilterType;

  TileImageType::SizeType outputSize;
  outputSize[0] = croppedTileSizeX;
  outputSize[1] = croppedTileSizeY;

  AddImageFilterType::Pointer addImageFilter = AddImageFilterType::New();
  DivideImageFilterType::Pointer divideImageFilter =
DivideImageFilterType::New();
  AverageImageWriterType::Pointer averageImageWriter =
AverageImageWriterType::New();

  // Setting up blank image as average
  NullFilterType::Pointer nullFilter = NullFilterType::New();
  TileImageReaderType::Pointer reader = TileImageReaderType::New();
  CastFilterType::Pointer castFilter = CastFilterType::New();
  AddImageFilterType::Pointer addFilter = AddImageFilterType::New();
  AverageImageOutputFilterType::Pointer averageImageOutputFilterType =
AverageImageOutputFilterType::New();
  AverageTileType::Pointer divisorImage = AverageTileType::New();
  AverageTileType::Pointer dividendImage = AverageTileType::New();

  CreateConstantImage (divisorImage, (double)imageFileName.size());
  CreateConstantImage (dividendImage, 0.0);
  string *imFileName = new string("");
  string outputImageDir(tvOutputDirectory);
  for (int iTile = 0; iTile < 10; iTile++) {
  //for (int iTile = 0; iTile < (int) imageFileName.size(); iTile++) {
    cout << "Processing iTile = " << iTile;
    imFileName->clear();
    imFileName->append(outputImageDir);
    imFileName->append("/");
    imFileName->append(imageFileName.at(iTile).c_str());
    cout << ", Image name : " <<  imFileName->c_str() << endl;
    reader->SetFileName (imFileName->c_str());
    castFilter->SetInput (reader->GetOutput());
    addFilter->SetInput1 (castFilter->GetOutput());
    addFilter->SetInput2 (dividendImage);
    nullFilter->SetInput (addFilter->GetOutput());
    dividendImage->SetRequestedRegion (nullFilter->GetOutput());
    reader->Update();
    addFilter->Update();
    nullFilter->Update();
  }

  //divideImageFilter->SetInput1(dividendImage);
  //divideImageFilter->SetInput2(divisorImage);

  //averageImageWriter->SetInput(divideImageFilter->GetOutput());
  averageImageOutputFilterType->SetInput (dividendImage);
  averageImageWriter->SetInput(averageImageOutputFilterType->GetOutput());
  //averageImageWriter->SetInput();
  string *averageImageFileName = new string("");
  averageImageFileName->clear();
  averageImageFileName->append(outputImageDir);
  averageImageFileName->append("/");
  averageImageFileName->append("AverageImage.img");
  averageImageWriter->SetFileName (averageImageFileName->c_str());
  cout << "Average Image name : " <<  averageImageFileName->c_str() << endl;
  averageImageWriter->Update();

  return 0;
}

How do I proceed? Thanks in advance for your help.

-- 
Thanks,
Kannan



-- 
Thanks,
Kannan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20120820/b450a34d/attachment.htm>


More information about the Insight-users mailing list