Skip to content

Instantly share code, notes, and snippets.

@emilroz
Last active August 29, 2015 14:04
Show Gist options
  • Save emilroz/ffd6defbaa433f750e05 to your computer and use it in GitHub Desktop.
Save emilroz/ffd6defbaa433f750e05 to your computer and use it in GitHub Desktop.
OMERO Matlab toolbox example for binary masks.
% Copyright (C) 2014 Glencoe Software, Inc.
% All rights reserved.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are met:
%
% 1. Redistributions of source code must retain the above copyright notice, this
% list of conditions and the following disclaimer.
% 2. Redistributions in binary form must reproduce the above copyright notice,
% this list of conditions and the following disclaimer in the documentation
% and/or other materials provided with the distribution.
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
% ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
% WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
% DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
% ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
% (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
% LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
% ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
% (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
% SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
% loadOmero;
client = loadOmero('localhost');
session = client.createSession('user', 'pass');
client.enableKeepAlive(60);
image_id = 1;
z_plane = 0;
channel = 0;
time_point = 0;
images = getImages(session, image_id);
plane = getPlane(session, images, z_plane, channel, time_point);
% segment data
background = imopen(plane,strel('disk', 15));
equalised_illumination = plane - background;
increased_contrast = imadjust(equalised_illumination);
level = graythresh(increased_contrast);
bw = im2bw(increased_contrast, level);
bw = bwareaopen(bw, 50);
components = bwconncomp(bw, 4);
% create masks and save to OMERO image.
stats = regionprops(components, 'PixelList');
for k=1:numel(stats)
roi = omero.model.RoiI;
x = stats(k).PixelList(:, 1);
y = stats(k).PixelList(:, 2);
%
x0 = min(x);
y0 = min(y);
width = max(x) - x0 + 1;
height = max(y) - y0 + 1;
binary_mask = zeros(height, width);
%
for j = 1 : numel(x),
x_position = x(j) - x0 + 1;
y_position = y(j) - y0 + 1;
binary_mask(y_position, x_position) = 1;
end
%
mask = createMask(x0 - 1, y0 - 1, binary_mask);
setShapeCoordinates(mask, z_plane, channel, time_point);
mask.setFillColor(rint(2113863680)); %7DFF0000 - 50% red
mask.setStrokeColor(rint(2113863680)); %7DFF0000 - 50% red
mask.setTextValue(rstring('2014a'));
%
roi.addShape(mask);
roi.setImage(omero.model.ImageI(images.getId.getValue, false));
roi = session.getUpdateService().saveAndReturnObject(roi);
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment