Skip to content

openflexure/openflexure-microscope-matlab-client

Repository files navigation

A MATLAB client for the OpenFlexure Microscope

These MATLAB classes make it easy for you to connect and control an OpenFlexure Microscope over a network. You are able to move the microscope and get the images from the microscope, as well as run extensions.

Installation

The files can be downloaded from View OpenFlexure Microscope MATLAB Client on File Exchange.

Or you just need to download/clone this repository and make sure the files are added to your MATLAB path.

Usage

To connect to your microscope, you can either:

  • Use microscope.local.

    microscope = OFMClient('microscope.local');
  • Use the microscope's hostname or IP address.

    microscope = OFMClient('example.host.name');
    microscope = OFMClient('XXX.XXX.XXX.XXX');

If necessary you can also set the port (default is 5000):

microscope = OFMClient('example.host.name','port');

Check the connection

To test if your connection has worked, you can run the following block (it is also a script in the repository called check_the_connection.m.):

%Test the stage movement
pos = microscope.position_as_matrix();
starting_pos = pos;
pos(1) = pos(1) + 100;
microscope.move(pos);
disp("Is the microscope in the starting position?");
disp(isequal(microscope.position_as_matrix(),starting_pos));
pos(1) = pos(1) - 100;
microscope.move(pos);
disp("Is the microscope in the starting position?");
disp(isequal(microscope.position_as_matrix(),starting_pos));

%Test the autofocus
ret = microscope.autofocus();

%Acquire an image
image = microscope.grab_image();
figure;
imagesc(image);

%List the extensions
disp("Active microscope extensions");
disp(microscope.extensions);

This will test the stage movement, run the autofocus routine, show a picture taken by the microscope and list the extensions that are on the microscope.

See a live video preview

You can view a video preview in MATLAB using

microscope.preview();

This will launch a window with the video preview direct from the microscope. To use this functionality you will need to install the IP Camera Support from MATLAB. MATLAB should prompt you to install this on first use.

Basic Commands

The MicroscopeClient object has a few basic methods. If you created an instance of the object with the name microscope as above, these are the commands:

Command Input arguments Output arguments Description
microscope.position() A struct with fields x, y and z. The microscope stage's current position.
microscope.position_as_matrix() A 1x3 matrix of the form [x y z]. The microscope stage's current position.
microscope.move(position) Either a 1x3 matrix of the form [x y z] or a struct with the fields x, y, z. Moves the stage to the absolute position.
microscope.move_rel(position) Either a 1x3 matrix of the form [x y z] or a struct with the fields x, y, z. Moves the stage relative to the current position.
microscope.capture_image() A [image_height x image_width x 3] uint8 array Takes a non persistant image capture
microscope.grab_image() A [image_height x image_width x 3] uint8 array Gets the next image the camera sends in its MJPEG preview stream.
microscope.calibrate_xy() Untested.
microscope.autofocus() Runs the fast autofocus routine.
microscope.record_video(format, framerate, length, save_locally, save_location) format: video format (h264 and mjpeg tested) (default: h264)
framerate: video framerate (fps) (default: 30)
length: length of video (seconds) (default: 1)
save_locally: Logical on whether to save video locally (default: true)
save_location: The directory in which to save the video (filename is the timestamped one generated by the microscope) (default: current working directory)
The location (either locally or on the microscope) where the video has been saved Records a video using the video extension.
microscope.preview() Launches a preview window displaying a live video feed from the microscope.

For more information, run help OFMClient.

Extensions

To run methods provided by the microscope extensions, you can use the extensions struct to make get or post requests.

However, due to MATLAB struct limitation, you need to replace any . in your extension name to _DOT_ and any - to _DASH_. For example, the extension org.openflexure.autofocus should be written as org_DOT_openflexure_DOT_autofocus.

For a post request, data can be a struct, character vector, numeric, cell etc. The full list of supported types for post requests is in the MATLAB documentation, but they have not all been tested.

microscope.extensions.your_DOT_extension_DOT_name.link_name.get();
microscope.extensions.your_DOT_extension_DOT_name.link_name.post(data);