Skip to content

Instantly share code, notes, and snippets.

@dave3d
Last active June 22, 2022 15:33
Show Gist options
  • Save dave3d/6e9a0f37929e63bdc982e7c69bed3f87 to your computer and use it in GitHub Desktop.
Save dave3d/6e9a0f37929e63bdc982e7c69bed3f87 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"id": "a694e51a",
"metadata": {},
"outputs": [],
"source": [
"# Use a virtual environment, since the notebook install these packages \n",
"!pip install itkwidgets simpleitk"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "c818de9e",
"metadata": {},
"outputs": [],
"source": [
"import os.path\n",
"import urllib.request\n",
"if not os.path.isfile(\"ct_example.nii.gz\"):\n",
" print(\"image not found. downloading now.\")\n",
" urllib.request.urlretrieve(\"https://raw.githubusercontent.com/dave3d/dicom2stl/main/examples/Data/ct_example.nii.gz\", \"ct_example.nii.gz\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4dbf5526",
"metadata": {},
"outputs": [],
"source": [
"import SimpleITK as sitk\n",
"\n",
"# Example volume: https://raw.githubusercontent.com/dave3d/dicom2stl/main/examples/Data/ct_example.nii.gz\"\n",
"img = sitk.ReadImage(\"ct_example.nii.gz\")\n",
"\n",
"cut = sitk.Image([300,300,1], sitk.sitkUInt16)\n",
"\n",
"rotate = sitk.Euler3DTransform()\n",
"\n",
"# 15 degree rotation about X axis\n",
"rotate.SetRotation(.2618, 0.0, 0.0)\n",
"\n",
"cut.SetDirection(rotate.GetMatrix())\n",
"\n",
"cut2 = sitk.Resample(img, cut)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3f3e8cfe",
"metadata": {},
"outputs": [],
"source": [
"#sitk.Show(cut2)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "b9f3d0a6",
"metadata": {
"scrolled": true
},
"outputs": [],
"source": [
"import itkwidgets\n",
"\n",
"itkwidgets.view(cut2, axes=True, slicing_planes=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "4502bfea",
"metadata": {},
"outputs": [],
"source": [
"sitk.WriteImage(cut2, \"cut.nrrd\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3 (ipykernel)",
"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.8.12"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment