[Insight-users] Problem with ITK and GDCM in the same program

edoardo.belletti at alice.it edoardo.belletti at alice.it
Wed May 12 15:10:30 EDT 2010


Hi everyone,
I have a problem: I have created this programthat use both ITK and GDCM libraries:

// GDCM Libraries
#include "gdcmReader.h"
#include "gdcmPrinter.h"
#include "gdcmDictPrinter.h"
// ITK Libraries
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkSobelEdgeDetectionImageFilter.h"
#include "itkCastImageFilter.h"
#include "itkRescaleIntensityImageFilter.h"
#include "itkGDCMImageIO.h"
#include "itkRegionOfInterestImageFilter.h"
// Standard Libraries
#include <string>
#include <ios>
#include <istream>
#include <iostream>
#include <fstream>
#include <stdio.h>     
#include <stdlib.h> 
// Type definitions
const unsigned int Dimension = 2;
typedef unsigned char PixelType;
typedef itk::Image< PixelType, Dimension > ImageType;
typedef itk::ImageFileReader< ImageType > ReaderType;
typedef itk::ImageFileWriter< ImageType > WriterType;
typedef itk::GDCMImageIO ImageIOType;
typedef itk::Image< float, Dimension > OutputImageType;
typedef itk::SobelEdgeDetectionImageFilter< OutputImageType, OutputImageType > SobelFilterType;
typedef itk::RegionOfInterestImageFilter< ImageType, ImageType > RegionFilterType;
typedef itk::CastImageFilter< ImageType, OutputImageType> CastToRealFilterType;
typedef itk::RescaleIntensityImageFilter< OutputImageType, ImageType > RescaleFilter;   
// Functions
template <typename TPrinter> bool Region(const std::string & filename, unsigned int* X_min, unsigned int* Y_min, unsigned int* X_max, unsigned int* Y_max );


int main (int argc, char *argv[])
{
	if( argc < 2 )
	{
		std::cerr << "Usage: " << std::endl;
		std::cerr << argv[0] << " inputImageFile  outputImageFile " << std::endl;
		return EXIT_FAILURE;
	}
  	std::string filename = argv[1];


//	ImageIOType::Pointer dicomIO = ImageIOType::New();
//	ReaderType::Pointer reader = ReaderType::New();
//	reader->SetFileName( argv[1] );
//	reader->SetImageIO( dicomIO );
//	reader->Update();


  	unsigned int x_min = 1;
	unsigned int y_min = 1;
	unsigned int x_max = 1;
	unsigned int y_max = 1;
  	if( Region<gdcm::Printer>( filename, &x_min, &y_min, &x_max, &y_max ) )
  	{
		std::cout << "x_min = " << x_min << std::endl;	
		std::cout << "y_min = " << y_min << std::endl;
		std::cout << "x_max = " << x_max << std::endl;
		std::cout << "y_max = " << y_max << std::endl;
  	}
  	else
  	{
		std::cout << "no\n";

  	}

  	//Return
  	return EXIT_SUCCESS;
}



// Functions


template <typename TPrinter> bool Region(const std::string & filename, unsigned int* X_min, unsigned int* Y_min, unsigned int* X_max, unsigned int* Y_max )
{
  	gdcm::Reader reader;
  	reader.SetFileName( filename.c_str() );
  	bool success = reader.Read();
  	if( !success )
    {
    	std::cerr << "Failed to read: " << filename << std::endl;
    	return false;
    }

// Replace the stream buffer used by 'std::cout' with a suitable other 
// stream buffer.
// Direct 'std::cout' to not print to the screen but also write to a file.
  	std::streambuf* cout_sbuf = std::cout.rdbuf(); // save original sbuf
  	std::ofstream   fout( "header_dicom.txt" );
  	std::cout.rdbuf( fout.rdbuf() ); // redirect 'cout' to a 'fout'   

  	TPrinter printer;
  	printer.SetFile ( reader.GetFile() );
  	printer.Print( std::cout );

// Restoring the original stream buffer is necessary because the stream
// buffer to which 'std::cout' was redirected is destroyed at the end of
// 'main()'.
  	std::cout.rdbuf( cout_sbuf ); // restore the original stream buffer


  	std::string str, str_prec;
  	std::fstream f_input;
  	bool findX0 = false; 
  	bool findX1 = false; 
  	bool findY0 = false;
  	bool findY1 = false;
  	f_input.open ( "header_dicom.txt", std::ios::in );
  	if ( f_input.fail() )
  	{
		std::cerr << "ERROR: can not open file!\n";
		remove ( "header_dicom.txt" );
		return false;
  	}

  	while ( f_input >> str )
  	{
		if ( str.compare("(0018,6018)") == 0 )
		{
			do {
				str_prec = str;
				f_input >> str;
			} while ( str.compare("#") != 0 );
			*X_min = atoi(str_prec.c_str());
			findX0 = true;
			//std::cout << *X_min << std::endl;
		}
		if ( str.compare("(0018,601a)") == 0 )
		{ 
			do {
				str_prec = str;
				f_input >> str;
			} while ( str.compare("#") != 0 );
			*Y_min = atoi(str_prec.c_str());
			findY0 = true;
			//std::cout << *Y_min << std::endl;
		}
		if ( str.compare("(0018,601c)") == 0 )
		{
			do {
				str_prec = str;
				f_input >> str;
			} while ( str.compare("#") != 0 );
			*X_max = atoi(str_prec.c_str());
			findX1 = true;
			//std::cout << *X_max << std::endl;
		}
		if ( str.compare("(0018,601e)") == 0 )
		{
			do {
				str_prec = str;
				f_input >> str;
			} while ( str.compare("#") != 0 );
			*Y_max = atoi(str_prec.c_str());
			findY1 = true;
			//std::cout << *Y_max << std::endl;
		}

		//std::cout << str << std::endl;
	}

	if ( ( !findX0 ) || ( !findY0 ) || ( !findX1 ) || ( !findY1 ) )
	{
		remove ( "header_dicom.txt" );
		return false;
	}
	f_input.close();
	  
	remove ( "header_dicom.txt" );
	return true;
}



