|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "3fd1ff50", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Segmenting `grains.tif` with Ilastik and Python\n", |
| 9 | + "\n", |
| 10 | + "This proposed solution consists of two steps; first, we separate (approximately) the grain bulks from the grain boundaries using [Ilastik](https://ilastik.org). \n", |
| 11 | + "\n", |
| 12 | + "<video width=\"320\" height=\"240\" controls>\n", |
| 13 | + " <source src=\"./ilastik-recording.mp4\" type=\"video/mp4\">\n", |
| 14 | + "</video>\n", |
| 15 | + "\n", |
| 16 | + "We export the `Simple Segmentation` output from Ilastik as a TIFF image.\n", |
| 17 | + "\n", |
| 18 | + "Then, we read this segmentation mask into Python, and we apply a few simple operations to label the grains using [Scikit-image](https://scikit-image.org). These steps are described below." |
| 19 | + ] |
| 20 | + }, |
| 21 | + { |
| 22 | + "cell_type": "code", |
| 23 | + "execution_count": null, |
| 24 | + "id": "2758b261", |
| 25 | + "metadata": {}, |
| 26 | + "outputs": [], |
| 27 | + "source": [ |
| 28 | + "from pathlib import Path\n", |
| 29 | + "import numpy as np\n", |
| 30 | + "\n", |
| 31 | + "import skimage.io\n", |
| 32 | + "from skimage.morphology import label\n", |
| 33 | + "from skimage.segmentation import watershed\n", |
| 34 | + "from scipy.ndimage import distance_transform_edt" |
| 35 | + ] |
| 36 | + }, |
| 37 | + { |
| 38 | + "cell_type": "code", |
| 39 | + "execution_count": null, |
| 40 | + "id": "19b01b27", |
| 41 | + "metadata": {}, |
| 42 | + "outputs": [], |
| 43 | + "source": [ |
| 44 | + "# Path to the `Simple Segmentation` output from Ilastik:\n", |
| 45 | + "segmentation_file = \"grains_Simple Segmentation.tif\"\n", |
| 46 | + "\n", |
| 47 | + "# Read the grain boundaries mask from Ilastik\n", |
| 48 | + "segmentation = skimage.io.imread(segmentation_file)\n", |
| 49 | + "segmentation = segmentation.astype(np.uint8) # Make sure it's an 8-bit array\n", |
| 50 | + "\n", |
| 51 | + "# Binary mask of the bulk (`background` class):\n", |
| 52 | + "bulk = segmentation == 1\n", |
| 53 | + "\n", |
| 54 | + "# Binary mask of the grain boundaries (`foreground` class)\n", |
| 55 | + "boundaries = segmentation == 2\n", |
| 56 | + "\n", |
| 57 | + "# Label the grain bulks (connected components):\n", |
| 58 | + "labelled_bulk = label(bulk)\n", |
| 59 | + "\n", |
| 60 | + "# Compute an euclidean distance transform in the grain boundary mask:\n", |
| 61 | + "distance_img = distance_transform_edt(boundaries)\n", |
| 62 | + "\n", |
| 63 | + "# Fill the remaining space (the grain boundaries) using a watershed transform:\n", |
| 64 | + "grains_labelled = watershed(-distance_img, markers=labelled_bulk)\n", |
| 65 | + "\n", |
| 66 | + "# Save the labelled image:\n", |
| 67 | + "mask_path = Path(\".\").resolve().parents[1] / \"Submissions\" / \"grains\" / \"example_solution.tif\"\n", |
| 68 | + "\n", |
| 69 | + "skimage.io.imsave(mask_path, grains_labelled)" |
| 70 | + ] |
| 71 | + }, |
| 72 | + { |
| 73 | + "cell_type": "code", |
| 74 | + "execution_count": null, |
| 75 | + "id": "0c7e363d", |
| 76 | + "metadata": {}, |
| 77 | + "outputs": [], |
| 78 | + "source": [] |
| 79 | + } |
| 80 | + ], |
| 81 | + "metadata": { |
| 82 | + "kernelspec": { |
| 83 | + "display_name": "imaging-server-kit", |
| 84 | + "language": "python", |
| 85 | + "name": "python3" |
| 86 | + }, |
| 87 | + "language_info": { |
| 88 | + "codemirror_mode": { |
| 89 | + "name": "ipython", |
| 90 | + "version": 3 |
| 91 | + }, |
| 92 | + "file_extension": ".py", |
| 93 | + "mimetype": "text/x-python", |
| 94 | + "name": "python", |
| 95 | + "nbconvert_exporter": "python", |
| 96 | + "pygments_lexer": "ipython3", |
| 97 | + "version": "3.10.18" |
| 98 | + } |
| 99 | + }, |
| 100 | + "nbformat": 4, |
| 101 | + "nbformat_minor": 5 |
| 102 | +} |
0 commit comments