Created
February 19, 2024 22:16
-
-
Save jmuhlich/59e114243278cf27e7f0720c2c956484 to your computer and use it in GitHub Desktop.
Example for stitching a fileseries with Ashlar
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from ashlar.fileseries import FileSeriesReader | |
from ashlar.reg import EdgeAligner, Mosaic, PyramidWriter | |
import numpy as np | |
import pandas as pd | |
import skimage.exposure, skimage.filters, skimage.io | |
import sys | |
import tifffile | |
input_tif_path = sys.argv[1] | |
pattern = "20231124_203726_S1_C902_P99_N99_F{series}.TIF" | |
print(f"Stitching TIF tiles: {input_tif_path}/{pattern}") | |
reader = FileSeriesReader( | |
input_tif_path, | |
pattern=pattern, | |
width=2, | |
height=2, | |
overlap=0.01, | |
pixel_size=0.120280945, | |
) | |
aligner = EdgeAligner(reader, channel=4, verbose=True) | |
aligner.run() | |
mosaic = Mosaic(aligner, aligner.mosaic_shape, channels=[4], verbose=True) | |
writer = PyramidWriter([mosaic], "output.ome.tif", verbose=True) | |
writer.run() | |
print() | |
print("Wrote output.ome.tif") | |
# Write out CSV with corrected position and shift distance for each FOV. | |
fields = [s for w, s in reader.metadata.all_series] | |
df = pd.DataFrame( | |
np.hstack([aligner.positions, aligner.shifts]), | |
columns=["Position_Y", "Position_X", "Shift_Y", "Shift_X"], | |
index=pd.Series(fields, name="Field"), | |
).sort_index( | |
axis="columns", | |
# Discard roundoff error in the low bits. | |
).round(5) | |
df.to_csv("fov_positions.csv") | |
print("Wrote fov_positions.csv") | |
# Take biggest subresolution without going over 3000 px on the longest side and | |
# write it out in JPEG format as a preview image. | |
out_tiff = tifffile.TiffFile("output.ome.tif") | |
preview_series = next(s for s in out_tiff.series[0].levels if max(s.shape) < 3000) | |
preview_img = preview_series.asarray() | |
vmin = np.exp(skimage.filters.threshold_otsu(np.log(preview_img[preview_img>0]))) | |
vmax = np.percentile(preview_img, 99.9) | |
preview_img = skimage.exposure.rescale_intensity(preview_img, in_range=(vmin, vmax)) | |
preview_img = skimage.util.img_as_ubyte(preview_img) | |
skimage.io.imsave("preview.jpg", preview_img) | |
print("Wrote preview.jpg") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment