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

robert tamburo robert.tamburo at gmail.com
Fri Aug 24 17:05:52 EDT 2012


I'm not seeing where you set dividendImage to anything but 0. Looks like
you need dividendImage = addFilter->GetOutput().

Alternatively, you could add N images together with NaryAddImageFilter (
http://www.itk.org/Doxygen/html/classitk_1_1NaryAddImageFilter.html), then
divide by N with ShiftScaleImageFilter (
http://www.itk.org/Doxygen/html/itkShiftScaleImageFilter_8h_source.html --
shift = 0, scale = 1/N).

On Fri, Aug 24, 2012 at 4:44 PM, Kannan U V <kannanuv at cshl.edu> wrote:

> 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
>
> _____________________________________
> Powered by www.kitware.com
>
> Visit other Kitware open-source projects at
> http://www.kitware.com/opensource/opensource.html
>
> Kitware offers ITK Training Courses, for more information visit:
> http://www.kitware.com/products/protraining.php
>
> Please keep messages on-topic and check the ITK FAQ at:
> http://www.itk.org/Wiki/ITK_FAQ
>
> Follow this link to subscribe/unsubscribe:
> http://www.itk.org/mailman/listinfo/insight-users
>
>


-- 
robert
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20120824/3f7427ac/attachment.htm>


More information about the Insight-users mailing list