Skip to content

Instantly share code, notes, and snippets.

@will-moore
Created February 10, 2020 15:03
Show Gist options
  • Save will-moore/d2a99efe0fa36192548a3b1ebc1d8a90 to your computer and use it in GitHub Desktop.
Save will-moore/d2a99efe0fa36192548a3b1ebc1d8a90 to your computer and use it in GitHub Desktop.
Finds the brightest point in a plane and draws an ellipse around it. Useful for generating test ROIs.
import omero
from omero.rtypes import rdouble, rint
import omero.grid
from omero.gateway import BlitzGateway
import numpy as np
USERNAME = "user"
PASSWORD = "password"
HOST = "merge-ci-devspace.openmicroscopy.org"
PORT = 4064
plate_id = 13855
conn = BlitzGateway(USERNAME, PASSWORD, host=HOST, port=PORT)
conn.connect()
updateService = conn.getUpdateService()
plate = conn.getObject("Plate", plate_id)
# Channels we want to analyse - 1 ROI created per channel
channels = [0]
print(plate.name)
for well in plate._listChildren():
well = omero.gateway.WellWrapper(conn, well)
image = well.getImage()
pix = image.getPrimaryPixels()
print(image.name, well.row, well.column)
size_x = image.getSizeX()
size_y = image.getSizeY()
for c in channels:
plane = pix.getPlane(theZ=0, theC=c, theT=0)
max_pixel = plane.max()
min_pixel = plane.min()
mean_pixel = (max_pixel + min_pixel) / 2
y = np.argmax(plane) // size_x
x = np.argmax(plane) % size_x
cx = x
cy = y
# walk away from max_pixel until below mean_pixel
# in x direction...
while(x < size_x and plane[y][x] > mean_pixel):
x += 1
x_end = x
x = cx
while(x > 0 and plane[y][x] > mean_pixel):
x -= 1
cx = (x_end + x) // 2
rx = x_end - cx
# and y
while(y < size_y and plane[y][cx] > mean_pixel):
y += 1
y_end = y
y = cy
while(y > 0 and plane[y][cx] > mean_pixel):
y -= 1
cy = (y_end + y) / 2
ry = y_end - cy
print('xy', cx, rx, cy, ry)
ellipse = omero.model.EllipseI()
ellipse.x = rdouble(cx)
ellipse.y = rdouble(cy)
ellipse.radiusX = rdouble(rx)
ellipse.radiusY = rdouble(ry)
ellipse.theZ = rint(0)
ellipse.theT = rint(0)
ellipse.theC = rint(c)
roi = omero.model.RoiI()
roi.setImage(image._obj)
roi.addShape(ellipse)
updateService.saveAndReturnObject(roi)
conn.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment