[Insight-users] Re: Exporting an array

Luis Ibanez luis.ibanez at kitware.com
Wed May 23 11:13:02 EDT 2007


Hi Frederic,


Thanks for letting us know that the method worked for you.


Please note that since now you are memcopying the image buffer
in to the memory that you allocated with IDL, you don't really
need to Mummify the buffer anymore.


You could simple get the buffer pointer straight from the
ITK image as:


   memcpy( dest_d, filter->GetOutput()->GetBufferPointer(),
           size[0]*size[1]*sizeof(InputPixelType));



The current call to SetContainerManageMemory(false) in your code
is producing a memory leak, because nobody is deleting the buffer
of the image that is the output of the filter.



BTW,
You may be interested to know that we are planning
to wrapping for IDL to ITK.

We will be happy to hear suggestions and to learn about what
a reasonable wish-list could be.


    Thanks


       Luis


=======================
Frédéric Stevens wrote:
> Hi again Luis,
> 
> I tried the a) method :
> 
>    InputImageType::PixelContainer *container;
>      container = filter->GetOutput()->GetPixelContainer();
>      container->SetContainerManageMemory(false);
>      
> memcpy(dest_d,container->GetImportPointer(),size[0]*size[1]*sizeof(InputPixelType));
> 
>     return dest;
> 
> It finally works. I don't why it wasn't working at the beginning, must 
> have made a mistake I didn't see. But now it works perfectly.
> 
> The b) method works perfectly too (wasn't working neither at the 
> beginning, I don't remember what I have changed though but it made both 
> works)
>     OutputImageType::Pointer outputimage = OutputImageType::New();
>     outputImage = filter->GetOutput();
>     
> outputImage->GetPixelContainer()->SetImportPointer(dest_d,numberOfPixels,false);
>     filter->GetOutput()->Allocate();
>     filter->Update();
> 
> Thank you Luis for your good advices. I really appreciate it.
> 
> 
> 
> On 5/23/07, *Luis Ibanez* <luis.ibanez at kitware.com 
> <mailto:luis.ibanez at kitware.com>> wrote:
> 
> 
>     Hi Frederic,
> 
> 
>     A couple of questions:
> 
> 
>     1)   Are you calling ITK as a DLL ?
> 
> 
> 
>     2)   The array dest_d that you are allocating
>           with  IDL_MakeTempArray() is being overrided
>           by the assignment of
> 
>             dest_d = container->GetImportPointer();
> 
>           What you probably want to do is:
> 
> 
>            a) use memcpy() to copy the content of
> 
>                  container->GetImportPointer();
>                  into the allocated array dest_d.
> 
>               and in this case, you don't really need
>               to Mummify the buffer, because by the time
>               the filter will be destroyed, you will have
>               already copied the image data into your
>               dest_d array.
> 
> 
>                      OR (exclusive OR)
> 
> 
>            b) you could use the dest_d array to be passed
>               as the "working memory" for the filter:
> 
>            outputImage = filter->GetOutput();
>            outputImage->GetPixelContainer()->SetImportPointer(
>                     dest_d,
>                     totalNumberOfPixels,
>                     false);
> 
>            filter->GetOutput()->Allocate();
>            filter->Update();
> 
> 
> 
> 
>     Regards,
> 
> 
>         Luis
> 
> 
> 
>     -----------------------
>     Frédéric Stevens wrote:
>      > Hi,
>      >
>      > Sorry for making a third topic but I wanted to say that I also
>     tried the
>      > method written by Luis Ibanez here :
>      > http://www.itk.org/CourseWare/Training/GettingStarted-V.pdf
>      > <http://www.itk.org/CourseWare/Training/GettingStarted-V.pdf
>     <http://www.itk.org/CourseWare/Training/GettingStarted-V.pdf>>
>      > I'm using the "Mummifying the buffer" method :
>      >
>      >     IDL_VPTR dest;
>      >     InputPixelType *dest_d;
>      >     dest_d = (InputPixelType *)
>      > IDL_MakeTempArray(IDL_TYP_INT,src-> value.arr->n_dim,src->
>      > value.arr->dim,IDL_ARR_INI_INDEX,&dest);
>      >
>      > /* Mummifying method */
>      >
>      >      InputImageType::PixelContainer *container;
>      >      container = filter->GetOutput()->GetPixelContainer();
>      >      container->SetContainerManageMemory(false);
>      >      dest_d = container->GetImportPointer();
>      >
>      >
>      > But once again, the dest_d doesn't contain the picture of the
>     filter's
>      > output... I really don't know how to do...
>      >
>      > Thanks,
>      >
>      > Frédéric.
>      >
>      > On 5/22/07, *Frédéric Stevens* <frederic.stevens at gmail.com
>     <mailto:frederic.stevens at gmail.com>
>      > <mailto:frederic.stevens at gmail.com
>     <mailto:frederic.stevens at gmail.com>>> 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>
>      >     <mailto: 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 <mailto:Insight-users at itk.org>
>      > http://www.itk.org/mailman/listinfo/insight-users
> 
> 


More information about the Insight-users mailing list