Skip to content

Instantly share code, notes, and snippets.

@jmuhlich
Created October 18, 2023 16:41
Show Gist options
  • Save jmuhlich/7a889144735bba5ccfcdeb095a36c70a to your computer and use it in GitHub Desktop.
Save jmuhlich/7a889144735bba5ccfcdeb095a36c70a to your computer and use it in GitHub Desktop.
Ashlar - stitch a single image, only aligning tiles along the stage path (ignoring other overlaps)
import argparse
from ashlar.reg import EdgeAligner, Mosaic, PyramidWriter
from ashlar.scripts.ashlar import build_reader, HelpFormatter
import networkx as nx
parser = argparse.ArgumentParser(
description='Stitch one multi-tile image with strict stage path following',
formatter_class=HelpFormatter,
)
parser.add_argument(
'filepath', metavar='FILE', help='Image file to be processed'
)
parser.add_argument(
'-o', '--output', dest='output', default='ashlar_output.ome.tif',
metavar='PATH', help="Output file, must end in .ome.tif"
)
parser.add_argument(
'-m', '--maximum-shift', type=float, default=15, metavar='SHIFT',
help='Maximum allowed per-tile corrective shift in microns',
)
parser.add_argument(
"--maximum-error", type=float, default=None,
help="Maximum alignment error (skips permutation test)",
)
parser.add_argument(
"--barrel-correction", type=float, default=0,
help="Barrel/pincushion correction parameter"
)
args = parser.parse_args()
print("Stitching and registering input image")
print(f' reading {args.filepath}')
reader = build_reader(
args.filepath,
args.barrel_correction,
)
aligner = EdgeAligner(
reader,
max_shift=args.maximum_shift,
max_error=args.maximum_error,
do_make_thumbnail=False,
verbose=True,
)
n = reader.metadata.num_images
# Override the distance-based neighbors graph with one that only connects tiles
# one-by-one as ordered in the input file. This order is typically the sequence
# in which the stage positions were visited during imaging, but you must verify
# this for your own data.
aligner._neighbors_graph = nx.from_edgelist(zip(range(0, n - 1), range(1, n)))
aligner.run()
print()
print(f"Merging tiles and writing to {args.output}")
mosaic = Mosaic(
aligner,
aligner.mosaic_shape,
verbose=True,
)
writer = PyramidWriter(
[mosaic],
args.output,
verbose=True,
)
writer.run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment