diff --git a/CMakeExternals/Numpy.cmake b/CMakeExternals/Numpy.cmake index 44dab7165e..6ab02f39c6 100644 --- a/CMakeExternals/Numpy.cmake +++ b/CMakeExternals/Numpy.cmake @@ -1,143 +1,140 @@ if( MITK_USE_Python ) option(MITK_USE_NUMPY "Use the numpy python library" OFF) # always build numpy in this case if(NOT MITK_USE_SYSTEM_PYTHON) set(MITK_USE_NUMPY ON CACHE BOOL "Use the numpy python library" FORCE) endif() if(MITK_USE_NUMPY) # Sanity checks if(DEFINED Numpy_DIR AND NOT EXISTS ${Numpy_DIR}) message(FATAL_ERROR "Numpy_DIR variable is defined but corresponds to non-existing directory") endif() if( NOT DEFINED Numpy_DIR ) set(proj Numpy) set(${proj}_DEPENDENCIES ) set(Numpy_DEPENDS ${proj}) if(NOT MITK_USE_SYSTEM_PYTHON) list(APPEND ${proj}_DEPENDENCIES Python) endif() set(_numpy_env " set(ENV{F77} \"\") set(ENV{F90} \"\") set(ENV{FFLAGS} \"\") set(ENV{ATLAS} \"None\") set(ENV{BLAS} \"None\") set(ENV{LAPACK} \"None\") set(ENV{MKL} \"None\") ") set(_external_python_project ${CMAKE_BINARY_DIR}/mitkExternalPythonProject.cmake) file(WRITE ${_external_python_project} " ${_numpy_env} set(ENV{VS_UNICODE_OUTPUT} \"\") set(ENV{CC} \"${CMAKE_C_COMPILER} ${CMAKE_C_COMPILER_ARG1}\") set(ENV{CFLAGS} \"${CMAKE_C_FLAGS} ${CMAKE_C_FLAGS_RELEASE}\") set(ENV{CXX} \"${CMAKE_CXX_COMPILER} ${CMAKE_CXX_COMPILER_ARG1}\") set(ENV{CXXFLAGS} \"${CMAKE_CXX_FLAGS} ${CMAKE_CXX_FLAGS_RELEASE}\") set(ENV{LDFLAGS} \"${CMAKE_LINKER_FLAGS} ${CMAKE_LINKER_FLAGS_RELEASE}\") function(MITK_PYTHON_BUILD_STEP proj step) set(_command \${ARGN}) message(\"Running \${proj} \${step}:${PYTHON_EXECUTABLE} \${_command}\") execute_process( COMMAND ${PYTHON_EXECUTABLE} \${_command} WORKING_DIRECTORY ${CMAKE_BINARY_DIR}/\${proj}-src RESULT_VARIABLE result OUTPUT_VARIABLE output ERROR_VARIABLE error ) set(output_file \"${CMAKE_BINARY_DIR}/\${proj}_\${step}_step_output.txt\") file(WRITE \${output_file} \${output}) set(error_file \"${CMAKE_BINARY_DIR}/\${proj}_\${step}_step_error.txt\") file(WRITE \${error_file} \${error}) if(NOT \${result} EQUAL 0) message(FATAL_ERROR \"Error in: \${proj}: \${error}\") endif() endfunction() ") # configure step set(_configure_step ${CMAKE_BINARY_DIR}/${proj}_configure_step.cmake) file(WRITE ${_configure_step} "include(\"${_external_python_project}\") file(WRITE \"${CMAKE_BINARY_DIR}/${proj}-src/site.cfg\" \"\") MITK_PYTHON_BUILD_STEP(${proj} configure setup.py config) ") # build step set(_build_step ${CMAKE_BINARY_DIR}/${proj}_build_step.cmake) file(WRITE ${_build_step} "include(\"${_external_python_project}\") MITK_PYTHON_BUILD_STEP(${proj} build setup.py build --fcompiler=none) ") # install step set(NUMPY_CMAKE_INSTALL_DIR ) if(NOT MITK_USE_SYSTEM_PYTHON) - set(_install_dir ${Python_DIR}) + set(_install_dir ${Python_BUILD_DIR}) if(WIN32) STRING(REPLACE "/" "\\\\" _install_dir ${Python_DIR}) endif() else() set(NUMPY_CMAKE_INSTALL_DIR INSTALL_DIR ${CMAKE_BINARY_DIR}/${proj}-install) set(_install_dir "${CMAKE_BINARY_DIR}/${proj}-install") if(WIN32) STRING(REPLACE "/" "\\\\" _install_dir ${CMAKE_BINARY_DIR}/${proj}-install) endif() endif() set(_install_step ${CMAKE_BINARY_DIR}/${proj}_install_step.cmake) file(WRITE ${_install_step} "include(\"${_external_python_project}\") MITK_PYTHON_BUILD_STEP(${proj} install setup.py install --prefix=${_install_dir}) ") #set(Numpy_URL "https://dl.dropboxusercontent.com/u/8367205/ExternalProjects/numpy-1.6.1.tar.gz") #set(Numpy_MD5 "2bce18c08fc4fce461656f0f4dd9103e") set(Numpy_URL "https://dl.dropboxusercontent.com/u/8367205/ExternalProjects/numpy-1.4.1.tar.gz") set(Numpy_MD5 "5c7b5349dc3161763f7f366ceb96516b") ExternalProject_Add(${proj} URL ${Numpy_URL} URL_MD5 ${Numpy_MD5} SOURCE_DIR ${proj}-src PREFIX ${proj}-cmake BUILD_IN_SOURCE 1 ${NUMPY_CMAKE_INSTALL_DIR} CONFIGURE_COMMAND ${CMAKE_COMMAND} -P ${_configure_step} BUILD_COMMAND ${CMAKE_COMMAND} -P ${_build_step} INSTALL_COMMAND ${CMAKE_COMMAND} -P ${_install_step} DEPENDS ${${proj}_DEPENDENCIES} ) if(WIN32) set(Numpy_DIR ${_install_dir}/Lib/site-packages) else() set(Numpy_DIR ${_install_dir}/lib/python2.7/site-packages) endif() set(NUMPY_INCLUDE_DIR ${Numpy_DIR}/numpy/core/include) - - # add Numpy to the python path set(ENV{PYTHONPATH} "${Numpy_DIR}") - else() mitkMacroEmptyExternalProject(${proj} "${proj_DEPENDENCIES}") endif() endif() endif() diff --git a/CMakeExternals/OpenCV.cmake b/CMakeExternals/OpenCV.cmake index 5769e2867d..4681fc9549 100644 --- a/CMakeExternals/OpenCV.cmake +++ b/CMakeExternals/OpenCV.cmake @@ -1,97 +1,99 @@ #----------------------------------------------------------------------------- # OpenCV #----------------------------------------------------------------------------- if(MITK_USE_OpenCV) # Sanity checks if(DEFINED OpenCV_DIR AND NOT EXISTS ${OpenCV_DIR}) message(FATAL_ERROR "OpenCV_DIR variable is defined but corresponds to non-existing directory") endif() set(proj OpenCV) set(proj_DEPENDENCIES) set(OpenCV_DEPENDS ${proj}) if(NOT DEFINED OpenCV_DIR) set(additional_cmake_args -DBUILD_opencv_java:BOOL=OFF ) if(MITK_USE_Python) #message(STATUS "PYTHON_EXECUTABLE: ${PYTHON_EXECUTABLE}") #message(STATUS "PYTHON_DEBUG_LIBRARY: ${PYTHON_DEBUG_LIBRARY}") #message(STATUS "PYTHON_INCLUDE_DIR: ${PYTHON_INCLUDE_DIR}") #message(STATUS "PYTHON_LIBRARY: ${PYTHON_LIBRARY}") list(APPEND additional_cmake_args -DBUILD_opencv_python:BOOL=ON -DBUILD_NEW_PYTHON_SUPPORT:BOOL=ON -DPYTHON_DEBUG_LIBRARY:FILEPATH=${PYTHON_DEBUG_LIBRARY} -DPYTHON_EXECUTABLE:FILEPATH=${PYTHON_EXECUTABLE} -DPYTHON_INCLUDE_DIR:PATH=${PYTHON_INCLUDE_DIR} -DPYTHON_INCLUDE_DIR2:PATH=${PYTHON_INCLUDE_DIR2} -DPYTHON_LIBRARY:FILEPATH=${PYTHON_LIBRARY} #-DPYTHON_LIBRARIES=${PYTHON_LIBRARY} #-DPYTHON_DEBUG_LIBRARIES=${PYTHON_DEBUG_LIBRARIES} ) if(NOT MITK_USE_SYSTEM_PYTHON) list(APPEND proj_DEPENDENCIES Python) + # export python home + set(ENV{PYTHONHOME} "${Python_DIR}") endif() # use the numpy build within mitk if(MITK_USE_NUMPY) list(APPEND proj_DEPENDENCIES Numpy) endif() else() list(APPEND additional_cmake_args -DBUILD_opencv_python:BOOL=OFF -DBUILD_NEW_PYTHON_SUPPORT:BOOL=OFF ) endif() # 12-05-02, muellerm, added QT usage by OpenCV if QT is used in MITK # 12-09-11, muellerm, removed automatic usage again, since this will struggle with the MITK Qt application object if(MITK_USE_QT) list(APPEND additional_cmake_args -DWITH_QT:BOOL=OFF -DWITH_QT_OPENGL:BOOL=OFF -DQT_QMAKE_EXECUTABLE:FILEPATH=${QT_QMAKE_EXECUTABLE} ) endif() set(OpenCV_PATCH_COMMAND ${CMAKE_COMMAND} -DTEMPLATE_FILE:FILEPATH=${MITK_SOURCE_DIR}/CMakeExternals/EmptyFileForPatching.dummy -P ${MITK_SOURCE_DIR}/CMakeExternals/PatchOpenCV-2.4.8.2.cmake) set(opencv_url ${MITK_THIRDPARTY_DOWNLOAD_PREFIX_URL}/OpenCV-2.4.8.2.tar.gz) set(opencv_url_md5 07fa7c1d225ea7fe8eeb1270a6b00e69) ExternalProject_Add(${proj} SOURCE_DIR ${CMAKE_BINARY_DIR}/${proj}-src BINARY_DIR ${proj}-build PREFIX ${proj}-cmake URL ${opencv_url} URL_MD5 ${opencv_url_md5} INSTALL_COMMAND "" PATCH_COMMAND ${OpenCV_PATCH_COMMAND} CMAKE_GENERATOR ${gen} CMAKE_ARGS ${ep_common_args} -DBUILD_DOCS:BOOL=OFF -DBUILD_TESTS:BOOL=OFF -DBUILD_EXAMPLES:BOOL=OFF -DBUILD_DOXYGEN_DOCS:BOOL=OFF -DWITH_CUDA:BOOL=ON ${additional_cmake_args} DEPENDS ${proj_DEPENDENCIES} ) set(OpenCV_DIR ${CMAKE_CURRENT_BINARY_DIR}/${proj}-build) else() mitkMacroEmptyExternalProject(${proj} "${proj_DEPENDENCIES}") endif() endif() diff --git a/CMakeExternals/Python.cmake b/CMakeExternals/Python.cmake index d7d1eb1b70..60feceed7e 100644 --- a/CMakeExternals/Python.cmake +++ b/CMakeExternals/Python.cmake @@ -1,163 +1,163 @@ if( MITK_USE_Python AND NOT MITK_USE_SYSTEM_PYTHON ) # Sanity checks if(DEFINED Python_DIR AND NOT EXISTS ${Python_DIR}) message(FATAL_ERROR "Python_DIR variable is defined but corresponds to non-existing directory") endif() if(NOT DEFINED Python_DIR) set(proj Python) set(proj_DEPENDENCIES ) set(Python_DEPENDS ${proj}) set(PYTHON_MAJOR_VERSION 2) set(PYTHON_MINOR_VERSION 7) set(PYTHON_PATCH_VERSION 3) set(PYTHON_SOURCE_PACKAGE Python-${PYTHON_MAJOR_VERSION}.${PYTHON_MINOR_VERSION}.${PYTHON_PATCH_VERSION}) set(PYTHON_SOURCE_DIR "${CMAKE_BINARY_DIR}/${PYTHON_SOURCE_PACKAGE}") # patch the VS compiler config if(WIN32) set(PYTHON_PATCH_COMMAND PATCH_COMMAND ${CMAKE_COMMAND} -DPYTHON_SOURCE_DIR:PATH=${PYTHON_SOURCE_DIR} -P ${CMAKE_CURRENT_LIST_DIR}/Patch${proj}.cmake) endif() # download the source code ExternalProject_Add(Python-src URL "https://dl.dropboxusercontent.com/u/8367205/ExternalProjects/${PYTHON_SOURCE_PACKAGE}.tgz" URL_MD5 "2cf641732ac23b18d139be077bd906cd" PREFIX ${CMAKE_BINARY_DIR}/${PYTHON_SOURCE_PACKAGE}-cmake SOURCE_DIR "${PYTHON_SOURCE_DIR}" ${PYTHON_PATCH_COMMAND} CONFIGURE_COMMAND "" BUILD_COMMAND "" INSTALL_COMMAND "" ) set(additional_cmake_cache_args ) list(APPEND additional_cmake_cache_args -DBUILTIN_ARRAY:BOOL=ON -DBUILTIN_AUDIOOP:BOOL=ON -DBUILTIN_BINASCII:BOOL=ON -DBUILTIN_BISECT:BOOL=ON -DBUILTIN_BSDB:BOOL=ON -DBUILTIN_BSSDB:BOOL=ON -DBUILTIN_BZ2:BOOL=ON -DBUILTIN_CMATH:BOOL=ON -DBUILTIN_COLLECTIONS:BOOL=ON -DBUILTIN_CODECS_CN:BOOL=ON -DBUILTIN_CODECS_HK:BOOL=ON -DBUILTIN_CODECS_ISO2022:BOOL=ON -DBUILTIN_CODECS_JP:BOOL=ON -DBUILTIN_CODECS_KR:BOOL=ON -DBUILTIN_CODECS_TW:BOOL=ON -DBUILTIN_CPICKLE:BOOL=ON -DBUILTIN_CRYPT:BOOL=ON -DBUILTIN_CSTRINGIO:BOOL=ON -DBUILTIN_CSV:BOOL=ON -DBUILTIN_CTYPES:BOOL=OFF #-DBUILTIN_CTYPES_TEST:BOOL=OFF #-DBUILTIN_CURSES:BOOL=ON -DBUILTIN_DATETIME:BOOL=ON -DBUILTIN_DBM:BOOL=ON -DBUILTIN_ELEMENTTREE:BOOL=ON -DBUILTIN_FCNTL:BOOL=ON -DBUILTIN_FUNCTOOLS:BOOL=ON -DBUILTIN_FUTURE_BUILTINS:BOOL=ON -DBULTIN_GDBM:BOOL=ON -DBUILTIN_GRP:BOOL=ON -DBUILTIN_HASHLIB:BOOL=ON -DBUILTIN_HEAPQ:BOOL=ON -DBUILTIN_HOTSHOT:BOOL=ON -DBUILTIN_IO:BOOL=ON -DBUILTIN_ITERTOOLS:BOOL=ON -DBUILTIN_JSON:BOOL=ON -DBUILTIN_LOCALE:BOOL=ON -DBUILTIN_LSPROF:BOOL=ON -DBUILTIN_MATH:BOOL=ON -DBUILTIN_MMAP:BOOL=ON -DBUILTIN_MULTIBYTECODEC:BOOL=ON -DBUILTIN_MD5:BOOL=ON -DBUILTIN_MULTIPROCESSING:BOOL=ON -DBUILTIN_NIS:BOOL=ON -DBUILTIN_NIT:BOOL=ON -DBUILTIN_OPERATOR:BOOL=ON -DBUILTIN_PARSER:BOOL=ON -DBUILTIN_POSIX:BOOL=ON -DBUILTIN_PWD:BOOL=ON -DBUILTIN_PYEXPAT:BOOL=ON -DBUILTIN_READLINE:BOOL=ON -DBUILTIN_RESOURCE:BOOL=ON -DBULTIN_RANDOM:BOOL=ON -DBUILTIN_SCPROXY:BOOL=OFF -DBUILTIN_SELECT:BOOL=ON -DBUILTIN_SHA:BOOL=ON -DBUILTIN_SHA256:BOOL=ON -DBUILTIN_SHA512:BOOL=ON -DBUILTIN_SOCKET:BOOL=ON -DBUILTIN_SPWD:BOOL=ON -DBUILTIN_SQLITE3:BOOL=OFF -DBUILTIN_SSL:BOOL=ON -DBUILTIN_STROP:BOOL=ON -DBUILTIN_STRUCT:BOOL=ON -DBUILTIN_SUBPROCESS:BOOL=ON -DBUILTIN_SYSLOG:BOOL=ON -DBUILTIN_TERMIOS:BOOL=ON #-DBUILTIN_TESTCAPI:BOOL=OFF -DBUILTIN_TIME:BOOL=ON -DBUILTIN_TKINTER:BOOL=ON -DBUILTIN_UNICODEDATA:BOOL=ON -DBUILTIN_WINREG:BOOL=ON -DBUILTIN_ZLIB:BOOL=ON ) # CMake build environment for python ExternalProject_Add(${proj} URL "https://dl.dropboxusercontent.com/u/8367205/ExternalProjects/python-cmake-buildsystem.tar.gz" URL_MD5 "171090922892acdaf1a155e22765d72d" SOURCE_DIR ${CMAKE_BINARY_DIR}/${proj}-src PREFIX ${proj}-cmake BINARY_DIR ${proj}-build INSTALL_DIR ${proj}-install CMAKE_ARGS ${ep_common_args} -DCMAKE_INSTALL_PREFIX:PATH= CMAKE_CACHE_ARGS -DCMAKE_CXX_COMPILER:FILEPATH=${CMAKE_CXX_COMPILER} -DCMAKE_C_COMPILER:FILEPATH=${CMAKE_C_COMPILER} #-DBUILD_TESTING:BOOL=OFF -DBUILD_SHARED:BOOL=ON -DBUILD_STATIC:BOOL=OFF -DUSE_SYSTEM_LIBRARIES:BOOL=ON ${additional_cmake_cache_args} DEPENDS Python-src ${${proj}_DEPENDENCIES} ) set(Python_DIR "${CMAKE_BINARY_DIR}/${proj}-install") + set(Python_BUILD_DIR "${CMAKE_BINARY_DIR}/${proj}-build") - set(PYTHON_EXECUTABLE "${Python_DIR}/bin/python${CMAKE_EXECUTABLE_SUFFIX}") - + # use the python executable in the build dir for unix systems. The stripped + # ones will cause conflicts if system libraries are present during the build/configure process + # of opencv, since they will try to lookup the sys path first if no lib is directly + # linked with it s path into the executable if(UNIX) - #set(PYTHON_EXECUTABLE "${Python_BUILD_DIR}/bin/python${CMAKE_EXECUTABLE_SUFFIX}") + set(PYTHON_EXECUTABLE "${Python_BUILD_DIR}/bin/python${CMAKE_EXECUTABLE_SUFFIX}") set(PYTHON_INCLUDE_DIR "${Python_DIR}/include/python${PYTHON_MAJOR_VERSION}.${PYTHON_MINOR_VERSION}") - set(PYTHON_LIBRARY "${Python_DIR}/lib/${CMAKE_SHARED_LIBRARY_PREFIX}python${PYTHON_MAJOR_VERSION}.${PYTHON_MINOR_VERSION}${CMAKE_SHARED_LIBRARY_SUFFIX}") - #set(ENV{PYTHONHOME} "${Python_BUILD_DIR}") + set(PYTHON_LIBRARY "${Python_BUILD_DIR}/lib/${CMAKE_SHARED_LIBRARY_PREFIX}python${PYTHON_MAJOR_VERSION}.${PYTHON_MINOR_VERSION}${CMAKE_SHARED_LIBRARY_SUFFIX}") + set(ENV{PYTHONHOME} "${Python_BUILD_DIR}") else() - #set(PYTHON_EXECUTABLE "${Python_DIR}/bin/python${CMAKE_EXECUTABLE_SUFFIX}") + set(PYTHON_EXECUTABLE "${Python_DIR}/bin/python${CMAKE_EXECUTABLE_SUFFIX}") set(PYTHON_INCLUDE_DIR "${Python_DIR}/include") set(PYTHON_LIBRARY "${Python_DIR}/libs/python${PYTHON_MAJOR_VERSION}${PYTHON_MINOR_VERSION}.lib") + set(ENV{PYTHONHOME} "${Python_DIR}") endif() - # export python home - set(ENV{PYTHONHOME} "${Python_DIR}") - - # get the name of the library. The name is used for dynamic loading - # the library on unix systems in development setups + # get the name of the library get_filename_component(PYTHON_LIBRARY_NAME "${PYTHON_LIBRARY}" NAME) else() mitkMacroEmptyExternalProject(${proj} "${proj_DEPENDENCIES}") endif() endif() diff --git a/Modules/Python/CMakeLists.txt b/Modules/Python/CMakeLists.txt index 37dbcc8cb2..b3841e503a 100644 --- a/Modules/Python/CMakeLists.txt +++ b/Modules/Python/CMakeLists.txt @@ -1,22 +1,21 @@ if( MITK_USE_Python ) - if(NOT MITK_USE_SYSTEM_PYTHON) add_definitions( -DUSE_MITK_BUILTIN_PYTHON ) endif() set(OpenCV_DEP ) if(MITK_USE_OpenCV) set(OpenCV_DEP OpenCV) endif() MITK_CREATE_MODULE( DEPENDS MitkCore EXPORT_DEFINE MITK_PYTHON_EXPORT PACKAGE_DEPENDS Qt4|QtGui CTK PythonLibs SimpleITK VTK|vtkPython+vtkWrappingPythonCore ${OpenCV_DEP} INCLUDE_DIRS ${NUMPY_INCLUDE_DIR} ) configure_file(PythonPath.h.in "${CMAKE_CURRENT_BINARY_DIR}/PythonPath.h" @ONLY) add_subdirectory(Testing) endif() diff --git a/Modules/Python/Testing/mitkPythonWrappingTest.cpp b/Modules/Python/Testing/mitkPythonWrappingTest.cpp index 9865b4a644..01f58ffc9b 100644 --- a/Modules/Python/Testing/mitkPythonWrappingTest.cpp +++ b/Modules/Python/Testing/mitkPythonWrappingTest.cpp @@ -1,380 +1,434 @@ /*=================================================================== The Medical Imaging Interaction Toolkit (MITK) Copyright (c) German Cancer Research Center, Division of Medical and Biological Informatics. All rights reserved. This software is distributed WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See LICENSE.txt or http://www.mitk.org for details. ===================================================================*/ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include // vtk cone sample snippet #include // vtk decimate pro snippet #include #include "SimpleITK.h" namespace sitk = itk::simple; namespace mitk { static bool Equal ( mitk::Image* img1, mitk::Image* img2 ); static bool Equal ( mitk::Surface* s1, Surface *s2 ); mitk::Image::Pointer SimpleItkToMitkImage( sitk::Image& sitkImage ); sitk::Image MitkToSimpleItkImage( mitk::Image* image ); } bool mitk::Equal ( mitk::Image* img1, mitk::Image* img2 ) { + MITK_INFO << "hallo"; mitk::ImageReadAccessor ra1(img1); mitk::ImageReadAccessor ra2(img2); const unsigned int* img1Dims = img1->GetDimensions(); if ( img1->GetDimension() != img2->GetDimension() ) + { + MITK_ERROR << "Dimension do not match"; return false; + } if( img1Dims[0] != img2->GetDimensions()[0] || img1Dims[1] != img2->GetDimensions()[1] || img1Dims[2] != img2->GetDimensions()[2] ) + { + MITK_ERROR << "Dimensions do not match"; return false; + } if ( img1->GetPixelType().GetPixelType() != img2->GetPixelType().GetPixelType()) + { + MITK_ERROR << "Pixeltypes do not match"; return false; + } if ( img1->GetGeometry()->GetSpacing()[0] != img2->GetGeometry()->GetSpacing()[0] || img1->GetGeometry()->GetSpacing()[1] != img2->GetGeometry()->GetSpacing()[1] || img1->GetGeometry()->GetSpacing()[2] != img2->GetGeometry()->GetSpacing()[2] ) + { + MITK_ERROR << "Spacing does not match"; return false; + } if ( img1->GetGeometry()->GetOrigin()[0] != img2->GetGeometry()->GetOrigin()[0] || img1->GetGeometry()->GetOrigin()[1] != img2->GetGeometry()->GetOrigin()[1] || img1->GetGeometry()->GetOrigin()[2] != img2->GetGeometry()->GetOrigin()[2] ) + { + MITK_ERROR << "Origin does not match"; return false; + } size_t size = img1Dims[0] * img1Dims[1] * img1Dims[2] * img1->GetPixelType().GetSize(); // bytewise compare the image for ( size_t i = 0; i < size; ++i ) { - if ( ((char*)ra1.GetData())[i] != ((char*)ra2.GetData())[i] ) + if ( ((char*)ra1.GetData())[i] != ((char*)ra2.GetData())[i] ) { + MITK_INFO << "Byte does not match"; return false; + } } return true; } bool mitk::Equal( mitk::Surface* s1, mitk::Surface* s2 ) { vtkPolyData* poly1 = s1->GetVtkPolyData(); vtkPolyData* poly2 = s2->GetVtkPolyData(); double p1[3] = { 0.0,0.0,0.0 }; double p2[3] = { 0.0,0.0,0.0 }; if ( poly1->GetNumberOfPoints() != poly2->GetNumberOfPoints() ) { MITK_WARN << "Size does not match : s1 = " << poly1->GetNumberOfPoints() << ", s2 = " << poly2->GetNumberOfPoints(); return false; } for ( vtkIdType i = 0; i < poly1->GetNumberOfPoints(); ++i ) { poly1->GetPoint(i,p1); poly2->GetPoint(i,p2); if ( !mitk::Equal(p1[0],p2[0]) || !mitk::Equal(p1[1],p2[1]) || !mitk::Equal(p1[2],p2[2]) ) { MITK_WARN << "Points do not match: i: "<< i << "p1(" << p1[0] << "," << p1[1] << "," << p1[2] << "), p2(" << p2[0] << "," << p2[1] << "," << p2[2] << ")" ; return false; } } return true; } sitk::Image mitk::MitkToSimpleItkImage( mitk::Image* image ) { const mitk::Vector3D spacing = image->GetGeometry()->GetSpacing(); mitk::Point3D origin = image->GetGeometry()->GetOrigin(); mitk::PixelType pixelType = image->GetPixelType(); mitk::ImageReadAccessor ra(image); void* buffer = (void*) ra.GetData(); sitk::ImportImageFilter importer; std::vector sitkSpacing; sitkSpacing.push_back(spacing[0]); sitkSpacing.push_back(spacing[1]); sitkSpacing.push_back(spacing[2]); std::vector sitkOrigin; sitkOrigin.push_back(origin[0]); sitkOrigin.push_back(origin[1]); sitkOrigin.push_back(origin[2]); std::vector sitkSize; for ( unsigned int i = 0; i < image->GetDimension(); ++i ) sitkSize.push_back(image->GetDimensions()[i]); importer.SetSpacing(sitkSpacing); importer.SetSize(sitkSize); importer.SetOrigin(sitkOrigin); if( pixelType.GetComponentType() == itk::ImageIOBase::DOUBLE ) { importer.SetBufferAsDouble((double*) buffer); } else if( pixelType.GetComponentType() == itk::ImageIOBase::FLOAT ) { importer.SetBufferAsFloat((float*) buffer); } else if( pixelType.GetComponentType() == itk::ImageIOBase::SHORT) { importer.SetBufferAsInt16((int16_t*) buffer); } else if( pixelType.GetComponentType() == itk::ImageIOBase::CHAR ) { importer.SetBufferAsInt8((int8_t*) buffer); } else if( pixelType.GetComponentType() == itk::ImageIOBase::INT ) { importer.SetBufferAsInt32((int32_t*) buffer); } else if( pixelType.GetComponentType() == itk::ImageIOBase::LONG ) { importer.SetBufferAsInt64((int64_t*) buffer); } else if( pixelType.GetComponentType() == itk::ImageIOBase::UCHAR ) { importer.SetBufferAsUInt8((uint8_t*) buffer); } else if( pixelType.GetComponentType() == itk::ImageIOBase::UINT ) { importer.SetBufferAsUInt32((uint32_t*) buffer); } else if( pixelType.GetComponentType() == itk::ImageIOBase::ULONG ) { importer.SetBufferAsUInt64((uint64_t*) buffer); } else if( pixelType.GetComponentType() == itk::ImageIOBase::USHORT ) { importer.SetBufferAsUInt16((uint16_t*) buffer); } return importer.Execute(); } mitk::Image::Pointer mitk::SimpleItkToMitkImage( sitk::Image& sitkImage ) { mitk::Image::Pointer image = mitk::Image::New(); void* buffer = NULL; mitk::PixelType pixelType = MakeScalarPixelType(); std::vector sitkSpacing = sitkImage.GetSpacing(); double spacing[3] = { sitkSpacing[0], sitkSpacing[1], sitkSpacing[2] }; std::vector sitkOrigin = sitkImage.GetOrigin(); double origin[3] = { sitkOrigin[0], sitkOrigin[1], sitkOrigin[2] }; std::vector sitkSize = sitkImage.GetSize(); unsigned int dimensions[4] = { 1,1,1,1}; for ( size_t i = 0; i < sitkSize.size(); ++i ) dimensions[i] = sitkSize[i]; size_t size = 0; if ( sitkImage.GetPixelIDValue() == sitk::sitkInt8 ) { pixelType = MakeScalarPixelType(); buffer = (void*) sitkImage.GetBufferAsInt8(); size = sizeof(char); } else if( sitkImage.GetPixelIDValue() == sitk::sitkInt16 ) { pixelType = MakeScalarPixelType(); buffer = (void*) sitkImage.GetBufferAsInt16(); size = sizeof(short); } else if( sitkImage.GetPixelIDValue() == sitk::sitkInt32 ) { pixelType = MakeScalarPixelType(); buffer = (void*) sitkImage.GetBufferAsInt32(); size = sizeof(int); } else if( sitkImage.GetPixelIDValue() == sitk::sitkInt64 ) { pixelType = MakeScalarPixelType(); buffer = (void*) sitkImage.GetBufferAsInt64(); size = sizeof(long); } else if( sitkImage.GetPixelIDValue() == sitk::sitkUInt8 ) { pixelType = MakeScalarPixelType(); buffer = (void*) sitkImage.GetBufferAsUInt8(); size = sizeof(unsigned char); } else if( sitkImage.GetPixelIDValue() == sitk::sitkUInt16 ) { pixelType = MakeScalarPixelType(); buffer = (void*) sitkImage.GetBufferAsUInt16(); size = sizeof(unsigned short); } else if( sitkImage.GetPixelIDValue() == sitk::sitkUInt32 ) { pixelType = MakeScalarPixelType(); buffer = (void*) sitkImage.GetBufferAsUInt32(); size = sizeof(unsigned int); } else if( sitkImage.GetPixelIDValue() == sitk::sitkUInt64 ) { pixelType = MakeScalarPixelType(); buffer = (void*) sitkImage.GetBufferAsUInt64(); size = sizeof(unsigned long); } else if( sitkImage.GetPixelIDValue() == sitk::sitkFloat32 ) { pixelType = MakeScalarPixelType(); buffer = (void*) sitkImage.GetBufferAsFloat(); size = sizeof(float); } else if( sitkImage.GetPixelIDValue() == sitk::sitkFloat64 ) { pixelType = MakeScalarPixelType(); buffer = (void*) sitkImage.GetBufferAsDouble(); size = sizeof(double); } image->Initialize(pixelType,sitkImage.GetDimension(),dimensions); image->SetSpacing(spacing); image->SetOrigin(origin); for(size_t i = 0; i < sitkSize.size(); ++i ) size *= sitkSize[i]; mitk::ImageWriteAccessor wa(image); memcpy(wa.GetData(),buffer, size); return image; } class mitkPythonWrappingTestSuite : public mitk::TestFixture { CPPUNIT_TEST_SUITE(mitkPythonWrappingTestSuite); - MITK_TEST(testImageTransfer); + MITK_TEST(testSimpleItkImageTransfer); + MITK_TEST(testCVImageTransfer); MITK_TEST(testSurfaceTransfer); MITK_TEST(testVtkCreateConePythonSnippet); MITK_TEST(testVtkDecimateProPythonSnippet); MITK_TEST(testSimpleITKMedianFilterSnippet); + MITK_TEST(testOpenCVMedianFilter); CPPUNIT_TEST_SUITE_END(); private: mitk::PythonService* m_PythonService; mitk::Image::Pointer m_Image; + mitk::Image::Pointer m_Image2D; mitk::Surface::Pointer m_Surface; QMap m_Snippets; public: void setUp() { //get the context of the python module us::Module* module = us::ModuleRegistry::GetModule("MitkPython"); us::ModuleContext* context = module->GetModuleContext(); //get the service which is generated in the PythonModuleActivator us::ServiceReference serviceRef = context->GetServiceReference(); m_PythonService = dynamic_cast( context->GetService(serviceRef) ); m_Image = mitk::IOUtil::LoadImage(GetTestDataFilePath("Pic3D.nrrd")); + m_Image2D = mitk::IOUtil::LoadImage(GetTestDataFilePath("Png2D-bw.png")); m_Surface = mitk::IOUtil::LoadSurface(GetTestDataFilePath("binary.stl")); QmitkPythonSnippets::LoadStringMap(QmitkPythonSnippets::DEFAULT_SNIPPET_FILE,m_Snippets); } void tearDown() { m_Image = NULL; + m_Image2D = NULL; m_Surface = NULL; } - void testImageTransfer() + void testSimpleItkImageTransfer() { std::string varName("mitkImage"); CPPUNIT_ASSERT_MESSAGE ( "Is SimpleITK Python Wrapping available?", m_PythonService->IsSimpleItkPythonWrappingAvailable() == true ); CPPUNIT_ASSERT_MESSAGE( "Valid image copied to python import should return true.", m_PythonService->CopyToPythonAsSimpleItkImage( m_Image, varName) == true ); mitk::Image::Pointer pythonImage = m_PythonService->CopySimpleItkImageFromPython(varName); CPPUNIT_ASSERT_MESSAGE( "Compare if images are equal after transfer.", mitk::Equal(pythonImage,m_Image) ); } + void testCVImageTransfer() + { + std::string varName("mitkImage"); + CPPUNIT_ASSERT_MESSAGE ( "Is OpenCV Python Wrapping available?", + m_PythonService->IsOpenCvPythonWrappingAvailable() == true ); + + CPPUNIT_ASSERT_MESSAGE( "Valid image copied to python import should return true.", + m_PythonService->CopyToPythonAsCvImage( m_Image2D, varName) == true ); + + mitk::Image::Pointer pythonImage = m_PythonService->CopyCvImageFromPython(varName); + + // todo pixeltypes do not match, cv is changing it + //CPPUNIT_ASSERT_MESSAGE( "Compare if images are equal after transfer.", + // mitk::Equal(pythonImage,m_Image2D) ); + } + void testSurfaceTransfer() { std::string varName("mitkSurface"); CPPUNIT_ASSERT_MESSAGE ( "Is VTK Python Wrapping available?", m_PythonService->IsVtkPythonWrappingAvailable() == true ); CPPUNIT_ASSERT_MESSAGE( "Valid surface copied to python import should return true.", m_PythonService->CopyToPythonAsVtkPolyData( m_Surface, varName) == true ); mitk::Surface::Pointer pythonSurface = m_PythonService->CopyVtkPolyDataFromPython(varName); CPPUNIT_ASSERT_MESSAGE( "Compare if surfaces are equal after transfer.", mitk::Equal(pythonSurface,m_Surface) ); } void testVtkCreateConePythonSnippet() { // cone in cpp mitk::Surface::Pointer mitkSurface = mitk::Surface::New(); vtkSmartPointer coneSrc = vtkSmartPointer::New(); coneSrc->SetResolution(60); coneSrc->SetCenter(-2,0,0); coneSrc->Update(); mitkSurface->SetVtkPolyData(coneSrc->GetOutput()); // run python code CPPUNIT_ASSERT_MESSAGE ( "Is VTK Python Wrapping available?", m_PythonService->IsVtkPythonWrappingAvailable() == true ); m_PythonService->Execute( m_Snippets["vtk: create cone"].toStdString(), mitk::IPythonService::MULTI_LINE_COMMAND ); + CPPUNIT_ASSERT_MESSAGE( "Python execute error occured.", !m_PythonService->PythonErrorOccured()); mitk::Surface::Pointer pythonSurface = m_PythonService->CopyVtkPolyDataFromPython("cone"); CPPUNIT_ASSERT_MESSAGE( "Compare if cones are equal.", mitk::Equal(pythonSurface, mitkSurface) ); } void testVtkDecimateProPythonSnippet() { // decimate pro in cpp mitk::Surface::Pointer mitkSurface = mitk::Surface::New(); vtkSmartPointer deci = vtkSmartPointer::New(); deci->SetInputData(m_Surface->GetVtkPolyData()); deci->SetTargetReduction(0.9); deci->PreserveTopologyOn(); deci->Update(); mitkSurface->SetVtkPolyData(deci->GetOutput()); // decimate pro in python CPPUNIT_ASSERT_MESSAGE ( "Is VTK Python Wrapping available?", m_PythonService->IsVtkPythonWrappingAvailable() == true ); CPPUNIT_ASSERT_MESSAGE( "Valid surface copied to python import should return true.", m_PythonService->CopyToPythonAsVtkPolyData( m_Surface, "mitkSurface") == true ); m_PythonService->Execute( m_Snippets["vtk.vtkDecimatePro"].toStdString(), mitk::IPythonService::MULTI_LINE_COMMAND ); + CPPUNIT_ASSERT_MESSAGE( "Python execute error occured.", !m_PythonService->PythonErrorOccured()); mitk::Surface::Pointer pythonSurface = m_PythonService->CopyVtkPolyDataFromPython("mitkSurface_new"); CPPUNIT_ASSERT_MESSAGE( "Compare if surfaces are equal.", mitk::Equal(pythonSurface, mitkSurface) ); } void testSimpleITKMedianFilterSnippet() { // simple itk median filter in cpp sitk::MedianImageFilter medianFilter; medianFilter.SetRadius(1); sitk::Image sitkImage = medianFilter.Execute(mitk::MitkToSimpleItkImage(m_Image)); mitk::Image::Pointer mitkImage = mitk::SimpleItkToMitkImage(sitkImage); // simple itk median filter in python CPPUNIT_ASSERT_MESSAGE ( "Is SimpleItk Python Wrapping available?", m_PythonService->IsSimpleItkPythonWrappingAvailable() == true ); CPPUNIT_ASSERT_MESSAGE( "Valid image copied to python import should return true.", m_PythonService->CopyToPythonAsSimpleItkImage(m_Image, "mitkImage") == true ); m_PythonService->Execute( m_Snippets["medianfilter"].toStdString(), mitk::IPythonService::MULTI_LINE_COMMAND ); + CPPUNIT_ASSERT_MESSAGE( "Python execute error occured.", !m_PythonService->PythonErrorOccured()); mitk::Image::Pointer pythonImage = m_PythonService->CopySimpleItkImageFromPython("mitkImage_new"); CPPUNIT_ASSERT_MESSAGE( "Compare if images are equal.", mitk::Equal(pythonImage, mitkImage) ); } - //TODO opencv median filter + + //TODO opencv median filter, add cpp test code + void testOpenCVMedianFilter() + { + // simple itk median filter in python + CPPUNIT_ASSERT_MESSAGE ( "Is OpenCV Python Wrapping available?", m_PythonService->IsOpenCvPythonWrappingAvailable() == true ); + + CPPUNIT_ASSERT_MESSAGE( "Valid image copied to python import should return true.", m_PythonService->CopyToPythonAsCvImage(m_Image2D, "mitkImage") == true ); + + m_PythonService->Execute( m_Snippets["opencv median filter"].toStdString(), mitk::IPythonService::MULTI_LINE_COMMAND ); + + CPPUNIT_ASSERT_MESSAGE( "Python execute error occured.", !m_PythonService->PythonErrorOccured()); + } }; MITK_TEST_SUITE_REGISTRATION(mitkPythonWrapping)