when i run it without the comments on this lines (49-53):
	ImageIOType::Pointer dicomIO = ImageIOType::New();
	ReaderType::Pointer reader = ReaderType::New();
	reader->SetFileName( argv[1] );
	reader->SetImageIO( dicomIO );
	reader->Update();

the output is:

edoardo at edoardo-laptop:~/ITK/Tesi/01_Autocrop/crop/bin$ make
Scanning dependencies of target cropping
[100%] Building CXX object CMakeFiles/cropping.dir/cropping.cxx.o
Linking CXX executable cropping
/usr/local/lib/InsightToolkit/libitkgdcm.a(gdcmGlobal.o): In function `gdcm::Global::Global()':
gdcmGlobal.cxx:(.text+0x0): multiple definition of `gdcm::Global::Global()'
/usr/local/lib/libgdcmDICT.a(gdcmGlobal.cxx.o):gdcmGlobal.cxx:(.text+0x0): first defined here
/usr/local/lib/InsightToolkit/libitkgdcm.a(gdcmGlobal.o): In function `gdcm::Global::Global()':
gdcmGlobal.cxx:(.text+0x300): multiple definition of `gdcm::Global::Global()'
/usr/local/lib/libgdcmDICT.a(gdcmGlobal.cxx.o):gdcmGlobal.cxx:(.text+0x80): first defined here
/usr/local/lib/InsightToolkit/libitkgdcm.a(gdcmGlobal.o): In function `gdcm::Global::~Global()':
gdcmGlobal.cxx:(.text+0x600): multiple definition of `gdcm::Global::~Global()'
/usr/local/lib/libgdcmDICT.a(gdcmGlobal.cxx.o):gdcmGlobal.cxx:(.text+0x100): first defined here
/usr/local/lib/InsightToolkit/libitkgdcm.a(gdcmGlobal.o): In function `gdcm::Global::~Global()':
gdcmGlobal.cxx:(.text+0x694): multiple definition of `gdcm::Global::~Global()'
/usr/local/lib/libgdcmDICT.a(gdcmGlobal.cxx.o):gdcmGlobal.cxx:(.text+0x14c): first defined here
collect2: ld returned 1 exit status
make[2]: *** [cropping] Errore 1
make[1]: *** [CMakeFiles/cropping.dir/all] Errore 2
make: *** [all] Errore 2

instead of if these lines are commented I don't have any error; but my problem is that I want to use also ITK libraries into my project.

my CMakeList.txt:
CMAKE_MINIMUM_REQUIRED(VERSION 2.8)

PROJECT(ImageIO)

# Find ITK.
FIND_PACKAGE(ITK)
IF(NOT ITK_DIR)
  MESSAGE(FATAL_ERROR "Please set ITK_DIR.")
ENDIF(NOT ITK_DIR) 
INCLUDE(${ITK_USE_FILE})

