[Insight-users] Fwd: Average of multiple image files
Kannan U V
kannanuv at cshl.edu
Fri Aug 24 16:44:58 EDT 2012
Hi David,
Here is the code I wrote to average randomly generated tiles. I get a
blank image (filled with zeros) as the averageImage. But the random images
are different. I think I am doing something wrong in
dividendImage->SetRequestedRegion (nullFilter->GetOutput());
but I not able to figure out how.
void createAverageImage () {
cout << "In createAverageImage()" << endl;
ImageType::SizeType outputSize;
outputSize[0] = 10;
outputSize[1] = 10;
itk::RandomImageSource<ImageType>::Pointer randomImageSource =
itk::RandomImageSource<ImageType>::New();
randomImageSource->SetSize(outputSize);
AddImageFilterType::Pointer addImageFilter = AddImageFilterType::New();
DivideImageFilterType::Pointer divideImageFilter =
DivideImageFilterType::New();
AverageImageWriterType::Pointer averageImageWriter =
AverageImageWriterType::New();
NullFilterType::Pointer nullFilter = NullFilterType::New();
ImageReaderType::Pointer reader = ImageReaderType::New();
CastFilterType::Pointer castFilter = CastFilterType::New();
AddImageFilterType::Pointer addFilter = AddImageFilterType::New();
AverageImageOutputFilterType::Pointer averageImageOutputFilterType =
AverageImageOutputFilterType::New();
SumImageType::Pointer divisorImage = SumImageType::New();
SumImageType::Pointer dividendImage = SumImageType::New();
// Set region size of dividend and divisor images
SumImageType::SizeType sumOutputSize;
sumOutputSize[0] = 10;
sumOutputSize[1] = 10;
SumImageType::IndexType start;
start[0] = 0;
start[1] = 0;
SumImageType::RegionType region;
region.SetIndex(start);
region.SetSize(sumOutputSize);
// Set divisor image to total number of images
cout << "Allocating divisorImage" << endl;
divisorImage->SetRegions(region);
divisorImage->Allocate();
divisorImage->FillBuffer(10.0);
// Set dividend image to zero
cout << "Allocating dividendImage" << endl;
dividendImage->SetRegions(region);
dividendImage->Allocate();
dividendImage->FillBuffer(0.0);
string *imFileName = new string("");
string outputImageDir("./imageFiles/");
for (int iTile = 0; iTile < 10; iTile++) {
randomImageSource->SetNumberOfThreads(iTile); // to produce non-random
results
//for (int iTile = 0; iTile < (int) imageFileName.size(); iTile++) {
cout << "Processing Image = " << iTile;
stringstream ss;
ss << iTile << ".tif";
string *imFileName = new string("");
imFileName->clear();
imFileName->append(outputImageDir);
//imFileName->append("/");
imFileName->append("FileName");
imFileName->append(ss.str().c_str());
cout << ", Image name : " << imFileName->c_str() << endl;
reader->SetFileName (imFileName->c_str());
castFilter->SetInput (randomImageSource->GetOutput());
addFilter->SetInput1 (castFilter->GetOutput());
addFilter->SetInput2 (dividendImage);
nullFilter->SetInput (addFilter->GetOutput());
dividendImage->SetRequestedRegion (nullFilter->GetOutput());
reader->Update();
addFilter->Update();
}
divideImageFilter->SetInput1(dividendImage);
divideImageFilter->SetInput2(divisorImage);
//averageImageWriter->SetInput(divideImageFilter->GetOutput());
averageImageOutputFilterType->SetInput (dividendImage);
//averageImageWriter->SetInput(averageImageOutputFilterType->GetOutput());
averageImageWriter->SetInput(dividendImage);
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();
nullFilter->Update();
}
On Mon, Aug 20, 2012 at 12:45 PM, David Doria <daviddoria at gmail.com> wrote:
> > 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.
>
> Most functions in ITK are virtual functions :) Can you explain the
> problem you having? Or better, provide the smallest compilable,
> self-contained example of the problem. That is, rather than reading
> images from files, generate some random images programmatically and
> then call your function with them.
>
> David
>
--
Thanks,
Kannan
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20120824/74cb76b9/attachment.htm>
More information about the Insight-users
mailing list