从python +传递参数运行MATLAB脚本 [英] Run a MATLAB script from python + pass args

查看:408
本文介绍了从python +传递参数运行MATLAB脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Python的MATLAB中寻找.我需要使用MATLAB Image Acquisition Toolbox从摄像机中获取少量图像.

I am looking to from MATLAB from Python. I need to use the MATLAB Image Acquisition Toolbox to acquire few images from a video camera.

MATLAB似乎是一个不错的解决方案,因为图像采集很容易,之后我必须做一些图像处理.我已经搜索了很长时间,但是仍然没有找到任何可用于Python的方法.

MATLAB seems to be a nice solution because the Image Acquisition is easy and i have to do some image processing afterwards. I have searched for a long time but I still haven't found anything working to do this from Python.

这是我的一些尝试:

mlabwrap 1.1-运行MATLAB脚本:

像这样的MATLAB脚本:

A MATLAB script like:

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

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

You can run this script by using:

mlab.run('script.m')

但是,在哪里传递一些参数(目录,图像描述等)? 由于mlabwraps纪录片不佳,我什么也没找到. 我没有成功使用mlab.lookfor('theme of interest')函数

But, where to pass some arguments(directory, image description, etc)? I haven't found anything because of mlabwraps poor documentary. I've used the mlab.lookfor('theme of interest') function without success

mlabwrap 1.1-通过使用mlab函数进行图像采集:

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

At first sight no possibility to read out an "video input object", no functions such as:

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


python-matlab-bridge

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

我已经将Windows7 64位作为操作系统.他们说,它仅适用于Unix.

I've got Windows7 64 Bit as OS. They say, its only working on unix.

Nipype

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

似乎很新.我还没有尝试安装它.我想这似乎适合我的问题,但不适合Windows.

Seems to be very new. I havent tried to install it. It seems to be fitting to my problem but not to windows, i guess.

PyMAT

不支持python 2.7

No python 2.7 support

那么有人可以帮助我吗?

推荐答案

虽然我对python-matlab-bridge,Nipype或PyMAT不太熟悉,但是我对mlabwrap做了大量工作,而我会尝试回答有关该软件包的问题.

While I'm not very familiar with python-matlab-bridge, Nipype, or PyMAT, I've done a fair amount of work with mlabwrap, and I'll try and answer your question with regards to that package.

首先,如果您使用函数而不是脚本来进行工作,将会容易得多.让我们像这样在myFunction.m中将Matlab脚本重播为一个函数:

First, it will be a lot easier if you work in terms of functions, instead of scripts. Let's recast your Matlab script as a function, in myFunction.m like so:

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调用此函数,为函数参数传入字符串. mlabwrap.mlab模块中的所有Matlab函数(包括用户定义的函数)都可以作为属性使用.

You can then call this function from python using mlabwrap.mlab, passing in strings for the function arguments. All Matlab functions, including user-defined functions, are available as attributes from the mlabwrap.mlab module.

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

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

mlabwrap will convert your strings into a Matlab-readable format, and pass them to your function as arguments. If an AttributeError is raised, that usually means that your function is not on the Matlab path. You can add it with the command:

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

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

Just as a cautionary note, mlabwrap will automatically convert some argument types, such as strings or numpy arrays back and forth between Python and Matlab. However, there are many types, such as Matlab structs and classes, that it cannot convert. In this case, it will return an MLabObjectProxy from the Matlab function. These proxy objects cannot be manipulated in Python or converted into Python types, but can be successfully passed through mlabwrap into other Matlab functions. Often, for functions with complex output, it is better to write that output to a file within the Matlab function, and import the data from the file on the Python side. Good luck!

这篇关于从python +传递参数运行MATLAB脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