# Find GDCM
FIND_PACKAGE(GDCM)
  IF(GDCM_FOUND)
    INCLUDE(${GDCM_USE_FILE})
    IF( "${GDCM_MAJOR_VERSION}" LESS 2.0 )
      SET(MITK_GDCM_LIBRARIES gdcm)
    ELSE( "${GDCM_MAJOR_VERSION}" LESS 2.0 )
      SET(MITK_GDCM_LIBRARIES gdcmMSFF)
    ENDIF( "${GDCM_MAJOR_VERSION}" LESS 2.0 )
  ELSE(GDCM_FOUND)
    MESSAGE(FATAL_ERROR "Must set GDCM_DIR for MITK_USE_SYSTEM_GDCM.")
  ENDIF(GDCM_FOUND)



ADD_EXECUTABLE(cropping cropping.cxx )
TARGET_LINK_LIBRARIES(cropping gdcmMSFF ITKCommon ITKIO)



moreover my cmake configurations are these:

CMAKE_AR                         /usr/bin/ar                                                                                                           
 CMAKE_BUILD_TYPE                                                                                                                                       
 CMAKE_COLOR_MAKEFILE             ON                                                                                                                    
 CMAKE_CXX_COMPILER               /usr/bin/c++                                                                                                          
 CMAKE_CXX_FLAGS                                                                                                                                        
 CMAKE_CXX_FLAGS_DEBUG            -g                                                                                                                    
 CMAKE_CXX_FLAGS_MINSIZEREL       -Os -DNDEBUG                                                                                                          
 CMAKE_CXX_FLAGS_RELEASE          -O3 -DNDEBUG                                                                                                          
 CMAKE_CXX_FLAGS_RELWITHDEBINFO   -O2 -g                                                                                                                
 CMAKE_C_COMPILER                 /usr/bin/gcc                                                                                                          
 CMAKE_C_FLAGS                                                                                                                                          
 CMAKE_C_FLAGS_DEBUG              -g                                                                                                                    
 CMAKE_C_FLAGS_MINSIZEREL         -Os -DNDEBUG                                                                                                          
 CMAKE_C_FLAGS_RELEASE            -O3 -DNDEBUG                                                                                                          
 CMAKE_C_FLAGS_RELWITHDEBINFO     -O2 -g                                                                                                                
 CMAKE_EXE_LINKER_FLAGS                                                                                                                                 
 CMAKE_EXE_LINKER_FLAGS_DEBUG                                                                                                                           
CMAKE_EXE_LINKER_FLAGS_MINSIZE                                                                                                                         
 CMAKE_EXE_LINKER_FLAGS_RELEASE                                                                                                                         
 CMAKE_EXE_LINKER_FLAGS_RELWITH                                                                                                                         
 CMAKE_INSTALL_PREFIX             /usr/local                                                                                                            
 CMAKE_LINKER                     /usr/bin/ld                                                                                                           
 CMAKE_MAKE_PROGRAM               /usr/bin/make                                                                                                         
 CMAKE_MODULE_LINKER_FLAGS                                                                                                                              
 CMAKE_MODULE_LINKER_FLAGS_DEBU                                                                                                                         
 CMAKE_MODULE_LINKER_FLAGS_MINS                                                                                                                         
 CMAKE_MODULE_LINKER_FLAGS_RELE                                                                                                                         
 CMAKE_MODULE_LINKER_FLAGS_RELW                                                                                                                         
 CMAKE_NM                         /usr/bin/nm                                                                                                           
 CMAKE_OBJCOPY                    /usr/bin/objcopy                                                                                                      
 CMAKE_OBJDUMP                    /usr/bin/objdump                                                                                                      
 CMAKE_RANLIB                     /usr/bin/ranlib                                                                                                       
 CMAKE_SHARED_LINKER_FLAGS                                                                                                                              
 CMAKE_SHARED_LINKER_FLAGS_DEBU         
 CMAKE_SHARED_LINKER_FLAGS_MINS                                                                                                                         
 CMAKE_SHARED_LINKER_FLAGS_RELE                                                                                                                         
 CMAKE_SHARED_LINKER_FLAGS_RELW                                                                                                                         
 CMAKE_SKIP_RPATH                 OFF                                                                                                                   
 CMAKE_STRIP                      /usr/bin/strip                                                                                                        
 CMAKE_USE_RELATIVE_PATHS         OFF                                                                                                                   
 CMAKE_VERBOSE_MAKEFILE           OFF                                                                                                                   
 GDCM_DIR                         /usr/local/lib/gdcm-2.0                                                                                               
 ITK_DIR                          /usr/local/lib/InsightToolkit    


and my ITK_USE_SYSTEM_GDCM is ON 
and my GDCM_USE_ITK is OFF

Please I need help, I very don't know which is the problem.

Thank you very much for your interest
Best regards 
Edoardo





-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20100512/67862efb/attachment-0001.htm>


More information about the Insight-users mailing list