Dear All,<br><br>I am working in a project which requires to read images in MINC1 and MINC2 formats. For that I have coded a few template functions that I have copied bellow. The program compiles and links without any problem. However, when I try to run it gives segmentation fault (please see bellow). <br>
<br>&quot;ImageDataMINC2.mnc&quot; is the MINC image file downloaded from <a href="http://www.insight-journal.org/browse/publication/64">http://www.insight-journal.org/browse/publication/64</a><br><br>Could anybody please pointed out or provide any indication on what I am doing wrong?<br>
<br>Thank you very much,<br>Ricardo<br><br><br><br><br>----------------------- Output result ------------------------------<br><br>ferrari@ferrari-workstation:~/Workspace/MIP_PROJECTS$ bin/bin/iotests<br>Image (0x15c41f0)<br>
  RTTI typeinfo:   itk::Image&lt;short, 3u&gt;<br>  Reference Count: 2<br>  Modified Time: 48<br>  Debug: Off<br>  Observers: <br>    none<br>  Source: (none)<br>  Source output index:  0<br>  Release Data: Off<br>  Data Released: False<br>
  Global Release Data: Off<br>  PipelineMTime: 12<br>  UpdateMTime: 47<br>  LargestPossibleRegion: <br>    Dimension: 3<br>    Index: [0, 0, 0]<br>    Size: [203, 296, 147]<br>  BufferedRegion: <br>    Dimension: 3<br>    Index: [0, 0, 0]<br>
    Size: [203, 296, 147]<br>  RequestedRegion: <br>    Dimension: 3<br>    Index: [0, 0, 0]<br>    Size: [203, 296, 147]<br>  Spacing: [0.06, 0.06, 0.06]<br>  Origin: [-6.07, -11.243, -2.2]<br>  Direction: <br>1 0 0<br>0 1 0<br>
0 0 1<br><br>  IndexToPointMatrix: <br>  0.06 0 0<br>0 0.06 0<br>0 0 0.06<br><br>  PointToIndexMatrix: <br>  16.6667 0 0<br>0 16.6667 0<br>0 0 16.6667<br><br>  PixelContainer: <br>    ImportImageContainer (0x15be750)<br>      RTTI typeinfo:   itk::ImportImageContainer&lt;unsigned long, short&gt;<br>
      Reference Count: 1<br>      Modified Time: 44<br>      Debug: Off<br>      Observers: <br>        none<br>      Pointer: 0x7ff3d2e32010<br>      Container manages memory: true<br>      Size: 8832936<br>      Capacity: 8832936<br>
<br>Segmentation fault<br>ferrari@ferrari-workstation:~/Workspace/MIP_PROJECTS$ <br><br><br><br><br><br>------------------------ File main.cpp ---------------------------<br><br><br>#include &quot;io.h&quot;<br><br>#include &quot;wxVTKRenderWindowInteractor.h&quot;<br>
#include &quot;vtkCamera.h&quot;<br>#include &quot;vtkRenderer.h&quot;<br>#include &quot;vtkRenderWindow.h&quot;<br>#include &quot;vtkConeSource.h&quot;<br>#include &quot;vtkPolyDataMapper.h&quot;<br>#include &quot;vtkActor.h&quot;<br>
#include &quot;vtkPolyDataReader.h&quot;<br><br>#include &quot;vtkPNGReader.h&quot;<br>#include &quot;vtkImageMapper.h&quot;<br>#include &quot;vtkImageShiftScale.h&quot;<br>#include &quot;vtkInteractorStyleImage.h&quot;<br>
#include &quot;vtkActor2D.h&quot;<br><br>#include &quot;vtkImageViewer2.h&quot;<br>#include &quot;vtkImageData.h&quot;<br>#include &quot;vtkTesting.h&quot;<br>#include &quot;vtkTestUtilities.h&quot;<br><br><br><br><br>/// Pixel type definition<br>
typedef signed short                     PixelType;<br><br>/// Define type of the input and output images<br>typedef itk::Image&lt; PixelType, 3 &gt;         ImageType;<br><br><br><br>using namespace std;<br><br><br><br>class MyApp;<br>
class MyFrame;<br><br>// Define a new application type, each program should derive a class from wxApp<br>class MyApp : public wxApp<br>{<br>public:<br>    // this one is called on application startup and is a good place for the app<br>
    // initialization (doing it here and not in the ctor allows to have an error<br>    // return: if OnInit() returns false, the application terminates)<br>    virtual bool OnInit();<br>};<br><br>// Define a new frame type: this is going to be our main frame<br>
