python - 从 python 运行 MATLAB 脚本 + 传递参数

标签 python matlab wrapper mlabwrap

我希望从 Python 到 MATLAB。我需要使用 MATLAB Image Acquisition Toolbox 从摄像机中采集少量图像。

MATLAB 似乎是一个很好的解决方案,因为图像采集很容易,之后我必须做一些图像处理。我已经搜索了很长时间,但仍然没有找到任何可以从 Python 中执行此操作的方法。

以下是我的一些尝试:


mlabwrap 1.1 - 运行 MATLAB 脚本:

MATLAB 脚本如下:

vid = videoinput('testadaptor');
img = getsnapshot(vid);
imwrite(img,'./image.png','png');

您可以使用以下命令运行此脚本:

mlab.run('script.m')

但是,在哪里传递一些参数(目录、图像描述等)? 我还没有找到任何东西,因为 mlabwraps 的纪录片很差。 我使用了 mlab.lookfor('theme of interest') 函数但没有成功


mlabwrap 1.1 - 使用 mlab 函数获取图像:

乍一看不可能读出“视频输入对象”,没有以下功能:

image = getsnapshot(video input object)
imwrite(image,'directiory\image.png','png')

python-matlab-bridge

https://github.com/jaderberg/python-matlab-bridge

我的操作系统是 Windows7 64 位。他们说,它只适用于 unix。


Nipype

http://nipy.sourceforge.net/nipype/api/generated/nipype.interfaces.matlab.html

看起来很新。我还没有尝试安装它。我猜它似乎适合我的问题,但不适合 Windows。


PyMAT

不支持 python 2.7


那么有谁可以帮助我吗?

最佳答案

虽然我对 python-matlab-bridge、Nipype 或 PyMAT 不是很熟悉,但我已经用 mlabwrap 做了很多工作,我会尝试回答你关于那个包的问题。

首先,如果您在函数方面而不是脚本方面工作,将会容易得多。让我们在 myFunction.m 中将您的 Matlab 脚本重铸为一个函数,如下所示:

function myFunction(v_input, directory, file_name)

    vid = videoinput(v_input);
    img = getsnapshot(vid);
    location = [directory file_name]
    imwrite(img, location,'png');

然后您可以使用 mlabwrap.mlab 从 python 调用此函数,为函数参数传入字符串。所有 Matlab 函数,包括用户定义的函数,都可以作为 mlabwrap.mlab 模块的属性。

>>> from mlabwrap import mlab
>>> mlab.myFunction('testadaptor', './', 'image.png')

mlabwrap 会将您的字符串转换为 Matlab 可读的格式,并将它们作为参数传递给您的函数。如果出现 AttributeError,这通常意味着您的函数不在 Matlab 路径上。您可以使用以下命令添加它:

>>> mlab.path(mlab.path(), 'C:\function\directory')

作为一个警告,mlabwrap 会自动在 Python 和 Matlab 之间来回转换一些参数类型,例如字符串或 numpy 数组。但是,它无法转换许多类型,例如 Matlab 结构和类。在这种情况下,它将从 Matlab 函数返回一个 MLabObjectProxy。这些代理对象不能在 Python 中操作或转换为 Python 类型,但可以通过 mlabwrap 成功传递到其他 Matlab 函数中。通常,对于具有复杂输出的函数,最好将该输出写入 Matlab 函数内的一个文件,然后在 Python 端从该文件导入数据。 祝你好运!

关于python - 从 python 运行 MATLAB 脚本 + 传递参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13311415/

相关文章:

python - numpy.ndarray : converting to a "normal" class

python - 如何组合 Pandas 系列

python - MySQL 和 Python 生成重复条目错误,我无法解决

java - 使用java生成repmat()方法

java - Java 中的图像处理,如 MATLAB Image Processing Toolbox

javascript - 调整浏览器大小时页面内容(按钮、图像等)消失

java - Java中相同代码的不同输出

python - 将 Python SIGINT 重置为默认信号处理程序

python - 如何在 Visual Studio Code 中创建工作区?

matlab - 保存图形不会保存其标题位置