Created
January 18, 2016 12:47
-
-
Save emilroz/43b8588cbc0d547a09f0 to your computer and use it in GitHub Desktop.
Create masks in OMERO.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import omero | |
from omero.rtypes import rint, rdouble | |
from omero.gateway import ColorHolder | |
from omero.gateway import BlitzGateway | |
import numpy | |
import struct | |
import math | |
def create_mask(mask_bytes, bytes_per_pixel): | |
if bytes_per_pixel == 2: | |
divider = 16.0 | |
format_string = "H" # Unsigned short | |
byte_factor = 0.5 | |
elif bytes_per_pixel == 1: | |
divider = 8.0 | |
format_string = "B" # Unsiged char | |
byte_factor = 1 | |
else: | |
message = "Format %s not supported" | |
raise ValueError(message) | |
steps = math.ceil(len(mask_bytes) / divider) | |
mask = [] | |
for i in range(long(steps)): | |
binary = mask_bytes[ | |
i * int(divider):i * int(divider) + int(divider)] | |
format = str(int(byte_factor * len(binary))) + format_string | |
binary = struct.unpack(format, binary) | |
s = "" | |
for bit in binary: | |
s += str(bit) | |
mask.append(int(s, 2)) | |
return bytearray(mask) | |
def create_mask_roi( | |
update_service, mask_bytes, omero_image, x, y, width, height, color | |
): | |
# Create new ROI | |
roi = omero.model.RoiI() | |
mask = omero.model.MaskI() | |
# Set position | |
mask.setTheC(rint(0)) | |
mask.setTheZ(rint(0)) | |
mask.setTheT(rint(0)) | |
mask.setX(rdouble(mask_position_x)) | |
mask.setY(rdouble(mask_position_y)) | |
mask.setWidth(rdouble(mask_width)) | |
mask.setHeight(rdouble(mask_height)) | |
mask.setFillColor(rint(color.getInt())) | |
mask.setBytes(mask_bytes) | |
roi.addShape(mask) | |
roi.setImage(omero_image._obj) | |
roi = update_service.saveAndReturnObject(roi) | |
return roi.getId().getValue() | |
# Define mask's fill color | |
fill_color = ColorHolder() | |
fill_color.setRed(255) | |
fill_color.setBlue(0) | |
fill_color.setGreen(0) | |
fill_color.setAlpha(100) | |
# Define mask position and size | |
mask_position_x = 50 | |
mask_position_y = 50 | |
mask_width = 100 | |
mask_height = 100 | |
# Create [0, 1] mask | |
mask_array = numpy.random.randint(0, 2, mask_width * mask_height) | |
# Set correct number of bytes per value | |
mask_array = mask_array.astype(numpy.uint8) | |
# Convert the mask to bytes | |
mask_array = mask_array.tobytes() | |
# Pack the bytes to a bit mask | |
mask_packed = create_mask(mask_array, 1) | |
# Connect to the server | |
blitz = BlitzGateway("USERNAME", "PASSWORD", host="HOST", port="PORT") | |
blitz.connect() | |
# Define image you'd like to attach the mask to | |
image_id = 1 | |
# Get the image from the server | |
omero_image = blitz.getObject("Image", image_id) | |
# Get the update service | |
update_service = blitz.getUpdateService() | |
# Attach the mask to the image | |
roi_id = create_mask_roi( | |
update_service, mask_packed, omero_image, mask_position_x, mask_position_y, | |
mask_width, mask_height, fill_color) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment