{ "cells": [ { "cell_type": "markdown", "id": "d3fa7666-073e-4b9f-90ee-26e07c7a787f", "metadata": {}, "source": [ "# Visualising Landmarks\n", "\n", "In this notebook we'll use Matplotlib's animation module to generate a video of long axis sequence with predicted landmarks. This assumes the input image has shape `(H, W, 1, T)` for `T` timesteps, and landmarks have shape `(T, 2, 10)` for 10 2D landmarks at each timestep. The included example image is not part of the training dataset, its acquisition parameters and manufacturer vary significantly from those the network was trained with." ] }, { "cell_type": "code", "execution_count": 1, "id": "c3ca2b69-6ab9-42fa-b52d-b32b14e6d3e1", "metadata": {}, "outputs": [], "source": [ "%matplotlib inline\n", "import matplotlib.animation as animation\n", "import matplotlib.pyplot as plt\n", "import nibabel as nib\n", "import numpy as np\n", "from IPython.core.display import HTML\n", "\n", "# load example image and saved predicted landmarks\n", "image = nib.load(\"AMRGAtlas_0031.nii.gz\").get_fdata()\n", "landmarks = np.load(\"AMRGAtlas_0031_key-pred.npy\")" ] }, { "cell_type": "markdown", "id": "92d70190-54dd-4b2b-9ffd-b129d3a784ca", "metadata": {}, "source": [ "The loaded data is visualised as an animated video you can scroll through. Some of the landmarks are predicted quite well though others are off by quite a few pixels. Some points in the cardiac cycle are also predicted better than others. Note that the landmarks which don't appear in this image are correctly clustered in the `(0, 0)` corner." ] }, { "cell_type": "code", "execution_count": 2, "id": "711fdecc-7be5-4ac5-ab07-59dcb3d8d7a7", "metadata": {}, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n", "\n", "\n", "
\n", " \n", "
\n", " \n", "
\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "
\n", " \n", " \n", " \n", " \n", " \n", " \n", "
\n", "
\n", "
\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 2, "metadata": {}, "output_type": "execute_result" } ], "source": [ "fig, ax = plt.subplots(figsize=(10, 10), layout=\"tight\")\n", "ax.axis(\"off\")\n", "frames = []\n", "\n", "for idx in range(image.shape[-1]):\n", " im_slice = image[:, :, 0, idx]\n", " y, x = landmarks[idx]\n", "\n", " frames.append(\n", " [\n", " plt.imshow(im_slice, animated=True, cmap=\"gray\"),\n", " plt.scatter(x, y, s=150, c=np.arange(len(y)), marker=\"+\", linewidths=1.25, cmap=\"tab10\"),\n", " plt.text(0.5, 0.95, f\"Step {idx}\", ha=\"center\", va=\"bottom\", transform=ax.transAxes, size=20, c=\"white\"),\n", " ]\n", " )\n", "\n", "ani = animation.ArtistAnimation(fig, frames, interval=150, repeat_delay=0, blit=True)\n", "plt.close()\n", "\n", "HTML(ani.to_jshtml())" ] } ], "metadata": { "kernelspec": { "display_name": "Python [conda env:monai]", "language": "python", "name": "conda-env-monai-py" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.9.7" } }, "nbformat": 4, "nbformat_minor": 5 }