Month: June 2016

Non-rigid image alignment in twenty lines of Matlab

We have previously discussed rigid and non-rigid image alignment algorithms for calcium imaging data. If you have the image processing toolbox, a particularly compact solution for non-rigid image registration can be written in ~20 lines of code or so.  The code below returns a displacement field for each frame in the image sequence (output variable ‘disp’) that needs top be used to align the image.  The mean image after alignment is returned as well (output variable ‘m’).  The strategy follows the same recursive approach we used previously for rigid alignment.  The main Matlab function doing the calculation is imregdemons.


function [m,disp] = sbxalign_nonrigid(fname,idx)

if(length(idx)==1)
   A = squeeze(sbxread(fname,idx(1),1)); % just one frame... easy!
   m = A;
   disp = {zeros([size(A) 2])};
elseif (length(idx)==2) % align two frames
   A = squeeze(sbxread(fname,idx(1),1)); % read the frames
   B = squeeze(sbxread(fname,idx(2),1));
   [D,Ar] = imregdemons(A,B,[32 16 8 4],'AccumulatedFieldSmoothing',2.5,'PyramidLevels',4,'DisplayWaitBar',false);
   m = (Ar/2+B/2);
   disp = {D zeros([size(A) 2])};
else
   idx0 = idx(1:floor(end/2)); % split dataset in two
   idx1 = idx(floor(end/2)+1 : end); % recursive alignment
   [A,D0] = sbxalign_nonrigid(fname,idx0);
   [B,D1] = sbxalign_nonrigid(fname,idx1);
   [D,Ar] = imregdemons(A,B,[32 16 8 4],'AccumulatedFieldSmoothing',2.5,'PyramidLevels',4,'DisplayWaitBar',false);
   m = (Ar/2+B/2);
   D0 = cellfun(@(x) (x+D),D0,'UniformOutput',false); % concatenate distortions
   disp = [D0 D1];
end

Once the displacement field is computed one can obtain the aligned images by applying the distortion to each frame individually using imwarp.

The example below shows the original (top) and stabilized sequences in one case where imaging at high magnification clearly leads to non-rigid deformations that need correction.

Fixing a jittery, linear actuator

Do you hear a high-pitched noise when the port camera mirror is in one of the opened and/or closed positions?  If so, it is possible the linear actuator is jittering a bit around its position.

One way to solve this issue is to this install the Firgelli LAC configuration utility under yeti/firgelli.  Follow the instructions in that directory about how to install the Windows 10 driver.  Connect the mirror controller to the computer’s USB port. After it is done, you should be able to launch the configuration utility. Within the configuration utility, in the Direct Control Tab, decrease the accuracy parameter to a value of 94%:

firgelli1

Then, click on the Advanced Configuration tab and click on the Disable Defaults button:

firgelli2

This will save the accuracy settings in the EEPROM and disable the default settings set by the pots on the board.  Now you can quit the configuration utility and disconnect the USB cable from the board.

Problem fixed!

(If not, let us know…)

 

Knobby improvements

We have added a couple of new features to Knobby based on user feedback that some may find useful. These features are presented in different screen pages. You can switch between the three available pages by touching one of the 3 gray disks on the bottom left of the screen.

knobby_v3

The first screen is the familiar position control screen. Here, changes in the position of the dials are interpreted as commands to change the positions of the axes by a certain distance. Use this window to navigate small distances (millimeters) around your sample.

When you need to move long distances (centimeters) between experiments, you can now use the velocity control mode in the second screen (instead of trying to move fast by spinning the knobs quickly). In velocity control mode you can move each of the axes while touching the + or – signs on the screen. The movement will continue at a fixed speed until you stop touching the screen. Velocity control is similar to what is achieved with the 3D mouse.

Warning: The microscope moves very fast in this mode, so make sure you know which direction you are moving and verify that there are no obstacles that will impede the movement. In this mode the knobs are disabled.

The third screen is the z-stack control, which allows knobby to automatically step and cover a given depth, in a given number of steps, while stepping a given number of microscope frames. This allows for an efficient collection of z-stack imagery without starting/stopping the microscope.

For Knobby to have access to the microscope frame timing you need to connect the left SMA connector (the one closer to the screen) to the CAM0 trigger output of Scanbox. The parameter values can be changed by moving the X (range), Y (steps), Z (frames) and A (arm/disarm) knobs.  The variable range represents the total travel length you want the z-stack to cover (values can be positive or negative).  The variable steps represents the total number of slices in the z-stack.  For example, if range = 100um and steps = 11, then the spacing between slices is 10um. Finally, the variable frames represents how many frames the microscope will acquire per slice.

After the parameters are set and z-stack armed, switch back to position mode before imaging. To have the best possible resolution set the position step size to S-Fine. If the objective is rotated away from vertical you can choose normal or rotated mode to decide how to move.  In rotated mode the steps will be along the axis of the objective. Now start imaging. You will notice that Knobby will automatically as selected by the parameter values.  During scanning Knobby will display changes in Z (or X and Z in rotated mode), stop once the range has been covered, and disarm automatically. You can use this feature to acquire z-stacks if you have a need to cover a range larger than what you can achieve with the optotune alone.

Here is an example of a z-stack acquired with knobby with 81 planes at 5 um separation:

To use the latest features just update the software and the knobby firmware.

Play with these options and familiarize yourself with their use before using them in an actual experiment. Let us know if you have any questions.