class MyFrame : public wxFrame<br>{<br>public:<br>    // ctor(s)<br>    MyFrame(const wxString&amp; title, const wxPoint&amp; pos, const wxSize&amp; size);<br>    ~MyFrame();<br><br>    // event handlers (these functions should _not_ be virtual)<br>
    void OnQuit(wxCommandEvent&amp; event);<br>    void OnAbout(wxCommandEvent&amp; event);<br><br>protected:<br>    void ConstructVTK();<br>    void ConfigureVTK();<br>    void DestroyVTK();<br><br>private:<br>  wxVTKRenderWindowInteractor *m_pVTKWindow;<br>
<br>  // vtk classes<br>  vtkRenderer       *pRenderer;<br>  vtkRenderWindow   *pRenderWindow;<br>  vtkPolyDataMapper *pConeMapper;<br>  vtkActor          *pConeActor;<br>  vtkConeSource     *pConeSource;<br>  vtkImageViewer2   *viewer;<br>
  vtkPNGReader      *reader;<br><br>private:<br>    // any class wishing to process wxWindows events must use this macro<br>    DECLARE_EVENT_TABLE()<br>};<br><br>// IDs for the controls and the menu commands<br>enum<br>{<br>
    // menu items<br>    Minimal_Quit = 1,<br>    Minimal_About<br>};<br><br>#define MY_FRAME      101<br>#define MY_VTK_WINDOW 102<br><br>// the event tables connect the wxWindows events with the functions (event<br>// handlers) which process them. It can be also done at run-time, but for the<br>
// simple menu events like this the static method is much simpler.<br>BEGIN_EVENT_TABLE(MyFrame, wxFrame)<br>    EVT_MENU(Minimal_Quit,  MyFrame::OnQuit)<br>    EVT_MENU(Minimal_About, MyFrame::OnAbout)<br>END_EVENT_TABLE()<br>
<br>// Create a new application object: this macro will allow wxWindows to create<br>// the application object during program execution (it&#39;s better than using a<br>// static object for many reasons) and also declares the accessor function<br>
// wxGetApp() which will return the reference of the right type (i.e. MyApp and<br>// not wxApp)<br>IMPLEMENT_APP(MyApp)<br><br>// &#39;Main program&#39; equivalent: the program execution &quot;starts&quot; here<br>bool MyApp::OnInit()<br>
{<br>    // create the main application window<br>    MyFrame *frame = new MyFrame(_T(&quot;wxWindows-VTK App&quot;),<br>                                 wxPoint(50, 50), wxSize(450, 340));<br><br>    // and show it (the frames, unlike simple controls, are not shown when<br>
    // created initially)<br>    frame-&gt;Show(TRUE);<br><br>    // success: wxApp::OnRun() will be called which will enter the main message<br>    // loop and the application will run. If we returned FALSE here, the<br>
    // application would exit immediately.<br>    return TRUE;<br>}<br><br>// frame constructor<br>MyFrame::MyFrame(const wxString&amp; title, const wxPoint&amp; pos, const wxSize&amp; size)<br>       : wxFrame((wxFrame *)NULL, -1, title, pos, size)<br>
{<br>#ifdef __WXMAC__<br>    // we need this in order to allow the about menu relocation, since ABOUT is<br>    // not the default id of the about menu<br>    wxApp::s_macAboutMenuItemId = Minimal_About;<br>#endif<br><br>
    // create a menu bar<br>    wxMenu *menuFile = new wxMenu(_T(&quot;&quot;), wxMENU_TEAROFF);<br><br>    // the &quot;About&quot; item should be in the help menu<br>    wxMenu *helpMenu = new wxMenu;<br>    helpMenu-&gt;Append(Minimal_About, _T(&quot;&amp;About...\tCtrl-A&quot;), _T(&quot;Show about dialog&quot;));<br>
<br>    menuFile-&gt;Append(Minimal_Quit, _T(&quot;E&amp;xit\tAlt-X&quot;), _T(&quot;Quit this program&quot;));<br><br>    // now append the freshly created menu to the menu bar...<br>    wxMenuBar *menuBar = new wxMenuBar();<br>
    menuBar-&gt;Append(menuFile, _T(&quot;&amp;File&quot;));<br>    menuBar-&gt;Append(helpMenu, _T(&quot;&amp;Help&quot;));<br><br>    // ... and attach this menu bar to the frame<br>    SetMenuBar(menuBar);<br><br>#if wxUSE_STATUSBAR<br>
    // create a status bar just for fun (by default with 1 pane only)<br>    CreateStatusBar(2);<br>    SetStatusText(_T(&quot;Drag the mouse here! (wxWindows 2.4.0)&quot;));<br>#endif // wxUSE_STATUSBAR<br><br>    m_pVTKWindow = new wxVTKRenderWindowInteractor(this, MY_VTK_WINDOW);<br>
    //turn on mouse grabbing if possible<br>    m_pVTKWindow-&gt;UseCaptureMouseOn();<br>    ConstructVTK();<br>    ConfigureVTK();<br>}<br><br>MyFrame::~MyFrame()<br>{<br>  if(m_pVTKWindow) m_pVTKWindow-&gt;Delete();<br>
  DestroyVTK();<br>}<br><br>void MyFrame::ConstructVTK()<br>{<br>  viewer = vtkImageViewer2::New();<br>}<br><br>void MyFrame::ConfigureVTK()<br>{<br>    string inputFileName = &quot;ImageDataMINC2.mnc&quot;;<br><br>    ImageType::Pointer itkImage1 = ReadMinc2Image&lt; ImageType &gt;( inputFileName );<br>
<br>    cout &lt;&lt; itkImage1 &lt;&lt; endl;<br><br>    vtkImageData *vtkImage = ConvertItkToVtk&lt; ImageType &gt;( itkImage1 );<br><br>    viewer-&gt;SetInput( vtkImage );<br><br>//    vtkMINCImageReader *reader = vtkMINCImageReader::New();<br>
//    reader-&gt;SetFileName( inputFileName.c_str() );<br>//  reader-&gt;Update();<br>//    viewer-&gt;SetInput ( reader-&gt;GetOutput() );<br><br>    viewer-&gt;SetColorWindow ( 150 );<br>    viewer-&gt;SetColorLevel ( 170 );<br>
<br>    //Call vtkImageViewer2::SetInput before<br>    viewer-&gt;SetupInteractor (  m_pVTKWindow );<br>}<br><br>void MyFrame::DestroyVTK()<br>{<br>  if (viewer != 0)<br>    viewer-&gt;Delete();<br>}<br><br>// event handlers<br>
<br>void MyFrame::OnQuit(wxCommandEvent&amp; WXUNUSED(event))<br>{<br>    // TRUE is to force the frame to close<br>    Close(TRUE);<br>}<br><br>void MyFrame::OnAbout(wxCommandEvent&amp; WXUNUSED(event))<br>{<br>    wxString msg;<br>
    msg.Printf( _T(&quot;This is the about dialog of wx-vtk sample.\n&quot;));<br><br>    wxMessageBox(msg, _T(&quot;About wx-vtk&quot;), wxOK | wxICON_INFORMATION, this);<br>}<br><br><br><br><br><br><br><br>------------------------- FILE  io.h ----------------------------<br>
<br>#ifndef __MIP_IO_H__<br>#define __MIP_IO_H__<br><br>#include &lt;iostream&gt;<br><br>#include &quot;itkImage.h&quot;<br>#include &quot;itkImageIOFactory.h&quot;<br>#include &quot;itkMINC2ImageIOFactory.h&quot;<br>#include &quot;itkMINC2ImageIO.h&quot;<br>
#include &quot;itkCastImageFilter.h&quot;<br>#include &quot;itkVTKImageToImageFilter.h&quot;<br>#include &quot;itkImageToVTKImageFilter.h&quot;<br>#include &quot;itkImageFileReader.h&quot;<br>#include &quot;itkImageFileWriter.h&quot;<br>
#include &quot;itkCastImageFilter.h&quot;<br>#include &quot;itkVTKImageExport.h&quot;<br>#include &quot;itkGDCMImageIO.h&quot;<br>#include &quot;itkVTKImageIO.h&quot;<br>#include &quot;itkAnalyzeImageIO.h&quot;<br>#include &quot;itkVTKImageExport.h&quot;<br>
#include &quot;itkVTKImageImport.h&quot;<br><br>#include &quot;vtkImageData.h&quot;<br>#include &quot;vtkMINCImageReader.h&quot;<br>#include &quot;vtkMINCImageWriter.h&quot;<br>#include &quot;vtkImageReader.h&quot;<br>#include &quot;vtkImageWriter.h&quot;<br>
#include &quot;vtkSmartPointer.h&quot;<br><br><br>/// ************************************************************************************************************<br>/// This function will connect the given itk::VTKImageExport filter to the given vtkImageImport filter.<br>
/// ************************************************************************************************************<br>template &lt;typename ITK_Exporter, typename VTK_Importer&gt;<br>void ConnectPipelines( ITK_Exporter exporter, VTK_Importer* importer )<br>
{<br>    importer-&gt;SetUpdateInformationCallback( exporter-&gt;GetUpdateInformationCallback() );<br>    importer-&gt;SetPipelineModifiedCallback( exporter-&gt;GetPipelineModifiedCallback() );<br>    importer-&gt;SetWholeExtentCallback( exporter-&gt;GetWholeExtentCallback() );<br>
    importer-&gt;SetSpacingCallback( exporter-&gt;GetSpacingCallback() );<br>    importer-&gt;SetOriginCallback( exporter-&gt;GetOriginCallback() );<br>    importer-&gt;SetScalarTypeCallback( exporter-&gt;GetScalarTypeCallback() );<br>
    importer-&gt;SetNumberOfComponentsCallback( exporter-&gt;GetNumberOfComponentsCallback() );<br>    importer-&gt;SetPropagateUpdateExtentCallback( exporter-&gt;GetPropagateUpdateExtentCallback() );<br>    importer-&gt;SetUpdateDataCallback( exporter-&gt;GetUpdateDataCallback() );<br>
    importer-&gt;SetDataExtentCallback( exporter-&gt;GetDataExtentCallback() );<br>    importer-&gt;SetBufferPointerCallback( exporter-&gt;GetBufferPointerCallback() );<br>    importer-&gt;SetCallbackUserData( exporter-&gt;GetCallbackUserData() );<br>
}<br><br>/// ************************************************************************************************************<br>/// This function will connect the given vtkImageExport filter to the given itk::VTKImageImport filter.<br>
/// ************************************************************************************************************<br>template &lt;typename VTK_Exporter, typename ITK_Importer&gt;<br>void ConnectPipelines( VTK_Exporter* exporter, ITK_Importer importer )<br>
{<br>    importer-&gt;SetUpdateInformationCallback( exporter-&gt;GetUpdateInformationCallback() );<br>    importer-&gt;SetPipelineModifiedCallback( exporter-&gt;GetPipelineModifiedCallback() );<br>    importer-&gt;SetWholeExtentCallback( exporter-&gt;GetWholeExtentCallback() );<br>
    importer-&gt;SetSpacingCallback( exporter-&gt;GetSpacingCallback() );<br>    importer-&gt;SetOriginCallback( exporter-&gt;GetOriginCallback() );<br>    importer-&gt;SetScalarTypeCallback( exporter-&gt;GetScalarTypeCallback() );<br>
    importer-&gt;SetNumberOfComponentsCallback( exporter-&gt;GetNumberOfComponentsCallback() );<br>    importer-&gt;SetPropagateUpdateExtentCallback( exporter-&gt;GetPropagateUpdateExtentCallback() );<br>    importer-&gt;SetUpdateDataCallback( exporter-&gt;GetUpdateDataCallback() );<br>
    importer-&gt;SetDataExtentCallback( exporter-&gt;GetDataExtentCallback() );<br>    importer-&gt;SetBufferPointerCallback( exporter-&gt;GetBufferPointerCallback() );<br>    importer-&gt;SetCallbackUserData( exporter-&gt;GetCallbackUserData() );<br>
}<br><br>/// ************************************************************************************************************<br>///<br>/// ************************************************************************************************************<br>
template&lt; typename InputImageType &gt;<br>typename InputImageType::Pointer ConvertVtkToItk( vtkImageData *img )<br>{<br>    vtkImageExport* vtkExporter = vtkImageExport::New();<br>    vtkExporter-&gt;SetInput( img );<br>
<br>    typedef itk::VTKImageImport&lt; InputImageType &gt; ImageImportType;<br>    typename ImageImportType::Pointer itkImporter = ImageImportType::New();<br>    ConnectPipelines( vtkExporter, itkImporter );<br><br>    itkImporter-&gt;Update();<br>
    return itkImporter-&gt;GetOutput();<br>}<br><br>/// ************************************************************************************************************<br>///<br>/// ************************************************************************************************************<br>
template&lt; typename TImageType &gt;<br>typename TImageType::Pointer ReadMinc1Image( const std::string fileName )<br>{<br>    vtkMINCImageReader *reader = vtkMINCImageReader::New();<br>    reader-&gt;SetFileName( fileName.c_str() );<br>
<br>    try<br>    {<br>        reader-&gt;Update();<br>    }<br>    catch ( itk::ExceptionObject &amp; err )<br>    {<br>        std::cout &lt;&lt; &quot;Caught an exception: &quot; &lt;&lt; std::endl;<br>        std::cout &lt;&lt; err &lt;&lt; &quot; &quot; &lt;&lt; __FILE__ &lt;&lt; &quot; &quot; &lt;&lt; __LINE__ &lt;&lt; std::endl;<br>
        throw err;<br>    }<br>    catch (... )<br>    {<br>        std::cout &lt;&lt; &quot;Error while reading in image&quot; &lt;&lt; fileName &lt;&lt; std::endl;<br>        throw;<br>    }<br><br>    return ConvertVtkToItk&lt; TImageType &gt;( reader-&gt;GetOutput() );<br>
}<br><br>/// ************************************************************************************************************<br>///<br>/// ************************************************************************************************************<br>
template&lt; typename TImageType &gt;<br>typename TImageType::Pointer ReadMinc2Image( const std::string fileName )<br>{<br>    typedef itk::ImageFileReader&lt; TImageType &gt;    ImageFileReader;<br>    typedef itk::MINC2ImageIO ImageIOType;<br>
    ImageIOType::Pointer minc2ImageIO = ImageIOType::New();<br>    typename ImageFileReader::Pointer reader = ImageFileReader::New();<br>    reader-&gt;SetFileName( fileName );<br>    reader-&gt;SetImageIO( minc2ImageIO );<br>
<br>    try<br>    {<br>        reader-&gt;Update();<br>    }<br>    catch ( itk::ExceptionObject &amp; err )<br>    {<br>        std::cout &lt;&lt; &quot;Caught an exception: &quot; &lt;&lt; std::endl;<br>        std::cout &lt;&lt; err &lt;&lt; &quot; &quot; &lt;&lt; __FILE__ &lt;&lt; &quot; &quot; &lt;&lt; __LINE__ &lt;&lt; std::endl;<br>
        throw err;<br>    }<br>    catch (... )<br>    {<br>        std::cout &lt;&lt; &quot;Error while reading in image&quot; &lt;&lt; fileName &lt;&lt; std::endl;<br>        throw;<br>    }<br><br>    return reader-&gt;GetOutput();<br>
}<br><br><br>#endif <br><br><br>------------------------- File CMakeLists.txt __________________________<br><br><br>CMAKE_MINIMUM_REQUIRED(VERSION 2.7)<br><br>SET( ProgramName &quot;iotests&quot; )<br><br>PROJECT( ${ProgramName} )<br>
<br>FIND_PACKAGE (ITK REQUIRED)<br>IF (ITK_FOUND)<br>    INCLUDE( ${USE_ITK_FILE} )<br>    SET(ITK_LIBRARIES ITKCommon ITKBasicFilters ITKIO ITKIOMINC2 ITKMetaIO ITKNumerics ITKStatistics)<br>ENDIF(ITK_FOUND)<br><br>FIND_PACKAGE (VTK REQUIRED)<br>
IF (VTK_FOUND)<br>    INCLUDE( ${USE_VTK_FILE} )<br>    SET(VTK_LIBRARIES vtkRendering vtkGraphics vtkWidgets vtkHybrid vtkImaging vtkIO vtkFiltering vtkCommon)<br>ENDIF( VTK_FOUND)<br><br>#<br># The following allows you to access wxGLCanvas for GTK<br>
#<br>IF(WIN32)<br>    SET( GUI_EXECUTABLE WIN32 )<br>ELSE(WIN32)<br>      IF(APPLE)<br>        SET( GUI_EXECUTABLE MACOSX_BUNDLE )<br>        IF(VTK_USE_COCOA)<br>              SET_SOURCE_FILES_PROPERTIES( wxVtkWidgets/wxVTKRenderWindowInteractor.cxx PROPERTIES COMPILE_FLAGS &quot;-ObjC++&quot; )<br>
        ENDIF(VTK_USE_COCOA)<br>      ELSE(APPLE)<br>        #<br>        # CMake 2.6: technically those packages are not required since one can still use the Motif/X11 version and not the gtk one:<br>        #<br>        FIND_PACKAGE(PkgConfig REQUIRED)<br>
        pkg_check_modules (GTK2 gtk+-2.0)<br>        INCLUDE_DIRECTORIES(${GTK2_INCLUDE_DIRS})<br>        LINK_LIBRARIES(${GTK2_LIBRARIES})<br>        <br>        #<br>        # Can I require all my user to have the gl lib on linux, even if they do not really need it...<br>
        #<br>        SET( WXGLCANVASLIBS &quot;gl&quot; )<br>      ENDIF(APPLE)<br>ENDIF(WIN32)<br><br><br>#<br># wxWidgets is required to build the project<br>#<br># For GTK we need a couple of stuff:<br>#     gl: GLCanvas<br>
#     adv: wxSashLayoutWindow and such...<br>#<br>FIND_PACKAGE( wxWidgets COMPONENTS base core adv ${WXGLCANVASLIBS} REQUIRED )<br>IF(wxWidgets_FOUND)<br>    INCLUDE(${wxWidgets_USE_FILE})<br>ENDIF(wxWidgets_FOUND)<br><br>
INCLUDE_DIRECTORIES(<br>    ${CMAKE_CURRENT_SOURCE_DIR} <br>    ${CMAKE_CURRENT_SOURCE_DIR}/../itkVtk  <br>                  #<br>                  # itkVtk folder contains the followin files:<br>                  #<br>                  # itkImageToVTKImageFilter.h  itkImageToVTKImageFilter.txx <br>
                  # itkVTKImageToImageFilter.h  itkVTKImageToImageFilter.txx<br>)<br><br>SET( SRCS<br>    ${CMAKE_CURRENT_SOURCE_DIR}/../../viewer/wxVtkWidgets/wxVTKRenderWindowInteractor.cxx<br>)<br><br>ADD_EXECUTABLE( ${ProgramName}<br>
    main.cpp <br>    ${SRCS} <br>)<br><br>TARGET_LINK_LIBRARIES( ${ProgramName}<br>       ${VTK_LIBRARIES}<br>       ${ITK_LIBRARIES}<br>       ${wxWidgets_LIBRARIES}       <br><br><br><br>