[Insight-users] Memory problem

Melanie Uks meluks2010 at googlemail.com
Wed Apr 13 04:47:34 EDT 2011


Hi all,

I tried to filter a 3D image (300x300x512) with the CannyEdgeFilter. I had
the problem that I could not allocate the memory for the filtering. Anyway I
don't like the CannyEdgeFilter Implementation as one has to guess which
values to take for the thresholding before the gradientmagnitude image
exists... Therefore I wanted to write my own edge detection routine.

The steps:
I generate a image (size: 300x300x512)
I generate a vector image (size: 300x300x512, vector dimension:3)
a) I split the image, process the imagepart with
GradientRecursiveGaussianImageFilter
b) I paste the processed imagepart in the vector image with the
PasteImageFilter
I repeat a) and b) until the whole image is processed

Now the first question: Why can't I process the complete image. I have to
split the image and get a result... This is the error I get:
*itk::ExceptionObject (0151C728)
Location: "class itk::CovariantVector<float,3> *__thiscall
itk::ImportImageConta
iner<unsigned long,class itk::CovariantVector<float,3>
>::AllocateElements(unsig
ned long) const"
File: h:\itk\insighttoolkit-3.16.0\code\common\itkImportImageContainer.txx
Line: 188
Description: Failed to allocate memory for image.*

Ok, if I accept that I have to split the image, I have a second question: I
was able to run my example programm (code at the end of this mail). Then I
copied the code into my actual program. It is a programm with several itk
functions and Qt GUI. The only difference is that the image is saved as
global variable. I was not able to run the processing. I always had the
memory error. Why???

Here now the code for the example processing:

edgetest.cxx

#include "conio.h"

#include "itkImageFileWriter.h"
#include "itkImageFileReader.h"
#include "itkRegionOfInterestImageFilter.h"
#include "itkGradientRecursiveGaussianImageFilter.h"
#include "itkGradientToMagnitudeImageFilter.h"
#include "itkPasteImageFilter.h"
#include "itkImage.h"

