12

我正在从 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。


尼佩佩

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

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


PyMAT

不支持 python 2.7


那么有没有人可以帮助我?

4

2 回答 2

12

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.

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');

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 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')

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!

于 2012-11-09T22:27:23.887 回答
6
  1. Python/OpenCV:您可以使用本机解决方案从您的视频设备获取图像。使用 OpenCV,您甚至可以进行实时图像处理。
  2. matlab_wrapper:假设您有一个接受一些参数并返回图像数组的 MATLAB函数(不是脚本),例如[img] = get_image(some_parameter),您可以编写如下内容:
matlab = matlab_wrapper.MatlabSession()
img = matlab.workspace.get_image(some_parameter)

免责声明:我是 matlab_wrapper 的作者

于 2014-07-20T13:14:00.957 回答