Included Richards autofocus plugin in defaults

This commit is contained in:
Joel Collins 2019-01-29 13:54:47 +00:00
parent cc36aa7593
commit d7e19c40e2
3 changed files with 84 additions and 6 deletions

View file

@ -1,6 +1,11 @@
from openflexure_microscope.plugins import MicroscopePlugin
import time
import numpy as np
from .api import IdentifyAPI
from openflexure_microscope.plugins import MicroscopePlugin
from openflexure_microscope.utilities import set_properties
from .focus_utils import sharpness_sum_lap2
from .api import IdentifyAPI, MeasureSharpnessAPI, AutofocusAPI
class Plugin(MicroscopePlugin):
@ -10,6 +15,8 @@ class Plugin(MicroscopePlugin):
api_views = {
'/identify': IdentifyAPI,
'/measure_sharpness': MeasureSharpnessAPI,
'/autofocus': AutofocusAPI,
}
def identify(self):
@ -18,5 +25,34 @@ class Plugin(MicroscopePlugin):
"""
response = "My parent camera is {}, and my parent stage is {}.".format(self.microscope.camera, self.microscope.stage)
print(response)
return response
def autofocus(self, dz, settle=0.5, metric_fn=sharpness_sum_lap2):
"""Perform a simple autofocus routine.
The stage is moved to z positions (relative to current position) in dz,
and at each position an image is captured and the sharpness function
evaulated. We then move back to the position where the sharpness was
highest. No interpolation is performed.
dz is assumed to be in ascending order (starting at -ve values)
"""
camera = self.microscope.camera
stage = self.microscope.stage
with set_properties(stage, backlash=256), stage.lock, camera.lock:
sharpnesses = []
positions = []
camera.annotate_text = ""
for i in stage.scan_z(dz, return_to_start=False):
positions.append(stage.position[2])
time.sleep(settle)
sharpnesses.append(self.measure_sharpness(metric_fn))
newposition = positions[np.argmax(sharpnesses)]
stage.focus_rel(newposition - stage.position[2])
return positions, sharpnesses
def measure_sharpness(self, metric_fn=sharpness_sum_lap2):
"""Measure the sharpness of the camera's current view."""
return metric_fn(self.microscope.camera.array(use_video_port=True, resize=(640,480)))