int main(int argc, char* argv[])
{
   // Verify number of parameters in command line
  // if( argc < 3 )
    // {
    // std::cerr << "Usage: " << std::endl;
    // std::cerr << argv[0] << " inputImageFile  outputVectorImageFile " <<
std::endl;
    // return EXIT_FAILURE;
    // }
  typedef float   PixelType;
  typedef float   ComponentType;
  static const unsigned int Dimension = 3;
  typedef itk::Image< PixelType, Dimension > ImageType;
  typedef itk::CovariantVector< ComponentType,
                                Dimension  >      OutputPixelType;
  typedef itk::Image< OutputPixelType, Dimension >    OutputImageType;


  ImageType::Pointer image = ImageType::New();

  ImageType::IndexType start;
  for(int i = 0; i < Dimension; i++)
    start[i]=0;

  ImageType::SizeType size;
  size[0] = 300;
  size[1] = 300;
  size[2] = 512;

  ImageType::RegionType region;
  region.SetSize(size);
  region.SetIndex(start);

  ImageType::SpacingType spacing;
  spacing[0] = 20;
  spacing[1] = 20;
  spacing[2] = 4.00493;

  image->SetRegions(region);
  image->SetSpacing(spacing);
  image->Allocate();

  std::cout << region << std::endl;

  OutputImageType::Pointer vec_image = OutputImageType::New();
  OutputImageType::RegionType vecregion;

  OutputImageType::SizeType vecsize;        //Size
  vecsize[0] = (image->GetLargestPossibleRegion().GetSize())[0];
  vecsize[1] = (image->GetLargestPossibleRegion().GetSize())[1];
  vecsize[2] = (image->GetLargestPossibleRegion().GetSize())[2];
  std::cout<<"size0: "<< vecsize[0]<< " size1: "<< vecsize[1] << " size2: "
<< vecsize[2] <<std::endl;
  vecregion.SetSize( vecsize );

  OutputImageType::IndexType vecstart;        //Start
  vecstart[0] = (image->GetOrigin())[0];
  vecstart[1] = (image->GetOrigin())[1];
  vecstart[2] = (image->GetOrigin())[2];
  std::cout<<" start0: "<< vecstart[0]<< " start1: "<< vecstart[1] << "
start2: " << vecstart[2] <<std::endl;
  vecregion.SetIndex( vecstart );

  vec_image->SetRegions(vecregion);
  vec_image->SetSpacing(image->GetSpacing());
  vec_image->Allocate();

    // The image buffer is initialized to a particular value
  OutputImageType::PixelType  initialValue;

  // A vector can initialize all its components to the
  // same value by using the Fill() method.
  initialValue.Fill( 0.0 );

  // Now the image buffer can be initialized with this
  // vector value.
  vec_image->FillBuffer( initialValue );
  std::cout<< "Allocate" << std::endl;

  typedef itk::RegionOfInterestImageFilter< ImageType, ImageType >
ROIFilterType;
  ROIFilterType::Pointer roifilter = ROIFilterType::New();

  // Number of Regions
  int splitcnt_x = 2;
  int splitcnt_y = 2;
  int overlap = 15;

  int stepcnt_x = (int) (size[0]*1.0/splitcnt_x + 0.5);
  int stepcnt_y = (int) (size[1]*1.0/splitcnt_y + 0.5);


  ImageType::IndexType roistart;
  roistart[2]=0;
  ImageType::SizeType roisize;
  roisize[2]=512;
  ImageType::RegionType roiregion;


  for (int cnt_x = 0; cnt_x < splitcnt_x; cnt_x++)
  {
    for (int cnt_y = 0; cnt_y < splitcnt_y; cnt_y++)
    {
        roistart[0]= cnt_x*stepcnt_x - overlap;
        roistart[1]= cnt_y*stepcnt_y - overlap;
        if(cnt_x == 0)
        {
            roistart[0] = 0;
            roisize[0] = stepcnt_x + overlap;
        }
        else
        {
            roisize[0] = stepcnt_x + 2*overlap;
        }
        if(roisize[0]+roistart[0] > size[0])
        {
            roisize[0] = size[0]-roistart[0];
        }
        if(cnt_y == 0)
        {
            roistart[1] = 0;
            roisize[1] = stepcnt_y + overlap;
        }
        else
        {
            roisize[1] = stepcnt_y + 2*overlap;
        }
        if(roisize[1]+roistart[1] > size[1])
        {
            roisize[1] = size[1]-roistart[1];
        }


        roiregion.SetSize(roisize);
        roiregion.SetIndex(roistart);

        std::cout << "cnt_x: " << cnt_x << " cnt_y: " << cnt_y << std::endl;

        std::cout << roiregion << std::endl;

        std::cout << "ROI region inside image region is " <<
region.IsInside(roiregion) << std::endl;

        roifilter->SetRegionOfInterest(roiregion);
        roifilter->SetInput(image);

        //Filter class is instantiated
        typedef itk::GradientRecursiveGaussianImageFilter<ImageType,
OutputImageType> GradFilterType;

        GradFilterType::Pointer gradfilter = GradFilterType::New();

        //sigma is specified in millimeters
        gradfilter->SetSigma( 1.5 );

        //  processing pipeline:
        gradfilter->SetInput(roifilter->GetOutput());


        typedef itk::PasteImageFilter <OutputImageType, OutputImageType >
PasteImageFilterType;
        // The SetDestinationIndex() method prescribes where in the first
        // input to start pasting data from the second input.
        // The SetSourceRegion method prescribes the section of the second
        // image to paste into the first.

        OutputImageType::IndexType destinationIndex;
        destinationIndex[0] = cnt_x*stepcnt_x;
        destinationIndex[1] = cnt_y*stepcnt_y;
        destinationIndex[2] = 0;

        PasteImageFilterType::Pointer pasteFilter =
PasteImageFilterType::New ();
        pasteFilter->SetSourceImage(gradfilter->GetOutput());
        pasteFilter->SetDestinationImage(vec_image);

        OutputImageType::RegionType pasteregion;
        OutputImageType::IndexType pastestart;
        pastestart[2]=0;
        OutputImageType::SizeType pastesize;
        pastesize[2]=512;

        pastestart[0]= overlap;
        pastestart[1]= overlap;
        pastesize[0] = stepcnt_x;
        pastesize[1] = stepcnt_y;

        if(cnt_x == 0)
        {
            pastestart[0] = 0;
        }
        if(cnt_y == 0)
        {
            pastestart[1] = 0;
        }

        pasteregion.SetIndex(pastestart);
        pasteregion.SetSize(pastesize);

        pasteFilter->SetSourceRegion(pasteregion);
        pasteFilter->SetDestinationIndex(destinationIndex);

        try
        {
            pasteFilter->Update();
        }
        catch( itk::ExceptionObject & err )
        {
            std::cerr << "ExceptionObject caught !" << std::endl;
            std::cerr << err << std::endl;
            return EXIT_SUCCESS;
        }
    }
    typedef itk::GradientToMagnitudeImageFilter< OutputImageType, ImageType
> MagFilterType;
    MagFilterType::Pointer magfilter = MagFilterType::New();

    magfilter->SetInput(vec_image);

    try
    {
        magfilter->Update();
        image = magfilter->GetOutput();
    }
    catch( itk::ExceptionObject & err )
    {
        std::cerr << "ExceptionObject caught !" << std::endl;
        std::cerr << err << std::endl;
        return EXIT_SUCCESS;
    }

  }

  getch();
  return EXIT_SUCCESS;
}
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20110413/56e2b5a6/attachment-0001.htm>


More information about the Insight-users mailing list