Skip to content
Snippets Groups Projects
structure_tensor.ipynb 5.02 KiB
Newer Older
  • Learn to ignore specific revisions
  • s193396's avatar
    s193396 committed
    {
     "cells": [
      {
       "cell_type": "code",
    
    s193396's avatar
    s193396 committed
       "execution_count": 1,
    
    s193396's avatar
    s193396 committed
       "metadata": {},
    
    s193396's avatar
    s193396 committed
       "outputs": [],
    
    s193396's avatar
    s193396 committed
       "source": [
        "import qim3d"
       ]
      },
      {
       "cell_type": "markdown",
       "metadata": {},
       "source": [
        "### Structure tensor notebook"
       ]
      },
      {
       "cell_type": "markdown",
       "metadata": {},
       "source": [
        "This notebook shows how to compute eigenvalues and eigenvectors of the **structure tensor** of a 3D volume using the `qim3d` library. The structure tensor (matrix) represents information about the local gradient directions in the volume, such that the eigenvectors represent the orientation of the structure in the volume, and the corresponding eigenvaleus indicate the magnitude.\n",
        "\n",
        "The function `qim3d.processing.structure_tensor` returns two arrays `val` and `vec` for the eigenvalues and eigenvectors, respectively.\\\n",
    
    s193396's avatar
    s193396 committed
        "By having the argument `visulize = True`, the function displays a figure of three subplots:\n",
    
    s193396's avatar
    s193396 committed
        "\n",
        "* Slice of volume with vector field of the eigenvectors\n",
    
    s193396's avatar
    s193396 committed
        "* Orientation histogram of the eigenvectors\n",
        "* Slice of volume with overlaying colors of the orientation\n",
        "\n",
        "For all three subplots, the colors used to visualize the orientation within the volume are from the HSV colorspace. In these visualizations, the saturation of the color corresponds to the vector component of the slicing direction (i.e. $z$-component when choosing visualization along `axis = 0`). Hence, if an orientation in the volume is orthogonal to the slicing direction, the corresponding color of the visualization will be gray."
    
    s193396's avatar
    s193396 committed
       ]
      },
      {
       "cell_type": "markdown",
       "metadata": {},
       "source": [
    
    s193396's avatar
    s193396 committed
        "### **Example:** Structure tensor of brain tissue volume"
    
    s193396's avatar
    s193396 committed
       ]
      },
      {
       "cell_type": "code",
    
    s193396's avatar
    s193396 committed
       "execution_count": 3,
    
    s193396's avatar
    s193396 committed
       "metadata": {},
       "outputs": [
        {
         "data": {
          "application/vnd.jupyter.widget-view+json": {
    
    s193396's avatar
    s193396 committed
           "model_id": "f3908100382a45db87800809c86c71ee",
    
    s193396's avatar
    s193396 committed
           "version_major": 2,
           "version_minor": 0
          },
          "text/plain": [
    
    s193396's avatar
    s193396 committed
           "Output()"
    
    s193396's avatar
    s193396 committed
          ]
         },
         "metadata": {},
         "output_type": "display_data"
        }
       ],
       "source": [
    
    s193396's avatar
    s193396 committed
        "# Import 3D volume of brain tissue\n",
        "NT = qim3d.examples.NT_128x128x128\n",
    
    s193396's avatar
    s193396 committed
        "\n",
    
    s193396's avatar
    s193396 committed
        "# Visuaize the 3D volume\n",
    
    s193396's avatar
    s193396 committed
        "qim3d.viz.volumetric(NT)"
    
    s193396's avatar
    s193396 committed
       ]
      },
      {
       "cell_type": "markdown",
       "metadata": {},
       "source": [
        "From the visualization of the full volume, it can be seen that the circular structures of the brain tissue are aligned orthogonal to the $z$-axis (`axis = 0`). By choosing to slice the volume in this direction, the structure tensor visualizations will largely be gray, since the $z$-component of the eigenvectors are close to $0$, meaning the saturation of the coloring will be close to $0$ (i.e. gray). This can be seen below. "
       ]
      },
      {
       "cell_type": "code",
    
    s193396's avatar
    s193396 committed
       "execution_count": 4,
    
    s193396's avatar
    s193396 committed
       "metadata": {},
       "outputs": [
        {
         "data": {
          "application/vnd.jupyter.widget-view+json": {
    
    s193396's avatar
    s193396 committed
           "model_id": "3f74d2b73666429dba6c489a10d613f7",
    
    s193396's avatar
    s193396 committed
           "version_major": 2,
           "version_minor": 0
          },
          "text/plain": [
           "VBox(children=(HBox(children=(IntSlider(value=64, description='Slice index', layout=Layout(width='450px'), max…"
          ]
         },
         "metadata": {},
         "output_type": "display_data"
        }
       ],
       "source": [
    
    s193396's avatar
    s193396 committed
        "# Compute eigenvalues and eigenvectors of the structure tensor\n",
    
    s193396's avatar
    s193396 committed
        "val, vec = qim3d.processing.structure_tensor(NT, visualize = True, axis = 0) # Slicing in z-direction"
    
    s193396's avatar
    s193396 committed
       ]
      },
      {
       "cell_type": "markdown",
       "metadata": {},
       "source": [
    
    s193396's avatar
    s193396 committed
        "By slicing the volume in the $x$-direction (`axis = 2`) instead, the orientation along the length of the structures in the brain tissue can be seen instead. Then the structure tensor visualizations will be largely blue, corresponding to eigenvectors along the $x$-direction with angles of $\\approx \\frac{\\pi}{2}$ radians ($90$ degrees)."
    
    s193396's avatar
    s193396 committed
       ]
      },
      {
       "cell_type": "code",
    
    s193396's avatar
    s193396 committed
       "execution_count": 5,
    
    s193396's avatar
    s193396 committed
       "metadata": {},
       "outputs": [
        {
         "data": {
          "application/vnd.jupyter.widget-view+json": {
    
    s193396's avatar
    s193396 committed
           "model_id": "9f578d703c5f4812a5a70c346fae513f",
    
    s193396's avatar
    s193396 committed
           "version_major": 2,
           "version_minor": 0
          },
          "text/plain": [
           "VBox(children=(HBox(children=(IntSlider(value=64, description='Slice index', layout=Layout(width='450px'), max…"
          ]
         },
         "metadata": {},
         "output_type": "display_data"
        }
       ],
       "source": [
        "# Compute eigenvalues and eigenvectors of the structure tensor\n",
    
    s193396's avatar
    s193396 committed
        "val, vec = qim3d.processing.structure_tensor(NT, visualize = True, axis = 2) # Slicing in x-direction"
    
    s193396's avatar
    s193396 committed
       ]
      }
     ],
     "metadata": {
      "kernelspec": {
       "display_name": "qim3d",
       "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.10.14"
      }
     },
     "nbformat": 4,
     "nbformat_minor": 2
    }