[ITK] [ITK-users] Convert 2D itkImage to vnl_matrix

Marco Ulgelmo marco.ulge at gmail.com
Wed Apr 29 08:46:34 EDT 2015


Hello,

I was trying to get a matrix out of a 2D image as i need to perform
particular calculations. At first i open a 3D image and using
ExtractImageFilter i extract the 2D image collapsing the 3rd dimension.
There must be something wrong in the code as every time i try to perform
any kind of operation on the newly created matrix i get a segfault. It
might be really stupid but i haven't been able to spot it.

Code:

    typedef float OutputPixelType;

    typedef itk::Image< OutputPixelType, 4 > InputImageType;
    typedef itk::Image< OutputPixelType, 3 > OutputImageType;
    typedef itk::Image< OutputPixelType, 2 > BidimensionalImageType;

    typedef itk::ImageFileReader< InputImageType > ReaderType;
    typedef itk::ImageFileReader< OutputImageType > OutputReaderType;
    typedef itk::ImageFileWriter< OutputImageType > WriterType;

    //mediaFilePath is the path to a 3D image (mha extension)

    char* MediaFileName = (char*)mediaFilePath.c_str();

    OutputReaderType::Pointer reader = OutputReaderType::New();
    reader->SetFileName(MediaFileName);
    reader->Update();

    typedef itk::ExtractImageFilter< OutputImageType,
BidimensionalImageType > BidimensionalExtractorType;

    BidimensionalExtractorType::Pointer extractor =
BidimensionalExtractorType::New();

    extractor->InPlaceOn();
    extractor->SetDirectionCollapseToSubmatrix();
    try{
        reader->UpdateOutputInformation();
        }
    catch ( itk::ExceptionObject & exp ){
        return 0;// "Couldn't read selected Subject, format not
supported.";
    }

    OutputImageType::RegionType inputRegion3 =
reader->GetOutput()->GetLargestPossibleRegion();
    OutputImageType::SizeType size3 = inputRegion3.GetSize();
    size3[2] = 0;

    OutputImageType::IndexType start3 = inputRegion3.GetIndex();
    start3[2] = 0;
    OutputImageType::RegionType desiredRegion3;
    desiredRegion3.SetSize( size3 );
    desiredRegion3.SetIndex( start3 );

    extractor->SetExtractionRegion( desiredRegion3 );

    extractor->SetInput( reader->GetOutput() );
    //writer->SetInput( extractor->GetOutput() );

    int width =
reader->GetOutput()->GetLargestPossibleRegion().GetSize()[0];
    int height =
reader->GetOutput()->GetLargestPossibleRegion().GetSize()[1];

    const unsigned int rows = height;
    const unsigned int cols = width;

    typedef vnl_matrix_ref< OutputPixelType > MatrixType;

    BidimensionalImageType::Pointer image = extractor->GetOutput();

    MatrixType matrix( rows, cols, image->GetBufferPointer());

    matrix.data_array();

    std::cout << matrix[1][1] << std::endl;

Thanks,
Marco
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://public.kitware.com/pipermail/community/attachments/20150429/718b131b/attachment-0001.html>
-------------- next part --------------
_____________________________________
Powered by www.kitware.com

Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html

Kitware offers ITK Training Courses, for more information visit:
http://www.kitware.com/products/protraining.php

Please keep messages on-topic and check the ITK FAQ at:
http://www.itk.org/Wiki/ITK_FAQ

Follow this link to subscribe/unsubscribe:
http://public.kitware.com/mailman/listinfo/insight-users


More information about the Community mailing list