diff --git a/Wrapping/Common/mitk_swig_cpp_include.i b/Wrapping/Common/mitk_swig_cpp_include.i index d833c5aaaf..fc7392dd82 100644 --- a/Wrapping/Common/mitk_swig_cpp_include.i +++ b/Wrapping/Common/mitk_swig_cpp_include.i @@ -1,40 +1,61 @@ %{ #include #include #include #include #include #include #include // SWIG Doesn't wrap namespaces. This leads to some problems, if the namespaces are not used. using namespace mitk; using namespace itk; std::vector GetImageSize(mitk::Image::Pointer image) { std::vector< unsigned int > size; unsigned int dimension = image->GetDimension(); for (int i = 0; i < dimension; ++i) { size.push_back(image->GetDimension(i)); } return size; } std::vector GetImageSize(mitk::Image* image) { std::vector< unsigned int > size; unsigned int dimension = image->GetDimension(); for (int i = 0; i < dimension; ++i) { size.push_back(image->GetDimension(i)); } return size; } + +struct TypeDefinitions +{ + static const int ComponentTypeUInt8 = MapPixelType::value>::IOComponentType; + static const int ComponentTypeInt8 = MapPixelType::value>::IOComponentType; + static const int ComponentTypeUInt16 = MapPixelType::value>::IOComponentType; + static const int ComponentTypeInt16 = MapPixelType::value>::IOComponentType; + static const int ComponentTypeUInt32 = MapPixelType::value>::IOComponentType; + static const int ComponentTypeInt32 = MapPixelType::value>::IOComponentType; + static const int ComponentTypeFloat = MapPixelType::value>::IOComponentType; + static const int ComponentTypeDouble = MapPixelType::value>::IOComponentType; +}; %} std::vector GetImageSize(mitk::Image::Pointer image); std::vector GetImageSize(mitk::Image* image); + +%constant int ComponentTypeUInt8 = TypeDefinitions::ComponentTypeUInt8; +%constant int ComponentTypeInt8 = TypeDefinitions::ComponentTypeInt8; +%constant int ComponentTypeUInt16 = TypeDefinitions::ComponentTypeUInt16; +%constant int ComponentTypeInt16 = TypeDefinitions::ComponentTypeInt16; +%constant int ComponentTypeUInt32 = TypeDefinitions::ComponentTypeUInt32; +%constant int ComponentTypeInt32 = TypeDefinitions::ComponentTypeInt32; +%constant int ComponentTypeFloat = TypeDefinitions::ComponentTypeFloat; +%constant int ComponentTypeDouble = TypeDefinitions::ComponentTypeDouble; \ No newline at end of file diff --git a/Wrapping/Python/MITK.i b/Wrapping/Python/MITK.i index 362033a9ca..da1ce31ea8 100644 --- a/Wrapping/Python/MITK.i +++ b/Wrapping/Python/MITK.i @@ -1,158 +1,135 @@ %module PythonMITK %include %{ #include "mitkNumpyArrayConversion.cxx" %} // Numpy array conversion support %native(_GetMemoryViewFromImage) PyObject *mitk_GetMemoryViewFromImage( PyObject *self, PyObject *args ); %pythoncode %{ HAVE_NUMPY = True try: import numpy except ImportError: HAVE_NUMPY = False def _get_numpy_dtype( mitkImage ): """Given a MITK image, returns the numpy.dtype which describes the data""" if not HAVE_NUMPY: raise ImportError('Numpy not available.') # this is a mapping from MITK's pixel id to numpy's dtype - return numpy.float64 - _mitk_np = {sitkUInt8:numpy.uint8, - sitkUInt16:numpy.uint16, - sitkUInt32:numpy.uint32, - sitkUInt64:numpy.uint64, - sitkInt8:numpy.int8, - sitkInt16:numpy.int16, - sitkInt32:numpy.int32, - sitkInt64:numpy.int64, - sitkFloat32:numpy.float32, - sitkFloat64:numpy.float64, - sitkComplexFloat32:numpy.complex64, - sitkComplexFloat64:numpy.complex128, - sitkVectorUInt8:numpy.uint8, - sitkVectorInt8:numpy.int8, - sitkVectorUInt16:numpy.uint16, - sitkVectorInt16:numpy.int16, - sitkVectorUInt32:numpy.uint32, - sitkVectorInt32:numpy.int32, - sitkVectorUInt64:numpy.uint64, - sitkVectorInt64:numpy.int64, - sitkVectorFloat32:numpy.float32, - sitkVectorFloat64:numpy.float64, - sitkLabelUInt8:numpy.uint8, - sitkLabelUInt16:numpy.uint16, - sitkLabelUInt32:numpy.uint32, - sitkLabelUInt64:numpy.uint64 + _mitk_np = {ComponentTypeUInt8:numpy.uint8, + ComponentTypeUInt16:numpy.uint16, + ComponentTypeUInt32:numpy.uint32, + ComponentTypeInt8:numpy.int8, + ComponentTypeInt16:numpy.int16, + ComponentTypeInt32:numpy.int32, + ComponentTypeFloat:numpy.float32, + ComponentTypeDouble:numpy.float64, } - return _mitk_np[ sitkImage.GetPixelIDValue() ] + return _mitk_np[ mitkImage.GetPixelType().GetComponentType() ] def _get_mitk_pixelid(numpy_array_type): """Returns a SimpleITK PixelID given a numpy array.""" if not HAVE_NUMPY: raise ImportError('Numpy not available.') # This is a Mapping from numpy array types to sitks pixel types. _np_mitk = {numpy.character:sitkUInt8, - numpy.uint8:sitkUInt8, - numpy.uint16:sitkUInt16, - numpy.uint32:sitkUInt32, - numpy.uint64:sitkUInt64, - numpy.int8:sitkInt8, - numpy.int16:sitkInt16, - numpy.int32:sitkInt32, - numpy.int64:sitkInt64, - numpy.float32:sitkFloat32, - numpy.float64:sitkFloat64, - numpy.complex64:sitkComplexFloat32, - numpy.complex128:sitkComplexFloat64 + numpy.uint8:ComponentTypeUInt8, + numpy.uint16:ComponentTypeUInt16, + numpy.uint32:ComponentTypeUInt32, + numpy.int8:ComponentTypeInt8, + numpy.int16:ComponentTypeInt16, + numpy.int32:ComponentTypeInt32, + numpy.float32:ComponentTypeFloat, + numpy.float64:ComponentTypeDouble, } try: return _np_mitk[numpy_array_type.dtype] except KeyError: for key in _np_mitk: if numpy.issubdtype(numpy_array_type.dtype, key): return _np_mitk[key] raise TypeError('dtype: {0} is not supported.'.format(numpy_array_type.dtype)) def _get_sitk_vector_pixelid(numpy_array_type): """Returns a SimpleITK vecotr PixelID given a numpy array.""" if not HAVE_NUMPY: raise ImportError('Numpy not available.') # This is a Mapping from numpy array types to sitks pixel types. _np_sitk = {numpy.character:sitkVectorUInt8, numpy.uint8:sitkVectorUInt8, numpy.uint16:sitkVectorUInt16, numpy.uint32:sitkVectorUInt32, numpy.uint64:sitkVectorUInt64, numpy.int8:sitkVectorInt8, numpy.int16:sitkVectorInt16, numpy.int32:sitkVectorInt32, numpy.int64:sitkVectorInt64, numpy.float32:sitkVectorFloat32, numpy.float64:sitkVectorFloat64, } try: return _np_sitk[numpy_array_type.dtype] except KeyError: for key in _np_sitk: if numpy.issubdtype(numpy_array_type.dtype, key): return _np_sitk[key] raise TypeError('dtype: {0} is not supported.'.format(numpy_array_type.dtype)) # SimplyITK <-> Numpy Array conversion support. #http://www.nickdarnell.com/swig-casting-revisited/ def GetArrayViewFromImage(image): """Get a NumPy ndarray view of a SimpleITK Image. Returns a Numpy ndarray object as a "view" of the SimpleITK's Image buffer. This reduces pixel buffer copies, but requires that the SimpleITK image object is kept around while the buffer is being used. """ if not HAVE_NUMPY: raise ImportError('NumPy not available.') dtype = _get_numpy_dtype( image ) shape = GetImageSize(image); if image.GetPixelType().GetNumberOfComponents() > 1: shape = ( image.GetPixelType().GetNumberOfComponents(), ) + shape imageMemoryView = _PythonMITK._GetMemoryViewFromImage(image) arrayView = numpy.asarray(imageMemoryView).view(dtype = dtype) arrayView.shape = shape[::-1] return arrayView def GetArrayFromImage(image): """Get a NumPy ndarray from a SimpleITK Image. This is a deep copy of the image buffer and is completely safe and without potential side effects. """ # TODO: If the image is already not unique then a second copy may be made before the numpy copy is done. arrayView = GetArrayViewFromImage(image) # perform deep copy of the image buffer return numpy.array(arrayView, copy=True) %} \ No newline at end of file