-1

I get a segmentation fault when trying to display or apply any filter to an ikt::Image with float pixel types. I can use this same code (swapping the pixel types) to open an image with unsigned char pixel type and it works just fine. Both images I mentioned (the float pixel ones and the unsigned char one) are of type Nifti.

I also have opened the image with ImageIOBase to check the pixel type and its indeed float.

#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkExtractImageFilter.h"
#include "itkImageToVTKImageFilter.h"
#include "vtkImageViewer.h"
#include "vtkImageMapper3D.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkSmartPointer.h"
#include "vtkImageActor.h"
#include "vtkInteractorStyleImage.h"
#include "vtkRenderer.h"
#include "itkRGBPixel.h"
#include <unistd.h>
#include "QuickView.h"
#include <armadillo>
#include "itkBinaryThresholdImageFilter.h"
#include "itkMedianImageFilter.h"



typedef float FloatPixelType;
typedef itk::Image<FloatPixelType,  3> Input3DFloatImageType;
typedef itk::Image<FloatPixelType, 2> FloatImageType;
typedef itk::ImageFileReader< Input3DFloatImageType> Float3DReaderType;
typedef itk::ImageToVTKImageFilter<FloatImageType> FloatConnectorType;
typedef itk::ExtractImageFilter< Input3DFloatImageType, FloatImageType > FloatFilterType;

int main(int argc, char *argv[]){

const char * grayMatterFile = "../../I3T/I3TGM.img";
Float3DReaderType::Pointer floatReader = Float3DReaderType::New();
floatReader->SetFileName( grayMatterFile );

FloatFilterType::Pointer floatFilter = FloatFilterType::New();
floatFilter->SetDirectionCollapseToSubmatrix();
floatFilter->InPlaceOn();
floatReader->UpdateOutputInformation();
Input3DFloatImageType::RegionType floatRegion = floatReader->GetOutput()->GetLargestPossibleRegion();

Input3DFloatImageType::SizeType floatSize = floatRegion.GetSize();
floatSize[1] = 0;
Input3DFloatImageType::IndexType floatIndexStart = floatRegion.GetIndex();

for(int i = 0; floatRegion.GetSize()[1]; i++){
        
        floatIndexStart[1] = i;
        Input3DFloatImageType::RegionType floatDesiredRegion;
        
        floatDesiredRegion.SetSize( floatSize);
        floatDesiredRegion.SetIndex( floatIndexStart);
    
        // Float image
        floatFilter->SetInput( floatReader->GetOutput() );
        floatFilter->SetExtractionRegion( floatDesiredRegion ); 
        floatFilter->Update();
        ShowImage<FloatImageType>(floatFilter->GetOutput());
    
    
    }
}

template<typename TImageType>
void ShowImage(typename TImageType::Pointer image){

    typedef itk::ImageToVTKImageFilter<TImageType> ThisConnectorType;
    typename ThisConnectorType::Pointer connector = ThisConnectorType::New();
    connector->SetInput(image);

    try{
        connector->Update();
     } catch( itk::ExceptionObject & error ) {
        std::cerr << "Error: " << error << std::endl;
        return;
     }


    vtkSmartPointer<vtkImageActor> actor =  vtkSmartPointer<vtkImageActor>::New();

    actor->GetMapper()->SetInputData(connector->GetOutput());

    vtkSmartPointer<vtkRenderer> renderer = vtkSmartPointer<vtkRenderer>::New();
    renderer->AddActor(actor);
    renderer->ResetCamera();

    vtkSmartPointer<vtkRenderWindow> renderWindow = vtkSmartPointer<vtkRenderWindow>::New();
    renderWindow->AddRenderer(renderer);

    vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
    vtkSmartPointer<vtkInteractorStyleImage> style = vtkSmartPointer<vtkInteractorStyleImage>::New();

    renderWindowInteractor->SetInteractorStyle(style);

    renderWindowInteractor->SetRenderWindow(renderWindow);
    renderWindowInteractor->Initialize();

    renderWindowInteractor->Start();
}

Segmentation fault occurs in line: floatFilter->Update();

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Pablo Duque
  • 383
  • 2
  • 20

1 Answers1

1

You lack floatFilter->Update(); right before ShowImage.

Edit:

Your updated code (with minor modifications) runs on my computer. Your installation of ITK or VTK might be broken. Try running it in debug mode using an IDE, that will show you where is the fault occurring.

Dženan
  • 3,329
  • 3
  • 31
  • 44
  • Thank you for your answer! But the error persists, now it actually causes the segmentation fault on that line I added. Any other ideas? – Pablo Duque Nov 30 '17 at 18:59
  • 1
    What is the error message? ITK rarely just crashes, it usually outputs an error message. Also, your code does not compile. Can you add the includes and the missing definitions so I can copy-paste and reproduce your problem? – Dženan Dec 01 '17 at 19:57
  • It would only say Segmentation Fault. I only added the code that is part of the crash to this question. However you can see the full file in the following link. Its a very messy code since Im just testing many different things at the time. Just made the repo public so you can take a look at it. And thank you very much!! https://github.com/bonisthegreat/GrayMatter/blob/master/m0/m0.cxx – Pablo Duque Dec 02 '17 at 22:17
  • However I added all includes and I think all types that were missing to the question. Thanks again! – Pablo Duque Dec 04 '17 at 14:07