Virtual detector imaging#
In 4D-STEM, a camera records the whole diffraction pattern at every position of a scanning electron probe. A conventional STEM detector adds up the electrons that land on a fixed region of the pattern; with the whole pattern recorded, you choose that region after the experiment. This is a virtual detector: each scan position gives one pixel of the image. pyEMAPS makes bright field, dark field and annular dark field images this way, and images with a detector of any shape, from a mask. This feature requires License Activation.
See also
Steps 1 to 6 of the notebook adf.ipynb run these steps end to end on a real scan, shown here with its plots and output.
The examples use adftest900.im3, a 4D-STEM test scan of 30 x 30 positions with 512 x 512 pixel patterns, published by EMLab Solutions on Zenodo (472 MB).
Find the direct beam#
Detectors are placed relative to the direct beam, so find it first. Read the scan with
pyemaps.read_image, and take the brightest pixel of the
average pattern:
import numpy as np
import pyemaps
scan = pyemaps.read_image('adftest900.im3', scan_shape=(30, 30))
mean = scan.data.mean(axis=0) # the average of the 900 patterns
row, col = np.unravel_index(mean.argmax(), mean.shape)
center = (float(col), float(row)) # (column, row), as pyEMAPS expects
In adftest900.im3 the direct beam is at column 263, row 263, not at the middle of the pattern, and its intensity falls by a factor of ten within about 10 pixels of it. An .im3 file doesn’t record the scan’s shape, so scan_shape gives it.
Bright field, dark field and annular dark field#
StackImage.generateBDF adds up each
pattern’s pixels inside a disk or ring: center is the detector’s center, (column, row),
and rads its inner and outer radius, both in pixels. scol is the number of scan positions
per row, the width of the image.
from pyemaps import EM_INT, StackImage
detectors = {
'BF': (center, (0.0, 8.0)), # a disk on the direct beam
'ADF': (center, (50.0, 200.0)), # a ring around it
'DF': ((1.0, 256.0), (0.0, 200.0)), # a disk off the direct beam
}
for name, (c, rads) in detectors.items():
ret, data = StackImage('adftest900.im3', ndtype=EM_INT).generateBDF(
center=c, rads=rads, scol=30)
image = np.array(data, dtype=float).reshape(-1, 30) # 30 x 30 pixels
ret is 0 on success. Pass bShow=True to display the image as well, and title to name
it, such as title='Annular Dark Field'. Without title, the image is titled by its detector,
such as “Ring Detector, Radii 50-200 px, Center (263, 263)”: whether a detector gives bright or
dark field depends on where the direct beam is, which pyEMAPS doesn’t know.
Bright field (BF): a disk on the direct beam, a little smaller than it. It records the electrons that pass the sample unscattered.
Annular dark field (ADF): a ring around the direct beam. It records the electrons scattered to larger angles, so it shows the contrast of BF reversed: on adftest900.im3 the two images have a correlation of -0.72.
Dark field (DF): a disk that leaves out the direct beam, here one centred at the pattern’s left edge. Its image depends on which part of the pattern it covers.
An inner radius of 0 gives a disk detector, and a positive one a ring. A virtual detector is just a sum over pixels, so NumPy gives the same image; the notebook checks it.
A scan read with StackImage.from_file
knows its scan shape, and scol defaults to its width:
scan = StackImage.from_file('scan.tif', scan_shape=(64, 64))
ret, bf = scan.generateBDF(center=(128.0, 128.0), rads=(0.0, 20.0))
Detector masks#
A detector doesn’t have to be a disk or a ring.
StackImage.generateMaskedImage
takes a mask: an image file the size of a pattern, in pyEMAPS’ .im3 format, whose pixels
above 0.5 belong to the detector.
ret, data = StackImage('adftest900.im3', ndtype=EM_INT).generateMaskedImage('mask.im3', 30)
mask.im3, which comes with the samples, covers six spots on the ring around 60 pixels from the direct beam, a hexagon from one grain’s pattern. Only that grain lights up in the image: a mask on chosen diffraction spots gives a dark field image of the crystal regions that produce them.
To use a mask of your own, draw it as a NumPy array and convert it with
StackImage.convertImage, which saves it in
the sh_converted folder of the data folder and returns its
path:
from pyemaps import E_NPY
yy, xx = np.indices((512, 512))
radius = np.hypot(xx - center[0], yy - center[1])
ring = ((radius >= 55) & (radius <= 70)).astype(np.float32) # a narrow ring
mask_file = StackImage.convertImage(ring, nformat=E_NPY)
ret, data = StackImage('adftest900.im3', ndtype=EM_INT).generateMaskedImage(mask_file, 30)
The sample adf.py makes the annular dark field, dark field and masked images of this page, and convert_image.py converts a raw image file; see Examples.