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>"ImageDataMINC2.mnc" 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<short, 3u><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<unsigned long, short><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 "io.h"<br><br>#include "wxVTKRenderWindowInteractor.h"<br>
#include "vtkCamera.h"<br>#include "vtkRenderer.h"<br>#include "vtkRenderWindow.h"<br>#include "vtkConeSource.h"<br>#include "vtkPolyDataMapper.h"<br>#include "vtkActor.h"<br>
#include "vtkPolyDataReader.h"<br><br>#include "vtkPNGReader.h"<br>#include "vtkImageMapper.h"<br>#include "vtkImageShiftScale.h"<br>#include "vtkInteractorStyleImage.h"<br>
#include "vtkActor2D.h"<br><br>#include "vtkImageViewer2.h"<br>#include "vtkImageData.h"<br>#include "vtkTesting.h"<br>#include "vtkTestUtilities.h"<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< PixelType, 3 > 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& title, const wxPoint& pos, const wxSize& size);<br> ~MyFrame();<br><br> // event handlers (these functions should _not_ be virtual)<br>
void OnQuit(wxCommandEvent& event);<br> void OnAbout(wxCommandEvent& 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'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>// 'Main program' equivalent: the program execution "starts" here<br>bool MyApp::OnInit()<br>
{<br> // create the main application window<br> MyFrame *frame = new MyFrame(_T("wxWindows-VTK App"),<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->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& title, const wxPoint& pos, const wxSize& 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(""), wxMENU_TEAROFF);<br><br> // the "About" item should be in the help menu<br> wxMenu *helpMenu = new wxMenu;<br> helpMenu->Append(Minimal_About, _T("&About...\tCtrl-A"), _T("Show about dialog"));<br>
<br> menuFile->Append(Minimal_Quit, _T("E&xit\tAlt-X"), _T("Quit this program"));<br><br> // now append the freshly created menu to the menu bar...<br> wxMenuBar *menuBar = new wxMenuBar();<br>
menuBar->Append(menuFile, _T("&File"));<br> menuBar->Append(helpMenu, _T("&Help"));<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("Drag the mouse here! (wxWindows 2.4.0)"));<br>#endif // wxUSE_STATUSBAR<br><br> m_pVTKWindow = new wxVTKRenderWindowInteractor(this, MY_VTK_WINDOW);<br>
//turn on mouse grabbing if possible<br> m_pVTKWindow->UseCaptureMouseOn();<br> ConstructVTK();<br> ConfigureVTK();<br>}<br><br>MyFrame::~MyFrame()<br>{<br> if(m_pVTKWindow) m_pVTKWindow->Delete();<br>
DestroyVTK();<br>}<br><br>void MyFrame::ConstructVTK()<br>{<br> viewer = vtkImageViewer2::New();<br>}<br><br>void MyFrame::ConfigureVTK()<br>{<br> string inputFileName = "ImageDataMINC2.mnc";<br><br> ImageType::Pointer itkImage1 = ReadMinc2Image< ImageType >( inputFileName );<br>
<br> cout << itkImage1 << endl;<br><br> vtkImageData *vtkImage = ConvertItkToVtk< ImageType >( itkImage1 );<br><br> viewer->SetInput( vtkImage );<br><br>// vtkMINCImageReader *reader = vtkMINCImageReader::New();<br>
// reader->SetFileName( inputFileName.c_str() );<br>// reader->Update();<br>// viewer->SetInput ( reader->GetOutput() );<br><br> viewer->SetColorWindow ( 150 );<br> viewer->SetColorLevel ( 170 );<br>
<br> //Call vtkImageViewer2::SetInput before<br> viewer->SetupInteractor ( m_pVTKWindow );<br>}<br><br>void MyFrame::DestroyVTK()<br>{<br> if (viewer != 0)<br> viewer->Delete();<br>}<br><br>// event handlers<br>
<br>void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))<br>{<br> // TRUE is to force the frame to close<br> Close(TRUE);<br>}<br><br>void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))<br>{<br> wxString msg;<br>
msg.Printf( _T("This is the about dialog of wx-vtk sample.\n"));<br><br> wxMessageBox(msg, _T("About wx-vtk"), 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 <iostream><br><br>#include "itkImage.h"<br>#include "itkImageIOFactory.h"<br>#include "itkMINC2ImageIOFactory.h"<br>#include "itkMINC2ImageIO.h"<br>
#include "itkCastImageFilter.h"<br>#include "itkVTKImageToImageFilter.h"<br>#include "itkImageToVTKImageFilter.h"<br>#include "itkImageFileReader.h"<br>#include "itkImageFileWriter.h"<br>
#include "itkCastImageFilter.h"<br>#include "itkVTKImageExport.h"<br>#include "itkGDCMImageIO.h"<br>#include "itkVTKImageIO.h"<br>#include "itkAnalyzeImageIO.h"<br>#include "itkVTKImageExport.h"<br>
#include "itkVTKImageImport.h"<br><br>#include "vtkImageData.h"<br>#include "vtkMINCImageReader.h"<br>#include "vtkMINCImageWriter.h"<br>#include "vtkImageReader.h"<br>#include "vtkImageWriter.h"<br>
#include "vtkSmartPointer.h"<br><br><br>/// ************************************************************************************************************<br>/// This function will connect the given itk::VTKImageExport filter to the given vtkImageImport filter.<br>
/// ************************************************************************************************************<br>template <typename ITK_Exporter, typename VTK_Importer><br>void ConnectPipelines( ITK_Exporter exporter, VTK_Importer* importer )<br>
{<br> importer->SetUpdateInformationCallback( exporter->GetUpdateInformationCallback() );<br> importer->SetPipelineModifiedCallback( exporter->GetPipelineModifiedCallback() );<br> importer->SetWholeExtentCallback( exporter->GetWholeExtentCallback() );<br>
importer->SetSpacingCallback( exporter->GetSpacingCallback() );<br> importer->SetOriginCallback( exporter->GetOriginCallback() );<br> importer->SetScalarTypeCallback( exporter->GetScalarTypeCallback() );<br>
importer->SetNumberOfComponentsCallback( exporter->GetNumberOfComponentsCallback() );<br> importer->SetPropagateUpdateExtentCallback( exporter->GetPropagateUpdateExtentCallback() );<br> importer->SetUpdateDataCallback( exporter->GetUpdateDataCallback() );<br>
importer->SetDataExtentCallback( exporter->GetDataExtentCallback() );<br> importer->SetBufferPointerCallback( exporter->GetBufferPointerCallback() );<br> importer->SetCallbackUserData( exporter->GetCallbackUserData() );<br>
}<br><br>/// ************************************************************************************************************<br>/// This function will connect the given vtkImageExport filter to the given itk::VTKImageImport filter.<br>
/// ************************************************************************************************************<br>template <typename VTK_Exporter, typename ITK_Importer><br>void ConnectPipelines( VTK_Exporter* exporter, ITK_Importer importer )<br>
{<br> importer->SetUpdateInformationCallback( exporter->GetUpdateInformationCallback() );<br> importer->SetPipelineModifiedCallback( exporter->GetPipelineModifiedCallback() );<br> importer->SetWholeExtentCallback( exporter->GetWholeExtentCallback() );<br>
importer->SetSpacingCallback( exporter->GetSpacingCallback() );<br> importer->SetOriginCallback( exporter->GetOriginCallback() );<br> importer->SetScalarTypeCallback( exporter->GetScalarTypeCallback() );<br>
importer->SetNumberOfComponentsCallback( exporter->GetNumberOfComponentsCallback() );<br> importer->SetPropagateUpdateExtentCallback( exporter->GetPropagateUpdateExtentCallback() );<br> importer->SetUpdateDataCallback( exporter->GetUpdateDataCallback() );<br>
importer->SetDataExtentCallback( exporter->GetDataExtentCallback() );<br> importer->SetBufferPointerCallback( exporter->GetBufferPointerCallback() );<br> importer->SetCallbackUserData( exporter->GetCallbackUserData() );<br>
}<br><br>/// ************************************************************************************************************<br>///<br>/// ************************************************************************************************************<br>
template< typename InputImageType ><br>typename InputImageType::Pointer ConvertVtkToItk( vtkImageData *img )<br>{<br> vtkImageExport* vtkExporter = vtkImageExport::New();<br> vtkExporter->SetInput( img );<br>
<br> typedef itk::VTKImageImport< InputImageType > ImageImportType;<br> typename ImageImportType::Pointer itkImporter = ImageImportType::New();<br> ConnectPipelines( vtkExporter, itkImporter );<br><br> itkImporter->Update();<br>
return itkImporter->GetOutput();<br>}<br><br>/// ************************************************************************************************************<br>///<br>/// ************************************************************************************************************<br>
template< typename TImageType ><br>typename TImageType::Pointer ReadMinc1Image( const std::string fileName )<br>{<br> vtkMINCImageReader *reader = vtkMINCImageReader::New();<br> reader->SetFileName( fileName.c_str() );<br>
<br> try<br> {<br> reader->Update();<br> }<br> catch ( itk::ExceptionObject & err )<br> {<br> std::cout << "Caught an exception: " << std::endl;<br> std::cout << err << " " << __FILE__ << " " << __LINE__ << std::endl;<br>
throw err;<br> }<br> catch (... )<br> {<br> std::cout << "Error while reading in image" << fileName << std::endl;<br> throw;<br> }<br><br> return ConvertVtkToItk< TImageType >( reader->GetOutput() );<br>
}<br><br>/// ************************************************************************************************************<br>///<br>/// ************************************************************************************************************<br>
template< typename TImageType ><br>typename TImageType::Pointer ReadMinc2Image( const std::string fileName )<br>{<br> typedef itk::ImageFileReader< TImageType > ImageFileReader;<br> typedef itk::MINC2ImageIO ImageIOType;<br>
ImageIOType::Pointer minc2ImageIO = ImageIOType::New();<br> typename ImageFileReader::Pointer reader = ImageFileReader::New();<br> reader->SetFileName( fileName );<br> reader->SetImageIO( minc2ImageIO );<br>
<br> try<br> {<br> reader->Update();<br> }<br> catch ( itk::ExceptionObject & err )<br> {<br> std::cout << "Caught an exception: " << std::endl;<br> std::cout << err << " " << __FILE__ << " " << __LINE__ << std::endl;<br>
throw err;<br> }<br> catch (... )<br> {<br> std::cout << "Error while reading in image" << fileName << std::endl;<br> throw;<br> }<br><br> return reader->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 "iotests" )<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 "-ObjC++" )<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 "gl" )<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>