Run a MATLAB script from python + pass args
Asked Answered
O

2

13

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?

Officeholder answered 9/11, 2012 at 15:55 Comment(1)
I just tried pymatlab , I could run it in windows with some patch (sourceforge.net/p/pymatlab/tickets/1) but in my case i have problems with the data type dimensions (so far it works only with strings)Gaullism
J
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!

Jetsam answered 9/11, 2012 at 22:27 Comment(0)
T
6
  1. Python/OpenCV: you could use the native solution to acquire images from your video device. With OpenCV you can even do real-time image processing.
  2. matlab_wrapper: assuming that you have a MATLAB function (not script) that accepts some parameter and returns image array, e.g. [img] = get_image(some_parameter), you could write something like this:
matlab = matlab_wrapper.MatlabSession()
img = matlab.workspace.get_image(some_parameter)

Disclaimer: I'm the author of matlab_wrapper

Terence answered 20/7, 2014 at 13:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.