MATLAB and Python

MATLAB functionality can be mixed into Python scripts via the Python package Mlabwrap.

Installing Mlabwrap

To install MlabWrap follow the installation guide and make sure that the paths are set for the environment variables:


Example MATLAB scripts

Below are some sample scripts showing MATLAB being launched from OMERO.scripts. MATLAB functions can also call the OMERO Java language bindings interface to access the server from the MATLAB functions.

Calling a simple MATLAB function

import omero, omero.scripts as script
# import mlabwrap to launch matlab.
from mlabwrap import matlab;
client = script.client("", "Get matrix of random numbers drawn from a uniform distribution",
                        script.Long("x").inout(), script.Long("y").inout())

x = client.getInput("x").val
y  = client.getInput("y").val

# call the matlab rand function via mlabwrap will automatically launch matlab
# if it is not already running on the system and call the rand method.
val = matlab.rand(x,y);
print val

Using the OMERO interface inside MATLAB

This example shows the MATLAB script being called, passed to the client object and accessing the same client instance as the script.

import omero, omero.scripts as script
# import mlabwrap to launch matlab.
from mlabwrap import matlab;
client = script.client("", "Call the matlab projection code",
                        script.String("iceConfig").in(), script.String("user").in(),
                        script.Long("pixelsId").inout(), script.String("method").inout()

iceConfig = client.getInput("pixelsId").val
user = client.getInput("pixelsId").val
password = client.getInput("pixelsId").val
method  = client.getInput("method").val
stack = client.getInput("stack").val;

image = matlab.performProjection(iceConfig, username, password, pixelsId, stack, method);

The MATLAB projection method

function performProjection(iceConfig, username, password, pixelsId, zSection, method)

omerojavaService = createOmeroJavaService(iceConfig, username, password);
pixels = getPixels(omerojavaService, pixelsId);
stack = getPlaneStack(omerojavaService, pixelsId, zSection);
projectedImage = ProjectionOnStack(stack, method);
function [resultImage] = ProjectionOnStack(imageStack,type)

[zSections, X, Y] = size(imageStack);

if(strcmp(type,'mean') || strcmp(type, 'sum'))
    resultImage = squeeze(sum(imageStack));
        resultImage = resultImage./zSections;
    resultImage = squeeze(max(imageStack,[],1));