Run a MATLAB script from python + pass args

user1812539 picture user1812539 · Nov 9, 2012 · Viewed 16k times · Source

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 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.

Here are some of my tries:


mlabwrap 1.1 - run a MATLAB-script:

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

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 - Image acqusisition by using mlab functions:

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

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

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

No python 2.7 support


So is there anyone who can help me?

Answer

brentlance picture brentlance · Nov 9, 2012

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!