[Insight-users] pointer to image data
barbababa
tonimuusimaki at gmail.com
Wed Jul 20 03:42:53 EDT 2011
HI!
I cannot really use imagefilereader here because this is code for external
application.
The input to ITK from this external application works and goes like this:
#include "dmtoitk2.h"
ItkImage2::Pointer dmtoitk2( DM::Image input )
{
typedef itk::ImportImageFilter< PixelType, 2> ImportFilterType;
ImportFilterType::Pointer importFilter = ImportFilterType::New();
ImportFilterType::SizeType size;
size[0] = input.GetDimensionSize( 0 );
size[1] = input.GetDimensionSize( 1 );
ImportFilterType::IndexType start;
start.Fill( 0 );
ImportFilterType::RegionType region;
region.SetIndex( start );
region.SetSize( size );
importFilter->SetRegion( region );
double origin[ 2 ];
origin[0] = 0.0; // X coordinate
origin[1] = 0.0; // X coordinate
importFilter->SetOrigin( origin );
double spacing[ 2];
spacing[0] = 1.0; // along X direction
spacing[1] = 1.0; // along X direction
importFilter->SetSpacing( spacing );
const unsigned int numberOfPixels = size[0]*size[1] ;
const bool importImageFilterWillOwnTheBuffer = false;
PixelType *input_data ;
if ( input.GetDataType() != ImageData::REAL4_DATA )
{
DM::Image input1;
input1 = input.Clone();
input1.ChangeDataType( ImageData::REAL4_DATA );
PlugIn::ImageDataLocker input1l( input1,
PlugIn::ImageDataLocker::lock_data_CONTIGUOUS );
input_data = (PixelType *)( input1l.get_image_data().get_data() );
}
else
{
PlugIn::ImageDataLocker inputl( input,
PlugIn::ImageDataLocker::lock_data_CONTIGUOUS );
input_data = (PixelType *) ( inputl.get_image_data().get_data() );
}
importFilter->SetImportPointer(input_data, numberOfPixels,
importImageFilterWillOwnTheBuffer );
importFilter->Update();
ItkImage2::Pointer output = importFilter->GetOutput();
output->DisconnectPipeline();
return output;
}
The output from ITK does not work and currently is something like:
typedef float PixelTypereal2;
#include "itktodm2.h"
DM::Image itktodm2( ItkImagereal2 * input)
{
ItkImagereal2::RegionType region = input->GetBufferedRegion();
ItkImagereal2::SizeType size = region.GetSize();
DM::Image output = DM::NewImage( ( std::string("gaussian of ")).c_str()
, ImageData::REAL4_DATA
, size[0],size[1]);
const unsigned int numberOfPixels = size[0]*size[1];
//const unsigned int numberOfBytes = numberOfPixels *
sizeof(PixelTypereal2);
//const PixelTypereal2 * itkBuffer = input->GetBufferPointer();
PlugIn::ImageDataLocker outputl( output,
PlugIn::ImageDataLocker::lock_data_CONTIGUOUS );
// memcpy( outputl.get_image_data().get_data(), itkBuffer, numberOfBytes );
float *output_data = reinterpret_cast< float*>
(outputl.get_image_data().get_data());
// input is the output of some ITK filter
output_data = (input->GetBufferPointer()) ;
outputl.MarkDataChanged();
return output;
}
This works with memcpy though!
Is it possible that the input image goes out of scope and there is some
internal itk mechanism of
releasing the allocated memory of the input image? It would explain why the
output is zero everywhere!
Thanks!
--
View this message in context: http://itk-insight-users.2283740.n2.nabble.com/pointer-to-image-data-tp6596336p6601687.html
Sent from the ITK Insight Users mailing list archive at Nabble.com.
More information about the Insight-users
mailing list