[Insight-users] Re: Exporting an array

Luis Ibanez luis.ibanez at kitware.com
Wed May 23 09:21:06 EDT 2007


Hi Frederic,

Thanks for your detailed description of the problem.

To put it short:

     The ImportImageFilter is not what you need to
     use in order to pass an image back to another
     system.


Instead you should explore the two following options:


    1) Mummify the buffer of the output filter

  OR

    2) Provide the memory buffer to the output filter
       so when it computes the output image, it is
       already stored in the buffer that you provided.



For doing (1) the process is:

    ImageType::PixelContainer * container;
    container = filter->GetOutput()->GetPixelContainer();
    container->SetContainerManageMemory( false );
    ImageType::PixelType * buffer = container->GetImportPointer();

    Then you can pass that buffer to your application,
    and it will be the responsibility of your application
    to delete that memory buffer.



The process for (2) would be:

   ImageType::RegionTyperegion;
   region.SetSize( size );
   region.SetIndex( start );
   filter->GetOutput()->SetRegions( region);

   PixelType* pixelData= static_cast<PixelType* >(buffer);

   const boolfilterWillDeleteTheInputBuffer= false;

   const unsigned inttotalNumberOfPixels= nx * ny;

   filter->GetOutput()->GetPixelContainer()->SetImportPointer(
     pixelData, totalNumberOfPixels, filterWillDeleteTheInputBuffer);

   filter->GetOutput()->Allocate();
   filter->Update();


   In this case, the output image will be stored right away
   in the memory that you provided.


Note that if you are calling ITK as a DLL, then your only option
is (2), because memory allocation and deletion cannot cross DLL
boundaries. In other words, memory allocated in a DLL cannot be
deleted outside it, and the other way around.


    Regards,


       Luis


-----------------------
Frédéric Stevens wrote:
> I will give you more details about how I did.
> It is a function with (int argc, IDL_VPTR argv[]) as arguments
> First argument is an array passed thanks to IDL
> At the moment, I'm working on 16 bits DICOM (short type) with size : 
> 512*512 and using them works fine
> All the problem I have is to return the array modified back to IDL, here 
> is my code :
> 
> 
> typedef  short InputPixelType; 
> typedef  short OutputPixelType;
> const unsigned int Dimension = 2;
> typedef itk::Image< InputPixelType,  Dimension >   InputImageType;
> typedef itk::Image< OutputPixelType, Dimension >   OutputImageType;
> 
> IDL_VPTR src,dest;   // variables to import / export the picture from / 
> to IDL
> InputPixelType *src_d,*dest_d;
> src = argv[0];   // array 512*512
> src_d = (InputPixelType *)src->value.s.arr->data; // way to get the data 
> from the IDL variable
> 
> 
> // So now I'm using the ImportImageFilter using this array as Input and 
> it works perfectly fine
> 
> typedef itk::ImportImageFilter< InputPixelType, Dimension >   
> ImportFilterType;
> ImportFilterType::Pointer importFilter = ImportFilterType::New(); 
> ImportFilterType::SizeType  size;
> size[0]  = 512;  // size along X
> size[1]  = 512;  // size along Y
> ImportFilterType::IndexType start;
> start.Fill( 0 );
> ImportFilterType::RegionType region;
> region.SetIndex( start );
> region.SetSize(  size  );
> importFilter->SetRegion( region );
> double origin[ Dimension ];
> origin[0] = 0.0;    // X coordinate
> origin[1] = 0.0;    // Y coordinate
> importFilter->SetOrigin( origin );
> double spacing[ Dimension ];
> spacing[0] = 0.21484;    // along X direction
> spacing[1] = 0.21484;    // along Y direction
> importFilter->SetSpacing( spacing );
> const unsigned int numberOfPixels =  size[0] * size[1];
> const bool importImageFilterWillOwnTheBuffer = false;
> importFilter->SetImportPointer( (InputPixelType *)src_d, 
> numberOfPixels,importImageFilterWillOwnTheBuffer );
> 
> // Just for test I was using a binarythresholdfilter and it is working 
> with it
> 
> typedef itk::BinaryThresholdImageFilter< InputImageType, OutputImageType 
>  >  FilterType;
> FilterType::Pointer filter = FilterType::New();
> const OutputPixelType outsideValue = 0;
> const OutputPixelType insideValue  = 255;
> filter->SetOutsideValue( outsideValue );
> filter->SetInsideValue( insideValue );
> const InputPixelType lowerThreshold = 0;
> const InputPixelType upperThreshold = 500;   
> filter->SetLowerThreshold( lowerThreshold );
> filter->SetUpperThreshold( upperThreshold );
> filter->SetInput( importFilter->GetOutput() );
> 
> // Okay so until now, everything is fine. I can say it works because I 
> used a FileWriter and checked the picture after passing in the filter 
> with :
> 
> typedef itk::ImageFileWriter< OutputImageType >  WriterType;
> WriterType::Pointer writer = WriterType::New();
> writer->SetInput( filter->GetOutput() );
> writer->SetFileName( "e:\\test.dcm" );
> 
> try
> {
>     writer->Update();
> }
> catch (itk::ExceptionObject & e)
> {
>     sprintf(statusbuffer, "Error");
>     IDL_Message(IDL_M_NAMED_GENERIC,IDL_MSG_INFO, statusbuffer);
>     return src;
>  }
> 
> // Now is the part where I try to export it back to IDL
> 
> I tried both with ImportImageContainer without succeeding :
> 
> typedef itk::ImportImageContainer<InputPixelType, InputPixelType >  
> ImportContainerType;
> ImportContainerType::Pointer importFilter2 = ImportContainerType::New(); 
> importFilter2->SetImportPointer( (InputPixelType *)filter->GetOutput(), 
> numberOfPixels,false);
> 
> dest_d = (InputPixelType *) 
> IDL_MakeTempArray(IDL_TYP_INT,dest->value.arr->n_dim,dest->value.arr->dim,IDL_ARR_INI_INDEX,&paf);  
> // this allows to create an array with the same dimensions. each element 
> of the array is set to the value of its index.  (it works I get an array 
> with the right dimensions and with the values back)
> 
> dest_d = (InputPixelType *)importFilter2->GetBufferPointer();  // I try 
> to get the array from the binary filter
> 
> return dest;  // return the IDL_VPTR variable to IDL containing the 
> array.  The result in IDL is the array with each element is set to the 
> value of its index as if the GetBufferPointer didn't have any effect on 
> the variable. I tried also with the GetImportPointer but the result is 
> the same.
> 
> I tried in a similar way with the ImportImageFilter but the result was 
> still the same.
> 
> I hope that you understand better my problem like this and would really 
> be glad to be helped because I have tried for a long time studying the 
> problem without succeeding.
> 
> Regards,
> 
> Frédéric
> 
> 
> 
> On 5/21/07, *Frédéric Stevens* <frederic.stevens at gmail.com 
> <mailto:frederic.stevens at gmail.com>> wrote:
> 
>     Hi,
> 
>     I have successfully imported an array using ImportImageFilter, used
>     some filters on it and I can write it on the hard disk using
>     ImageFileWriter. Instead of creating a new file, I would like to
>     return this array in the same way as I imported it. (I am using an
>     image imported from IDL and I would like to export it back with the
>     filters applied).
>     Is there a function that allows to do this operation  ? 
> 
>     Many thanks,
> 
>     Frédéric
> 
> 
> 
> ------------------------------------------------------------------------
> 
> _______________________________________________
> Insight-users mailing list
> Insight-users at itk.org
> http://www.itk.org/mailman/listinfo/insight-users


More information about the Insight-users mailing list