Scripting Resources for DigitalMicrograph™

banner

Dave Mitchell's DigitalMicrograph™ Scripting Website

Home | Scripts | Examples | Functions | Recent Updates | Tutorials | Resources | Publications | Consulting | Projects | Contact & Bio |PyJEM| Search

 

Function: Compute 2D Polynomial Fit
Function
This function computes a 2D polynomial fit mapping the relationship between two 2D arrays of points.
Version
version:20190905, v1.0
Author
D. R. G. Mitchell
Acknowledgements
-
Comments

This functions uses the pseudoiverse matrix technqiue to compute fit parameters for a fifth order polynomial to map the relationship between two 2D arrays of data. In this example the input data are the x,y coordinates of a STEM scan (square raster) and the output data are the resulting x',y' coordinates of the (000) diffraction spot (descanning not applied). Due to the effects of projector lens distortion, the square array becomes distorted in the output data (looks like it is projected onto a spherical surface). By computing the best polynomial fit to the data, it enables the output coordinates to be calculated for any given set of input coordinates. The method uses the pseudoinverse matrix method for fitting and is therefore very computationally efficient, even for large data sets.

Two sets of data (input and output) have been hard coded into the script as examples. When run, the script will use this data and call the fitting function. The function returns two arrays which contain the six a and b polynomial fit variables needed for computing the x' and y' output data. The script extracts the variables from these arrays and uses them to calculate the output data. The script displays the original output data, the calculated output data and the difference of those two images - to highlight how good (or bad) the fit is.

The STEM data hard coded here was taken at the lowest STEM magnification possible (20kx). Under these conditions, the (barrel) distortion in the position of the (000) spot (output data) is at its most severe. With increasing magnification, the (000) spot displacement becomes smaller and so does the distortion. At 20kx near the centre of the scan, the difference between original and calculated data is a pixel or two. At 80kx this decreases to <0.1 pixel.

System Requirements
Should be compatible with all recent versions of DigitalMicrograph.
Known Issues
Where extensive deviations in the output data occur (eg in the corners of the scan), the polynomial fits are quite poor. However, near the centre of the scan, where the geometic distortion in the data is small, the fit is much better. This is simply a limitation of fitting higher order polynomials.Calibration data sets were 10 x 10 arrays of x,y points. The effect of including more calibration points (eg 20 x 20) has not been investigated, but may produce a better (more constrained) fit.
Supported
Yes
Included Files
Main script file.
Source Code

See attached script file.