Created
August 10, 2020 17:05
-
-
Save thewtex/6cceb515f160bbe8146ae348fcf16510 to your computer and use it in GitHub Desktop.
Segmentation image distance
This file contains hidden or 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
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": 1, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "from itkwidgets import view, cm\n", | |
| "import itk\n", | |
| "import numpy as np" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "ground_truth = itk.imread('ground-truth.nii.gz')\n", | |
| "segmentation = itk.imread('segmentation.nii.gz')" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "application/vnd.jupyter.widget-view+json": { | |
| "model_id": "50cdb3876b184ffeb35ef45205204807", | |
| "version_major": 2, | |
| "version_minor": 0 | |
| }, | |
| "text/plain": [ | |
| "Viewer(geometries=[], gradient_opacity=0.22, interpolation=False, point_sets=[], rendered_label_image=<itk.itk…" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "output_type": "display_data" | |
| } | |
| ], | |
| "source": [ | |
| "view(label_image=ground_truth, ui_collapsed=True)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "application/vnd.jupyter.widget-view+json": { | |
| "model_id": "35512f7950534acf9fb9a805e02c6018", | |
| "version_major": 2, | |
| "version_minor": 0 | |
| }, | |
| "text/plain": [ | |
| "Viewer(geometries=[], gradient_opacity=0.22, interpolation=False, point_sets=[], rendered_label_image=<itk.itk…" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "output_type": "display_data" | |
| } | |
| ], | |
| "source": [ | |
| "view(label_image=segmentation, ui_collapsed=True)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "# Cast to signed integer because we will subtract to obtain negative values\n", | |
| "ground_truth_signed = np.asarray(ground_truth).astype(np.int16)\n", | |
| "segmentation_signed = np.asarray(segmentation).astype(np.int16)\n", | |
| "\n", | |
| "subtracted = itk.subtract_image_filter(ground_truth_signed, segmentation_signed)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 6, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "false_positives = itk.binary_threshold_image_filter(subtracted, lower_threshold=1, inside_value=1)\n", | |
| "false_negatives = itk.binary_threshold_image_filter(subtracted, upper_threshold=-1, inside_value=1)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 7, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "distance = itk.signed_maurer_distance_map_image_filter(false_positives,\n", | |
| " inside_is_positive=True)\n", | |
| "false_positive_distances = itk.mask_image_filter(distance, false_positives)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 8, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "distance = itk.signed_maurer_distance_map_image_filter(false_negatives,\n", | |
| " inside_is_positive=True)\n", | |
| "false_negative_distances = itk.mask_image_filter(distance, false_negatives)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 12, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "application/vnd.jupyter.widget-view+json": { | |
| "model_id": "7a6d707c4fab4e708d4562063ddcb5bc", | |
| "version_major": 2, | |
| "version_minor": 0 | |
| }, | |
| "text/plain": [ | |
| "Viewer(cmap=['rainbow'], geometries=[], gradient_opacity=0.22, point_sets=[], rendered_image=<itk.itkImagePyth…" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "output_type": "display_data" | |
| } | |
| ], | |
| "source": [ | |
| "signed_distances = np.asarray(false_positive_distances) + -1 * np.asarray(false_negative_distances)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 16, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "application/vnd.jupyter.widget-view+json": { | |
| "model_id": "e49d88bfed3a43d6a609b203b298a5d2", | |
| "version_major": 2, | |
| "version_minor": 0 | |
| }, | |
| "text/plain": [ | |
| "Viewer(cmap=['rainbow'], geometries=[], gradient_opacity=0.22, opacity_gaussians=[[{'position': 0.769444444444…" | |
| ] | |
| }, | |
| "metadata": {}, | |
| "output_type": "display_data" | |
| } | |
| ], | |
| "source": [ | |
| "opacity_gaussians = [[{'position': 0.7694444444444445,\n", | |
| " 'height': 1,\n", | |
| " 'width': 0.22777777777777775,\n", | |
| " 'xBias': 0.5127777777777779,\n", | |
| " 'yBias': 0.5090909090909093},\n", | |
| " {'position': 0.21944444444444444,\n", | |
| " 'height': 1,\n", | |
| " 'width': 0.2277777777777778,\n", | |
| " 'xBias': -0.30277777777777776,\n", | |
| " 'yBias': 0.18181818181818166}]]\n", | |
| "\n", | |
| "viewer = view(signed_distances,\n", | |
| " cmap=cm.rainbow,\n", | |
| " opacity_gaussians=opacity_gaussians,\n", | |
| " ui_collapsed=True)\n", | |
| "viewer" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 13, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "data": { | |
| "text/plain": [ | |
| "[[{'position': 0.7694444444444445,\n", | |
| " 'height': 1,\n", | |
| " 'width': 0.22777777777777775,\n", | |
| " 'xBias': 0.5127777777777779,\n", | |
| " 'yBias': 0.5090909090909093},\n", | |
| " {'position': 0.21944444444444444,\n", | |
| " 'height': 1,\n", | |
| " 'width': 0.2277777777777778,\n", | |
| " 'xBias': -0.30277777777777776,\n", | |
| " 'yBias': 0.18181818181818166}]]" | |
| ] | |
| }, | |
| "execution_count": 13, | |
| "metadata": {}, | |
| "output_type": "execute_result" | |
| } | |
| ], | |
| "source": [ | |
| "viewer.opacity_gaussians" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 3", | |
| "language": "python", | |
| "name": "python3" | |
| }, | |
| "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.7.6" | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 2 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment