Dear All,<br><br>I have coded the following template functions to read a MINC1 image file and convert it ITK. The program compiles and runs <br>without any problem. However I am not sure if this would be the proper manner to do this. I am also not so sure if I have created any memory leaks.<br>
<br>Could anybody please tell me if this okay?<br><br>Thank you,<br>RIcardo<br><br><br><br><br><br>/// ************************************************************************************************************<br>
/// Main program<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>
int main( int argc, char **argv )<br>{<br> string inputFileName = "ImageDataMINC1.mnc";<br><br> ImageType::Pointer itkImage = ReadMinc1Image< ImageType >( inputFileName );<br><br> return 0;<br>}<br>
<br>/// ************************************************************************************************************<br>/// Convert ITK to VTK<br>/// ************************************************************************************************************<br>
template< typename TImageType ><br>vtkImageData *ConvertItkToVtk( typename TImageType::Pointer input )<br>{<br> typedef itk::ImageToVTKImageFilter< TImageType > ImageToVTKImageFilterType;<br> typename ImageToVTKImageFilterType::Pointer connector = ImageToVTKImageFilterType::New();<br>
connector->SetInput( input );<br> connector->Register();<br><br> return connector->GetOutput();<br>}<br><br>/// ************************************************************************************************************<br>
/// Convert VTK to ITK image format<br>/// ************************************************************************************************************<br>template< typename TImageType ><br>typename TImageType::Pointer ConvertVtkToItk( vtkImageData *img )<br>
{<br> typedef itk::VTKImageToImageFilter< TImageType > VTKImageToImageFilterType;<br> typename VTKImageToImageFilterType::Pointer connector = VTKImageToImageFilterType::New();<br> connector->SetInput( img );<br>
connector->GetImporter()->Update();<br><br> return connector->GetImporter()->GetOutput();<br>}<br><br>/// ************************************************************************************************************<br>
/// Read a MINC1 image file<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>