# Introduction

ScanImage can continuously detect motion of the currently acquired image relative to a reference image during an active acquisiton.

The motion correction can be used for

# Setup

# Algorithm

ScanImage ships with three algorithms for motion detection:

ScanImage Default | Function | Performance | Description |
---|---|---|---|

Default if GPU acceleration is not available | fftCorr | Good | estimates motion by calculating the fft-based circular cross correlation between the reference image and the current image |

Default if GPU acceleration is available | fftCorrGpu | Excellent | same as fftCorr, but calculates the fft-based cross correlation using the GPU |

fftCorrSideProj | Excellent | first calculates the mean side projections for x and y, then calculates the fft-based circular cross correlation between the side projections of the reference image and the current image; less accurate, but more performant than fftCorr |

The motion correction algorithm is split into two parts:

- Reference image pre-process function
- Motion correction algorithm

Use the Motion Correction Windows to select which motion correction algorithm is used.

# API

The pre-process function is called only when a new reference image is loaded, while the motion correction algorithm is called every time a frame is acquired. This reduces the number of reoccuring calculations. In the fft-based cross correlation, the fft of the reference image is processed once, and then stored for later use. The preprocess function ha two return values:

Return Value | Description |
---|---|

refDisplayImage | (Required) Used as a reference image in the Motion Correction Display Windows |

refImagePreProcessed | (Required) The pre-processed reference data passed to the motion correction algorithm whenever a frame is acquired. (Can be a matrix, struct, class..) |

The motion-correction function expects two inputs:

Input | Description |
---|---|

refImagePreProcessed | pre-processed reference data |

image | image data of current frame |

Output | Required | Data type | Description |
---|---|---|---|

success | Required | logical | Indicates if the motion algorithm found a match between the reference image and the current image |

ijOffset | Required if success==true | \[1x2]\ double | The [i,j] offset between the reference image and the current image |

quality | Can be empty | double | A metric describing the quality/certainty of the match |

cii | Can be empty | \[1xM\]double | A metric showing the quality/certainty of the match for each possible pixelshift in direction i |

cjj | Can be empty | \[1xN]double | A metric showing the quality/certainty of the match for each possible pixelshift in direction j |