--- jupytext: text_representation: format_name: myst kernelspec: name: python3 display_name: Python 3 --- (how-to-load-roi)= # Load ImageJ ROI files ImageJ/Fiji saves a selection as a `.roi` file, and the ROI Manager saves several of them as a `.zip` ROI set. Several cell-tracking software programs also export outlines in this format. {func}`~ktch.io.read_roi` reads the ROIs that store a list of coordinates (outlines, open curves and points), and {func}`~ktch.io.write_roi` writes them. ## Make a sample ROI set In this guide, we generate the ROI set from the outlines of two blobs in an image using OpenCV. ```{code-cell} ipython3 import tempfile from pathlib import Path import cv2 import matplotlib.pyplot as plt import numpy as np from ktch.io import read_roi, write_roi image = np.zeros((60, 90), dtype=np.uint8) cv2.ellipse(image, (25, 30), (16, 9), 30, 0, 360, 255, -1) cv2.circle(image, (36, 38), 6, 255, -1) cv2.fillPoly(image, [np.array([[55, 15], [80, 20], [76, 45], [60, 42]])], 255) contours, _ = cv2.findContours(image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) outlines = [contour[:, 0, :] for contour in contours] roi_set = Path(tempfile.mkdtemp()) / "RoiSet.zip" write_roi( roi_set, outlines, roi_type="traced", specimen_names=["1-1", "1-2"], names=["1", "2"], positions=1, ) ``` ## Read the ROIs ```{code-cell} ipython3 rois = read_roi(roi_set) rois ``` Each {class}`~ktch.io.RoiData` holds the coordinates, the ROI type, and the name and stack position the ROI stores. Tracking software, for example, stores the identifier of a cell as the name and its frame as the position. ```{code-cell} ipython3 roi = rois[0] roi.coords[:5], roi.name, roi.position ``` With `as_frame=True`, the coordinates of every ROI come as one DataFrame, indexed by the entry name in the set and the coordinate number: ```{code-cell} ipython3 df = read_roi(roi_set, as_frame=True) df.head() ``` ## Overlay the outlines on the image The coordinates are returned as the file stores them, in pixels with y downward. matplotlib and OpenCV place integer coordinates at pixel centres. Outlines that list their boundary pixels, as these do, therefore overlay without a shift: ```{code-cell} ipython3 fig, ax = plt.subplots() ax.imshow(image, cmap="gray") for roi in rois: closed = np.vstack([roi.coords, roi.coords[:1]]) ax.plot(closed[:, 0], closed[:, 1]) ax.set_axis_off() ``` ImageJ places the integer coordinates of an area ROI at the top-left corner of a pixel. An outline traced in ImageJ with the wand tool follows pixel edges: subtract 0.5 from its coordinates before overlaying it this way. ## Analyse the outlines The coordinates go to the analysis classes as they are: ```{code-cell} ipython3 from ktch.harmonic import EllipticFourierAnalysis efa = EllipticFourierAnalysis(n_harmonics=10) coef = efa.fit_transform([roi.coords for roi in rois]) coef.shape ``` ## Write ROIs {func}`~ktch.io.write_roi` writes one ROI to a `.roi` file, or several to a `.zip` ROI set. It takes coordinate arrays, or the {class}`~ktch.io.RoiData` that {func}`~ktch.io.read_roi` returns: ```{code-cell} ipython3 single = roi_set.with_name("blob.roi") write_roi(single, rois[0]) read_roi(single) ``` Coordinates that are not all integers are written with ImageJ's sub-pixel resolution. ```{seealso} - {func}`~ktch.io.read_roi`, {func}`~ktch.io.write_roi` and {class}`~ktch.io.RoiData` for the details of the format - {doc}`../../tutorials/harmonic/elliptic_Fourier_analysis` for analysing outlines with EFA ```