[ITK-users] image->GetBufferPointer() and opposite way for 3d images
alexfd7
alexfd7 at gmail.com
Thu May 5 10:46:05 EDT 2016
Hello everyone, I have a problem, someone help me?
I'm getting the image buffer. So I want to do the opposite way, import the
buffer (pixelData) back to the image object, I am using importImageFilter
filter to have the original image again. 2D images works very well, however
with 3d images (.nrrd) i'm not getting good result, I get a darker resulting
image than the original.
I thank the attention and the available space for me to post my question
float* pixelData; /**< Pointer to image data */
typedef float InputPixelType;
typedef float OutputPixelType;
typedef itk::Image<InputPixelType, 3> InputImageType;
typedef itk::Image<OutputPixelType, 3> OutputImageType;
typedef itk::ImageFileReader<InputImageType> ReaderType;
typedef itk::ImageFileWriter<OutputImageType> WriterType;
typedef itk::ImportImageFilter< float, 3 > ImportFilterType;
ImportFilterType::Pointer importFilter = ImportFilterType::New();
typename ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName( inputVolume.c_str() );
reader->Update();
typename InputImageType::Pointer image = reader->GetOutput();
typename InputImageType::RegionType region =
image->GetLargestPossibleRegion();
typename InputImageType::SizeType size = region.GetSize();
int sizePixel = (size[0]*size[1]*size[2]);
pixelData = (float*)malloc(size[0]*size[1]*size[2] * sizeof(float));
memcpy( pixelData, image->GetBufferPointer(),
size[0]*size[1]*size[2]*sizeof(float));
ImportFilterType::SizeType sizeImport;
sizeImport[0] = size[0]; // size along X
sizeImport[1] = size[1]; // size along Y
sizeImport[2] = size[2]; // size along Y
ImportFilterType::IndexType start;
start.Fill( 0 );
ImportFilterType::RegionType regionImport;
regionImport.SetIndex( start );
regionImport.SetSize( sizeImport );
importFilter->SetRegion( regionImport );
const itk::SpacePrecisionType origin[ 3 ] = { 0.0, 0.0, 0.0};
importFilter->SetOrigin( origin );
const itk::SpacePrecisionType spacing[ 3 ] = { 1.0, 1.0, 1.0};
importFilter->SetSpacing( spacing );
const bool importImageFilterWillOwnTheBuffer = true;
importFilter->SetImportPointer( pixelData, sizePixel,
importImageFilterWillOwnTheBuffer );
importFilter->Update();
typename WriterType::Pointer writer = WriterType::New();
writer->SetFileName( outputVolume.c_str() );
writer->SetInput( importFilter->GetOutput() );
writer->SetUseCompression(1);
writer->Update();
<http://itk-insight-users.2283740.n2.nabble.com/file/n7588815/original.png>
<http://itk-insight-users.2283740.n2.nabble.com/file/n7588815/result.png>
--
View this message in context: http://itk-insight-users.2283740.n2.nabble.com/image-GetBufferPointer-and-opposite-way-for-3d-images-tp7588815.html
Sent from the ITK Insight Users mailing list archive at Nabble.com.
More information about the Insight-users
mailing list