Run a MATLAB script from python + pass args
- 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.
- 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
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!