{
"cells": [
{
"cell_type": "markdown",
"id": "da6d5a51-f8ac-4865-893e-10186189faa1",
"metadata": {},
"source": [
"# From surface data to image data\n",
"In this notebook we will turn surfaces into binary and label images."
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "2c16dfdc-32ff-43e9-a9df-56f0deecaf10",
"metadata": {},
"outputs": [],
"source": [
"import napari_process_points_and_surfaces as nppas\n",
"import vedo"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "b9048ddd-5930-46e4-bbf4-61f3ca985f41",
"metadata": {
"pycharm": {
"is_executing": true
}
},
"outputs": [
{
"data": {
"text/html": [
"
\n",
"\n",
"\n",
" \n",
" | \n",
"\n",
"nppas.SurfaceTuple \n",
"\n",
"origin (z/y/x) | [0. 0. 0.] | \n",
"center of mass(z/y/x) | 50.000,46.575,42.589 | \n",
"scale(z/y/x) | 1.000,1.000,1.000 | \n",
"bounds (z/y/x) | 25.500...74.500 2.500...88.500 2.500...83.500 | \n",
"average size | 31.277 | \n",
"number of vertices | 19040 | \n",
"number of faces | 38076 | \n",
"\n",
" \n",
"\n",
" | \n",
"
\n",
"
"
],
"text/plain": [
"(array([[25.5, 44. , 47. ],\n",
" [26. , 43.5, 47. ],\n",
" [26. , 44. , 46.5],\n",
" ...,\n",
" [74.5, 56. , 51. ],\n",
" [74.5, 56. , 52. ],\n",
" [74.5, 56. , 53. ]], dtype=float32),\n",
" array([[ 2, 1, 0],\n",
" [ 4, 3, 0],\n",
" [ 4, 0, 1],\n",
" ...,\n",
" [19038, 18870, 18872],\n",
" [19038, 18872, 19039],\n",
" [19039, 18872, 18852]], dtype=int64))"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"mesh = vedo.load(\"../../data/branchoid.ply\")\n",
"surface = nppas.to_napari_surface_data(mesh)\n",
"\n",
"surface"
]
},
{
"cell_type": "markdown",
"id": "ff91dc0c-d93f-4873-9e8f-3387fb0e27b1",
"metadata": {
"pycharm": {
"is_executing": true
}
},
"source": [
"## Binary volumes\n",
"A common task is to fill a volume to create a binary image stack."
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "bbc09181-568f-49de-b3e4-17f28e25b5b8",
"metadata": {
"pycharm": {
"is_executing": true
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"\n",
" \n",
" | \n",
"\n",
"\n",
"\n",
"shape | (49, 86, 81) | \n",
"dtype | int32 | \n",
"size | 1.3 MB | \n",
"min | 0 | max | 1 | \n",
" \n",
"\n",
" | \n",
"
\n",
"
"
],
"text/plain": [
"StackViewNDArray([[[0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" ...,\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0]],\n",
"\n",
" [[0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" ...,\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0]],\n",
"\n",
" [[0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" ...,\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0]],\n",
"\n",
" ...,\n",
"\n",
" [[0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" ...,\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0]],\n",
"\n",
" [[0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" ...,\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0]],\n",
"\n",
" [[0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" ...,\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0]]])"
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"binary_image = nppas.surface_to_binary_volume(surface)\n",
"binary_image"
]
},
{
"cell_type": "markdown",
"id": "588d25bb",
"metadata": {},
"source": [
"## Labeled surface voxels\n",
"We can also sample points from the surface and turn these points into a label image."
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "79a81bc5-56f0-4a51-a06f-0f93294b1b1b",
"metadata": {
"pycharm": {
"is_executing": true
}
},
"outputs": [
{
"data": {
"text/plain": [
"array([[26. , 44. , 46.5],\n",
" [26. , 43.5, 48. ],\n",
" [26. , 43.5, 50. ],\n",
" ...,\n",
" [74.5, 55. , 52. ],\n",
" [74.5, 56. , 49. ],\n",
" [74.5, 56. , 51. ]], dtype=float32)"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"point_cloud = nppas.sample_points_from_surface(surface, distance_fraction=0.01)\n",
"point_cloud"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "23c3fcd4-45bb-4d9e-a7df-2134942a4100",
"metadata": {
"pycharm": {
"is_executing": true
}
},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"\n",
" \n",
" | \n",
"\n",
"\n",
"\n",
"shape | (76, 90, 85) | \n",
"dtype | int32 | \n",
"size | 2.2 MB | \n",
"min | 0 | max | 5167 | \n",
" \n",
"\n",
" | \n",
"
\n",
"
"
],
"text/plain": [
"StackViewNDArray([[[0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" ...,\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0]],\n",
"\n",
" [[0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" ...,\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0]],\n",
"\n",
" [[0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" ...,\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0]],\n",
"\n",
" ...,\n",
"\n",
" [[0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" ...,\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0]],\n",
"\n",
" [[0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" ...,\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0]],\n",
"\n",
" [[0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" ...,\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0]]])"
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"point_labels = nppas.points_to_labels(point_cloud)\n",
"point_labels"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "427aa2f2-0140-4ca9-82c8-23fc314b3c52",
"metadata": {},
"outputs": [
{
"data": {
"text/html": [
"\n",
"\n",
"\n",
" \n",
" | \n",
"\n",
"\n",
"\n",
"shape | (90, 85) | \n",
"dtype | int32 | \n",
"size | 29.9 kB | \n",
"min | 0 | max | 2678 | \n",
" \n",
"\n",
" | \n",
"
\n",
"
"
],
"text/plain": [
"StackViewNDArray([[0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" ...,\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0],\n",
" [0, 0, 0, ..., 0, 0, 0]])"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"point_labels[50]"
]
},
{
"cell_type": "markdown",
"id": "7e11ee27-22fa-4f17-b751-a017d3d2c1e7",
"metadata": {},
"source": [
"These labels can also be turned into points again."
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "6a4939de-52e7-4e68-a78c-80490d444dc4",
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[(26.0, 44.0, 47.0),\n",
" (26.0, 44.0, 48.0),\n",
" (26.0, 44.0, 50.0),\n",
" (26.0, 44.0, 52.0),\n",
" (26.0, 44.0, 54.0),\n",
" (26.0, 45.0, 46.0),\n",
" (26.0, 45.0, 48.0),\n",
" (26.0, 45.0, 50.0),\n",
" (26.0, 45.0, 52.0),\n",
" (26.0, 45.0, 55.0)]"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"centroids = nppas.labels_to_centroids(point_labels)\n",
"centroids[:10]"
]
}
],
"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.9.15"
},
"vscode": {
"interpreter": {
"hash": "bffd3f721d7127ef2391d1b3b329910e3ca6998c35d437db3070140f7f84cfb6"
}
}
},
"nbformat": 4,
"nbformat_minor": 5
}