[ITK-users] ITK Importing Image Data from a Buffer

assil assil-king at hotmail.fr
Mon Oct 27 11:48:40 EDT 2014


Hi Matt,

I already did that. my mistake i have plaste the wrong version of the code.
Her is what i have :


void Cimg_To_ITK (CImg<uchar> img)
{
    typedef unsigned char PixelType;
    typedef itk::RGBPixel< PixelType > RGBPixelType;
    typedef itk::Image< RGBPixelType, 2 > RGBImageType;
    typedef itk::ImportImageFilter< RGBPixelType, 2 > ImportFilterType;
    ImportFilterType::Pointer importFilter = ImportFilterType::New();
    typedef itk::ImageFileWriter<  RGBImageType  > WriterType;
	WriterType::Pointer writer = WriterType::New();


    RGBImageType::SizeType imsize;
    imsize[0] = img.width();
    imsize[1] = img.height();

    ImportFilterType::IndexType start;
    start.Fill( 0 );
    ImportFilterType::RegionType region;
    region.SetIndex( start );
    region.SetSize( imsize );
    importFilter->SetRegion( region );

    double origin[ 2 ];
    origin[0] = 0.0;    // X coordinate
    origin[1] = 0.0;    // Y coordinate
    importFilter->SetOrigin( origin );

    double spacing[ 2 ];
    spacing[0] = 1.0;    // along X direction
    spacing[1] = 1.0;    // along Y direction
    importFilter->SetSpacing( spacing );

    const unsigned int numberOfPixels = imsize[0] * imsize[1] * 3;
    const unsigned int numberOfBytes = numberOfPixels * sizeof( PixelType );
    const bool importImageFilterWillOwnTheBuffer = true;

    unsigned char * localBuffer = new unsigned char[ numberOfPixels ];
    memcpy(localBuffer, img.data(), numberOfBytes);
    importFilter->SetImportPointer( localBuffer,
numberOfPixels,importImageFilterWillOwnTheBuffer );
    importFilter->Update();
    writer->SetInput( importFilter->GetOutput() );
    writer->SetFileName( "output.png" );
    writer->Update();

}


the out image is in the same.



--
View this message in context: http://itk-insight-users.2283740.n2.nabble.com/ITK-Importing-Image-Data-from-a-Buffer-tp7586419p7586421.html
Sent from the ITK Insight Users mailing list archive at Nabble.com.


More information about the Insight-users mailing list