diff --git a/Modules/Python/mitkIPythonService.h b/Modules/Python/mitkIPythonService.h index 7976e7e0d6..d534851125 100644 --- a/Modules/Python/mitkIPythonService.h +++ b/Modules/Python/mitkIPythonService.h @@ -1,79 +1,82 @@ /*=================================================================== 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. ===================================================================*/ #ifndef mitkIPythonService_h #define mitkIPythonService_h // mitk #include "mitkPythonExports.h" #include "mitkImage.h" //for microservices #include // Qt #include #include #include namespace mitk { /// /// describes a python variable (data container) /// \see IPythonService::GetVariableStack() /// struct PythonVariable { QString m_Name; QString m_Type; QString m_Value; }; /// /// a PythonCommandObserver gets informed as soon as a python command was issued /// \see IPythonService::AddPythonCommandObserver() /// class PythonCommandObserver { public: virtual void CommandExecuted(const QString& pythonCommand) = 0; }; /// /// The central service for issuing Python Code /// The class also enables to transfer mitk images to python as itk::Image and vice versa /// \see IPythonService::GetVariableStack() /// class MITK_PYTHON_EXPORT IPythonService { public: static const int SINGLE_LINE_COMMAND = 0; static const int MULTI_LINE_COMMAND = 1; static const int EVAL_COMMAND = 2; virtual ~IPythonService(); // leer in mitkIPythonService.cpp implementieren virtual QVariant Execute( const QString& pythonCommand, int commandType = SINGLE_LINE_COMMAND ) = 0; virtual QList GetVariableStack() const = 0; virtual void AddPythonCommandObserver( PythonCommandObserver* observer ) = 0; virtual void RemovePythonCommandObserver( PythonCommandObserver* observer ) = 0; virtual void NotifyObserver( const QString& command ) = 0; virtual bool CopyToPythonAsItkImage( mitk::Image* image, const QString& varName ) = 0; virtual mitk::Image::Pointer CopyItkImageFromPython( const QString& varName ) = 0; + + virtual bool CopyToPythonAsCvImage( mitk::Image* image, const QString& varName ) = 0; + virtual mitk::Image::Pointer CopyCvImageFromPython( const QString& varName ) = 0; }; } US_DECLARE_SERVICE_INTERFACE(mitk::IPythonService, "org.mitk.services.IPythonService") #endif diff --git a/Modules/Python/mitkPythonService.cpp b/Modules/Python/mitkPythonService.cpp index a167b03030..d2a40c8e7a 100644 --- a/Modules/Python/mitkPythonService.cpp +++ b/Modules/Python/mitkPythonService.cpp @@ -1,197 +1,211 @@ /*=================================================================== 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 "mitkPythonService.h" #include #include #include #include const QString mitk::PythonService::m_TmpImageName("temp_mitk_image"); mitk::PythonService::PythonService() { m_PythonManager.initialize(); } mitk::PythonService::~PythonService() { } QVariant mitk::PythonService::Execute(const QString &pythonCommand, int commandType) { { MITK_DEBUG("mitk::PythonService") << "pythonCommand = " << pythonCommand.toStdString(); MITK_DEBUG("mitk::PythonService") << "commandType = " << commandType; } QVariant result; bool commandIssued = true; if(commandType == IPythonService::SINGLE_LINE_COMMAND ) result = m_PythonManager.executeString(pythonCommand, ctkAbstractPythonManager::SingleInput ); else if(commandType == IPythonService::MULTI_LINE_COMMAND ) result = m_PythonManager.executeString(pythonCommand, ctkAbstractPythonManager::FileInput ); else if(commandType == IPythonService::EVAL_COMMAND ) result = m_PythonManager.executeString(pythonCommand, ctkAbstractPythonManager::EvalInput ); else commandIssued = false; if(commandIssued) this->NotifyObserver(pythonCommand); return result; } QList mitk::PythonService::GetVariableStack() const { QList list; PyObject* dict = PyImport_GetModuleDict(); PyObject* object = PyDict_GetItemString(dict, "__main__"); PyObject* dirMain = PyObject_Dir(object); PyObject* tempObject; if(dirMain) { QString attr, attrValue, attrType; for(int i = 0; iob_type->tp_name; if(PyUnicode_Check(tempObject) || PyString_Check(tempObject)) attrValue = PyString_AsString(tempObject); else attrValue = ""; mitk::PythonVariable var; var.m_Name = attr; var.m_Value = attrValue; var.m_Type = attrType; list.append(var); } } return list; } void mitk::PythonService::AddPythonCommandObserver(mitk::PythonCommandObserver *observer) { if(m_Observer.contains(observer)) m_Observer.append(observer); } void mitk::PythonService::RemovePythonCommandObserver(mitk::PythonCommandObserver *observer) { m_Observer.removeOne(observer); } void mitk::PythonService::NotifyObserver(const QString &command) { foreach(mitk::PythonCommandObserver* observer, m_Observer) { observer->CommandExecuted(command); } } QString mitk::PythonService::GetTempImageName(const QString& ext) const { QString tmpFolder = QDir::tempPath(); QString fileName = tmpFolder + QDir::separator() + m_TmpImageName + ext; return fileName; } bool mitk::PythonService::CopyToPythonAsItkImage(mitk::Image *image, const QString &varName) { // save image QString fileName = this->GetTempImageName( QString::fromStdString(mitk::IOUtil::DEFAULTIMAGEEXTENSION) ); MITK_DEBUG("PythonService") << "Saving temporary file " << fileName.toStdString(); if( !mitk::IOUtil::SaveImage(image, fileName.toStdString()) ) { MITK_ERROR << "Temporary file could not be created."; } else { // TODO CORRECT TYPE SETUP, MAKE MITK_DEBUG("PythonService") MITK_DEBUG("PythonService") int dim = 3; QString type = "US"; QString command; command.append( QString("import itk\n") ); command.append( QString("imageType = itk.Image[%1, %2]\n") .arg( type ).arg( dim ) ); command.append( QString("readerType = itk.ImageFileReader[imageType]\n") ); command.append( QString("reader = readerType.New()\n") ); command.append( QString("reader.SetFileName( \"%1\" )\n") .arg(fileName) ); command.append( QString("reader.Update()\n") ); command.append( QString("%1 = reader.GetOutput()\n").arg( varName ) ); MITK_DEBUG("PythonService") << "Issuing python command " << command.toStdString(); this->Execute(command, IPythonService::MULTI_LINE_COMMAND ); QFile file(fileName); MITK_DEBUG("PythonService") << "Removing file " << fileName.toStdString(); file.remove(); return true; } return false; } mitk::Image::Pointer mitk::PythonService::CopyItkImageFromPython(const QString &varName) { mitk::Image::Pointer mitkImage; QString command; QString fileName = GetTempImageName( QString::fromStdString( mitk::IOUtil::DEFAULTIMAGEEXTENSION ) ); MITK_DEBUG("PythonService") << "Saving temporary file with python itk code " << fileName.toStdString(); command.append( QString("import itk\n") ); command.append( QString( "writer = itk.ImageFileWriter[ %1 ].New()\n").arg( varName ) ); command.append( QString( "writer.SetFileName( \"%1\" )\n").arg(fileName) ); command.append( QString( "writer.SetInput( %1 )\n").arg(varName) ); command.append( QString( "writer.Update()\n" ) ); MITK_DEBUG("PythonService") << "Issuing python command " << command.toStdString(); this->Execute(command, IPythonService::MULTI_LINE_COMMAND ); try { MITK_DEBUG("PythonService") << "Loading temporary file " << fileName.toStdString() << " as MITK image"; mitkImage = mitk::IOUtil::LoadImage( fileName.toStdString() ); } catch(std::exception& e) { MITK_ERROR << e.what(); } QFile file(fileName); if( file.exists() ) { MITK_DEBUG("PythonService") << "Removing temporary file " << fileName.toStdString(); file.remove(); } return mitkImage; } +bool mitk::PythonService::CopyToPythonAsCvImage( mitk::Image* image, const QString& varName ) +{ + bool convert = false; + if(image->GetDimension() != 2) + { + MITK_ERROR << "Only 2D images allowed for OpenCV images"; + return convert; + } +} + +mitk::Image::Pointer mitk::PythonService::CopyCvImageFromPython( const QString& varName ) +{ +return 0; +} ctkAbstractPythonManager *mitk::PythonService::GetPythonManager() { return &m_PythonManager; } diff --git a/Modules/Python/mitkPythonService.h b/Modules/Python/mitkPythonService.h index ba21f76595..48c52fd34c 100644 --- a/Modules/Python/mitkPythonService.h +++ b/Modules/Python/mitkPythonService.h @@ -1,49 +1,53 @@ /*=================================================================== 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. ===================================================================*/ #ifndef mitkPythonService_h #define mitkPythonService_h #include "mitkIPythonService.h" #include #include namespace mitk { class PythonService: public itk::LightObject, public mitk::IPythonService { public: PythonService(); ~PythonService(); // leer in mitkIPythonService.cpp implementieren QVariant Execute( const QString& pythonCommand, int commandType = SINGLE_LINE_COMMAND ); QList GetVariableStack() const; void AddPythonCommandObserver( PythonCommandObserver* observer ); void RemovePythonCommandObserver( PythonCommandObserver* observer ); void NotifyObserver( const QString& command ); bool CopyToPythonAsItkImage( mitk::Image* image, const QString& varName ); mitk::Image::Pointer CopyItkImageFromPython( const QString& varName ); + + bool CopyToPythonAsCvImage( mitk::Image* image, const QString& varName ); + mitk::Image::Pointer CopyCvImageFromPython( const QString& varName ); + ctkAbstractPythonManager* GetPythonManager(); protected: QString GetTempImageName(const QString &ext) const; private: QList m_Observer; ctkAbstractPythonManager m_PythonManager; static const QString m_TmpImageName; }; } #endif