Spot Segmentation¶
This page describes how nrxrdct converts raw Laue detector frames into
detected spot lists, the three available segmentation algorithms, their
key parameters, and how to choose among them.
Prerequisite: a calibrated
Cameraobject and a stack of raw detector frames. See Diffraction and Laue Theory for the detector geometry setup.
1. Overview¶
Each Laue detector frame is a 2-D image containing up to hundreds of diffraction spots on top of fluorescence background, parasitic scattering, and detector noise. The segmentation step converts this image into a list of sub-pixel peak positions (plus shape parameters) that can be passed to the orientation indexer.
The pipeline for a single frame is:
Raw frame
│
▼ gaussian_background() — FFT-based smooth background estimate
│
▼ frame − background — spot-only residual
│
▼ segmentation method — binary mask of candidate spot pixels
│
▼ clean_segmentation() — size filter, gap exclusion, border removal
│
▼ label_segmented_image() — connected-component labelling
│
▼ write_h5_spotsfile() — Gaussian fitting + HDF5 output
At the GrainMap level this pipeline is distributed across SLURM nodes
(via GrainMap.submit_segmentation) or run locally on multiple CPU cores
(via run_segmentation_local).
2. Segmentation methods¶
All three methods share the same signature:
They all begin with a log1p transform and intensity normalisation so their
responses are approximately gain-independent.
2.1 LoG — Laplacian-of-Gaussian¶
LoG_segmentation(image, mask, sigmas, threshold_percentile)
The Laplacian-of-Gaussian acts as a band-pass blob detector. It responds strongly to bright compact objects whose spatial scale matches the filter width:
A local maximum of the response marks the centre of a spot with radius \(r \approx \sqrt{2}\,\sigma\).
| Parameter | Type | Description |
|---|---|---|
sigmas |
float or list | Gaussian σ in pixels. Match to the expected spot radius: σ ≈ FWHM / 2.35. Pass a list for multi-scale detection. |
threshold_percentile |
float | Percentile of the LoG response (within the valid mask) used as the binary threshold. Default 99.9. |
When to use: round or weakly elongated spots of approximately known size. Multi-scale LoG (list of sigmas) handles frames where spot size varies.
from nrxrdct.laue.segmentation import LoG_segmentation
mask = LoG_segmentation(
image,
detector_mask,
sigmas=[1.5, 3.0, 5.0], # three scales in pixels
threshold_percentile=99.8,
)
2.2 WTH — White Top-Hat¶
WTH_segmentation(image, mask, disk_radius, threshold_percentile)
The white top-hat transform removes slowly varying background while keeping compact bright features:
Any structure wider than disk_radius pixels is suppressed; structures
smaller than the disk appear unmodified.
| Parameter | Type | Description |
|---|---|---|
disk_radius |
int or list | Radius of the disk structuring element in pixels. Each radius should be slightly larger than the largest expected spot. Pass a list for multi-scale detection. Default 7. |
threshold_percentile |
float | Percentile of the WTH response used as the binary threshold. Default 99.9. |
When to use: spots sitting on a strong, uneven background (e.g. fluorescence halos, detector module roll-off). Also works well for elongated or asymmetric spots that LoG tends to under-detect.
from nrxrdct.laue.segmentation import WTH_segmentation
mask = WTH_segmentation(
image,
detector_mask,
disk_radius=[5, 9], # two scales
threshold_percentile=99.9,
)
2.3 HYBRID — LoG + WTH union¶
hybrid_segmentation(image, mask, log_sigmas, wth_disk_radius, threshold_percentile)
Runs LoG and WTH concurrently (all scales in one ThreadPoolExecutor),
thresholds each family independently at threshold_percentile, then
combines the two binary masks with a logical OR.
| Parameter | Type | Description |
|---|---|---|
log_sigmas |
float or list | Sigma(s) for the LoG family. Default [2, 4, 8]. |
wth_disk_radius |
int or list | Disk radius/radii for the WTH family. Default [5, 7]. |
threshold_percentile |
float | Applied independently to each family. Default 99.9. |
When to use: frames that contain a mix of round and elongated/sharp spots, or when either method alone leaves significant false negatives.
from nrxrdct.laue.segmentation import hybrid_segmentation
mask = hybrid_segmentation(
image,
detector_mask,
log_sigmas=[2, 4],
wth_disk_radius=[5, 7],
threshold_percentile=99.85,
)
3. Cleaning parameters¶
After segmentation, clean_segmentation post-processes the binary mask:
| Parameter | Default | Description |
|---|---|---|
min_size |
3 |
Minimum spot area in pixels. Objects smaller than this are discarded as noise. |
max_size |
500 |
Maximum spot area in pixels. Objects larger than this (artefacts, beam-stop shadow) are discarded. |
gap_exclude |
3 |
Dilation radius (pixels) around detector gaps. Spots that overlap this zone are removed to avoid bad centroid positions. |
gap_closing |
3 |
Closing radius applied to the valid-pixel mask before the gap dilation. Fills isolated dead pixels so spots near single bad pixels are not incorrectly discarded. Set to 0 to disable. |
4. Background subtraction¶
gaussian_background(image, valid_mask, sigma=251)
Estimates and removes the slowly varying background (fluorescence, parasitic scattering) using an FFT-based large-sigma Gaussian:
where \(W\) is the valid-pixel mask. The normalisation by the smoothed mask ensures correct handling of detector gaps without boundary artefacts.
| Parameter | Default | Description |
|---|---|---|
sigma |
251 |
Gaussian width in pixels. Should be large enough that the background varies slowly on this scale but the kernel does not wash out spot signals — typically 5–20 × the typical spot size. |
The FFT-based implementation (scipy.fft) makes large sigma values
computationally cheap (runtime scales as \(O(N\log N)\), independent of sigma).
5. Spot fitting and HDF5 output¶
After cleaning, each connected component is fitted with a mixture of rotated 2-D Gaussians:
write_h5_spotsfile controls the fitting via two parameters:
| Parameter | Default | Description |
|---|---|---|
max_components |
1 |
Maximum number of Gaussian components to try. Fitting is attempted with 1, 2, … components; the first model that reaches r_squared_min is accepted. |
r_squared_min |
0.9 |
Minimum R² for a fit to be considered successful. Spots that do not reach this threshold are either stored as unfitted (include_unfitted=True) or skipped (False). |
d |
10 |
Half-size of the ROI cropped around each spot centroid (pixels). |
fit_spots |
True |
Set to False to skip Gaussian fitting entirely and store only the weighted centroid from regionprops. Much faster; sufficient for orientation indexing when shape is not needed. |
include_unfitted |
True |
Whether to write spots whose best fit did not reach r_squared_min. Their positions come from the heuristic initialisation, not a converged fit. |
6. Using GrainMap.submit_segmentation¶
The recommended way to run segmentation at scale is through the GrainMap
SLURM pipeline:
from nrxrdct.laue import GrainMap
gmap = GrainMap(camera=cam, map_shape=(ny, nx))
gmap.submit_segmentation(
h5_path="data/scan.h5",
h5_dataset="entry/eiger/data",
seg_dir="seg/",
# ── detector ──────────────────────────────────────
mask_path="mask.npy", # bool array, True = valid pixel
# ── background ────────────────────────────────────
bg_sigma=251,
# ── segmentation method ───────────────────────────
method="LoG", # "LoG" | "WTH" | "HYBRID"
method_kwargs={"sigmas": [2, 4, 8], "threshold_percentile": 99.9},
# ── cleaning ──────────────────────────────────────
min_size=3,
max_size=500,
gap_exclude=3,
gap_closing=3,
# ── spot fitting ──────────────────────────────────
fit_spots=True, # False = centroid only (faster)
max_components=1,
d=10,
# ── SLURM ─────────────────────────────────────────
n_jobs=20,
frames_per_job=50,
slurm_kwargs={"mem": "8G", "time": "01:00:00"},
)
Each SLURM job processes a subset of frames and writes one
seg/frame_{idx:05d}.h5 file per frame.
7. Reading results¶
Convert the per-frame HDF5 files to peak lists for orientation fitting:
from nrxrdct.laue.segmentation import convert_spotsfile2peaklist
# Single frame — returns (N, 9) array sorted by peak intensity
pl = convert_spotsfile2peaklist(
"seg/frame_00100.h5",
r_squared_min=0.9, # only well-fitted spots
include_unfitted=False,
)
# Columns: peak_X, peak_Y, peak_I, fwaxmaj, fwaxmin,
# inclination, Xdev, Ydev, peak_bkg
xy = pl[:, :2] # (N, 2) pixel coordinates for orientation fitting
GrainMap reads these files automatically during
submit_orientation / submit_orientation_mixed.
8. Choosing parameters¶
Method selection¶
- Start with LoG using a scalar
sigmasroughly equal to the typical spot half-width (pixels). Inspect a representative frame. - If many spots are missed due to uneven background, switch to WTH
with
disk_radiusset slightly larger than the largest spot. - Use HYBRID when spots vary widely in morphology or when either method alone misses a significant fraction.
Threshold percentile¶
threshold_percentile=99.9 keeps the top 0.1% of response pixels — a good
starting point for typical Laue maps. If too many false positives appear
(noise blobs passing min_size), raise towards 99.95–99.99. If faint
spots are missed, lower to 99.5–99.8.
Background sigma¶
Set bg_sigma so that the Gaussian kernel is larger than the largest spot
but small enough to follow the background envelope. For a 2048×2048 detector
with spots of 5–20 px, bg_sigma=251 (≈ 250 pixels ≈ 12 % of the frame) is
a reasonable default. For smaller detectors or finer beam, scale accordingly.
Fitting vs centroid-only¶
| Use case | Recommended |
|---|---|
| Orientation indexing only | fit_spots=False — 5–10× faster |
| Strain analysis | fit_spots=True, max_components=1 |
| Overlapping spots / multi-grain pixels | fit_spots=True, max_components=2 |
9. Simulation-guided (orientation-informed) segmentation¶
The blind methods in sections 2–8 treat every frame independently and have
no knowledge of where spots are expected to appear. When a prior orientation
estimate is available — from interactive indexing or a previous fit pass —
simulation_guided_segmentation uses that prediction to restrict the search
to small imagettes centred on each predicted spot position.
9.1 Why use it¶
| Situation | Advantage |
|---|---|
| Faint or small spots | Local SNR filter finds signals buried in global noise |
| Dense patterns | No risk of merging neighbouring spots from different layers |
| Strong parasitic background | Blind threshold is overwhelmed; guided search is not |
| Satellite peaks near a Bragg peak | Multiple local maxima per imagette are all recorded |
The tradeoff is that spots with no predicted position within search_radius
pixels are never found. A bad orientation estimate therefore causes a
complete miss rather than a distorted peak list.
9.2 Single-frame usage¶
from nrxrdct.laue import simulate_laue_stack, simulation_guided_segmentation
import numpy as np
# --- 1. Predict spot positions from the known orientation ----------------
spots = simulate_laue_stack(stack, camera, E_min_eV=5_000, E_max_eV=22_000)
# --- 2. Load image and mask -----------------------------------------------
image = load_frame(...) # (Nv, Nh) float32
mask = np.load("mask.npy").astype(bool) # True = valid pixel
# --- 3. Run guided segmentation -------------------------------------------
peaklist = simulation_guided_segmentation(
image, spots, mask,
outpath="seg/frame_00100.h5", # writes HDF5; omit to get array only
psf_sigma=1.0, # smoothing ~FWHM/2.35
search_radius=8, # search window half-size (px)
min_snr=2.0, # SNR gate
bg_sigma=60.0, # local background subtraction sigma
fit_spots=False, # centroid-only, faster
)
# peaklist: (N, 9) array — same columns as convert_spotsfile2peaklist
The spots argument also accepts a plain (N, 2) NumPy array of [xcam, ycam]
pixel positions if you want to supply predicted positions without running a
full simulation.
9.3 Parameters¶
| Parameter | Default | Description |
|---|---|---|
psf_sigma |
1.0 |
Gaussian smoothing σ (px) in the imagette before peak finding. Set to ≈ FWHM / 2.35 of a typical spot. 0 disables. |
search_radius |
7 |
Half-size (px) of the search box around each prediction. Must exceed the expected position error. |
min_distance |
1 |
Minimum separation (px) between two accepted local maxima within the same imagette. |
min_snr |
1.0 |
Minimum SNR to accept a local maximum. SNR = (peak − annulus median) / annulus MAD. |
bg_sigma |
60.0 |
Gaussian background subtraction σ (px) applied to the full image before imagette extraction. 0 disables. |
fit_spots |
False |
If True, fit a 2-D Gaussian around each accepted local maximum for sub-pixel position. |
d |
10 |
Half-size (px) of the Gaussian fit ROI. |
r_squared_min |
0.8 |
Minimum Gaussian fit R² to accept a fitted result. |
include_unfitted |
True |
Store peaks that pass the SNR gate but whose Gaussian fit failed, using the local-maximum position. |
9.4 Map-scale SLURM execution via LayeredMap¶
For a full scan, LayeredMap.submit_guided_segmentation parallelises
the work across SLURM jobs. The stack's current U matrices are frozen
at submission time — each job simulates spots once and applies the same
predicted positions to all its assigned frames.
# 1. Set the orientation estimate on the stack (from interactive indexing
# or a previous fit pass)
stack.buffer_layers[0].U = U_substrate
lmap = LayeredMap(ny=21, nx=21, stack=stack, h5_path="scan.h5")
# 2. Submit — seg files go to proc/seg/frame_?????.h5
job_ids = lmap.submit_guided_segmentation(
"proc/",
camera,
h5_dataset="1.1/measurement/eiger4m",
mask_path="mask.npy",
n_jobs=10,
# --- segmentation parameters ---
search_radius=8,
min_snr=2.5,
psf_sigma=1.0,
bg_sigma=60.0,
fit_spots=False,
)
# 3. Once jobs complete, run orientation fitting on the guided peak lists
lmap.run_orientation_local(
camera,
seg_dir="proc/seg/",
out_dir="proc/ubs/",
max_match_px=[30, 10, 3],
)
lmap.collect("proc/ubs/")
Single orientation for the whole map
submit_guided_segmentation uses a single fixed orientation for
all frames. This is appropriate for samples where orientation varies
little across the map (e.g. a single-crystal substrate). If
orientation varies significantly from pixel to pixel, run a coarse
orientation fit first with run_orientation_local using blind
segmentation, then re-segment with the per-frame orientations stored
in the map arrays.
9.5 Comparison with blind segmentation¶
| Aspect | Blind (LoG / WTH / Hybrid) | Simulation-guided |
|---|---|---|
| Requires prior orientation | No | Yes |
| Finds unexpected spots | Yes | No (only near predictions) |
| Robust to dense patterns | Moderate | High |
| Robust to strong background | Moderate | High |
| Finds satellites / split peaks | Only if above threshold | Yes — multiple maxima per imagette |
| Output format | Same .h5 |
Same .h5 (fully compatible) |