Merge branch 'simulation-axes' into 'v3'

Defocus and realign simulation axes

See merge request openflexure/openflexure-microscope-server!282
This commit is contained in:
Joe Knapper 2025-06-06 10:46:29 +00:00
commit 13c63f8481

View file

@ -17,6 +17,7 @@ import time
import cv2
import numpy as np
import piexif
from scipy.ndimage import gaussian_filter
from labthings_fastapi.utilities import get_blocking_portal
from labthings_fastapi.decorators import thing_action, thing_property
@ -29,6 +30,10 @@ from pydantic import RootModel
from . import BaseCamera, JPEGBlob
from ..stage import StageProtocol as Stage
# The ratio between "motor" steps and pixels
# higher related to a faster movement
RATIO = 0.2
class ArrayModel(RootModel):
"""A model for an array"""
@ -95,15 +100,24 @@ class SimulatedCamera(BaseCamera):
int(y) - h // 2 : int(y) - h // 2 + h,
] -= self.sprites[int(sprite)]
def generate_image(self, pos: tuple[int, int]):
def generate_image(self, pos: tuple[int, int, int]):
"""Generate an image with blobs based on supplied coordinates"""
cw, ch, _ = self.canvas_shape
w, h, _ = self.shape
tl = (int(pos[0]) - w // 2 - cw // 2, int(pos[1]) - h // 2 - ch // 2)
image = self.canvas[
tuple(slice(tl[i], tl[i] + self.shape[i]) for i in range(2))
+ (slice(None),)
]
canvas_width, canvas_height, _ = self.canvas_shape
image_width, image_height, _ = self.shape
pos = tuple(x * RATIO for x in pos)
top_left = (
int(pos[0]) - image_width // 2 - canvas_width // 2,
int(pos[1]) - image_height // 2 - canvas_height // 2,
)
x_slice = slice(top_left[0], top_left[0] + self.shape[0])
y_slice = slice(top_left[1], top_left[1] + self.shape[1])
z_slice = slice(None)
focused_image = self.canvas[(x_slice, y_slice, z_slice)]
image = gaussian_filter(
focused_image,
sigma=np.abs(pos[2]) / 5,
axes=(0, 1),
)
if image.shape != self.shape:
raise ValueError(
f"Image shape {image.shape} does not match intended shape {self.shape}"
@ -125,8 +139,8 @@ class SimulatedCamera(BaseCamera):
pos = self.get_stage_position()
except Exception as e:
print(f"Failed to get stage position: {e}")
pos = {"x": 0, "y": 0}
return self.generate_image((pos["x"] / 10, pos["y"] / 10))
pos = {"x": 0, "y": 0, "z": 0}
return self.generate_image((pos["y"], pos["x"], pos["z"]))
def __enter__(self):
self._capture_enabled = True