{
  "id": 460747,
  "title": "Visualizing the training datasets in 3D",
  "url": "/competitions/blood-vessel-segmentation/discussion/460747",
  "author_name": "",
  "post_date": "2023-12-11T00:02:27.203888300Z",
  "votes": 21,
  "comment_count": 5,
  "views": 0,
  "content": "<p>I find it helpful to gain different perspectives of the data we're analyzing. <a href=\"https://napari.org/\" target=\"_blank\">Napari</a> can be helpful for visualization. (<a href=\"https://www.kaggle.com/competitions/blood-vessel-segmentation/discussion/461920\" target=\"_blank\">Thanks, Bull!</a>) In addition, here's a Python script that converts a set of labels into a point cloud (point_cloud.ply) and 3D model (mesh.obj), so we can see the whole data set at once from different angles.</p>\n<p>This script takes about three minutes and  15 GB of RAM to process <code>kidney_1_dense</code> (Ubuntu 22.04, AMD Ryzen 5600 mobile, no dedicated GPU)</p>\n<pre><code> os\n numpy  np\n matplotlib.pyplot  plt\n skimage  io\n\n\ndirectory = \n\n\ntiff_files = ([f  f  os.listdir(directory)  f.endswith()])\n\n\n()\nimages = [io.imread(os.path.join(directory, file)).astype(np.uint8)  file  tiff_files]\n\n\nfirst_image_shape = images[].shape\nfirst_image_dtype = images[].dtype\n\nfirst_image_shape, first_image_dtype, tiff_files\n\n\n()\nbinary_images = [np.where(image &gt; , , ).astype(np.uint8)  image  images]  \nvoxel_grid = np.stack(binary_images, axis=)  \n\n\n images\n binary_images\n\n\n\n\n\n\n()\nx, y, z = voxel_grid.nonzero()\n\n\n\n\n\n\n\n\n\n\n\n\n skimage.measure  marching_cubes\n open3d  o3d\n\n\n()\nverts, faces, _, _ = marching_cubes(voxel_grid, level=)\n\n\n voxel_grid\n\n\npoint_cloud = o3d.geometry.PointCloud()\npoint_cloud.points = o3d.utility.Vector3dVector(np.column_stack((x, y, z)))\n\n\n()\nmesh = o3d.geometry.TriangleMesh()\nmesh.vertices = o3d.utility.Vector3dVector(verts)\nmesh.triangles = o3d.utility.Vector3iVector(faces)\n\n\npoint_cloud_file = \nmesh_file = \n\n\n()\no3d.io.write_point_cloud(point_cloud_file, point_cloud)\n()\no3d.io.write_triangle_mesh(mesh_file, mesh)\n\n()\npoint_cloud_file, mesh_file\n</code></pre>\n<p>Here is a render of the kidney_3_dense label folder.</p>\n<p><img src=\"https://storage.googleapis.com/kaggle-forum-message-attachments/2556712/20050/render.jpg\" alt=\"render\"></p>",
  "messages": [
    {
      "id": "2556712",
      "postDate": "12/11/2023 00:02:27",
      "content": "<p>I find it helpful to gain different perspectives of the data we're analyzing. <a href=\"https://napari.org/\" target=\"_blank\">Napari</a> can be helpful for visualization. (<a href=\"https://www.kaggle.com/competitions/blood-vessel-segmentation/discussion/461920\" target=\"_blank\">Thanks, Bull!</a>) In addition, here's a Python script that converts a set of labels into a point cloud (point_cloud.ply) and 3D model (mesh.obj), so we can see the whole data set at once from different angles.</p>\n<p>This script takes about three minutes and  15 GB of RAM to process <code>kidney_1_dense</code> (Ubuntu 22.04, AMD Ryzen 5600 mobile, no dedicated GPU)</p>\n<pre><code> os\n numpy  np\n matplotlib.pyplot  plt\n skimage  io\n\n\ndirectory = \n\n\ntiff_files = ([f  f  os.listdir(directory)  f.endswith()])\n\n\n()\nimages = [io.imread(os.path.join(directory, file)).astype(np.uint8)  file  tiff_files]\n\n\nfirst_image_shape = images[].shape\nfirst_image_dtype = images[].dtype\n\nfirst_image_shape, first_image_dtype, tiff_files\n\n\n()\nbinary_images = [np.where(image &gt; , , ).astype(np.uint8)  image  images]  \nvoxel_grid = np.stack(binary_images, axis=)  \n\n\n images\n binary_images\n\n\n\n\n\n\n()\nx, y, z = voxel_grid.nonzero()\n\n\n\n\n\n\n\n\n\n\n\n\n skimage.measure  marching_cubes\n open3d  o3d\n\n\n()\nverts, faces, _, _ = marching_cubes(voxel_grid, level=)\n\n\n voxel_grid\n\n\npoint_cloud = o3d.geometry.PointCloud()\npoint_cloud.points = o3d.utility.Vector3dVector(np.column_stack((x, y, z)))\n\n\n()\nmesh = o3d.geometry.TriangleMesh()\nmesh.vertices = o3d.utility.Vector3dVector(verts)\nmesh.triangles = o3d.utility.Vector3iVector(faces)\n\n\npoint_cloud_file = \nmesh_file = \n\n\n()\no3d.io.write_point_cloud(point_cloud_file, point_cloud)\n()\no3d.io.write_triangle_mesh(mesh_file, mesh)\n\n()\npoint_cloud_file, mesh_file\n</code></pre>\n<p>Here is a render of the kidney_3_dense label folder.</p>\n<p><img src=\"https://storage.googleapis.com/kaggle-forum-message-attachments/2556712/20050/render.jpg\" alt=\"render\"></p>",
      "rawMarkdown": "I find it helpful to gain different perspectives of the data we're analyzing. [Napari](https://napari.org/) can be helpful for visualization. ([Thanks, Bull!](https://www.kaggle.com/competitions/blood-vessel-segmentation/discussion/461920)) In addition, here's a Python script that converts a set of labels into a point cloud (point_cloud.ply) and 3D model (mesh.obj), so we can see the whole data set at once from different angles.\n\nThis script takes about three minutes and ~~50+~~ 15 GB of RAM to process `kidney_1_dense` (Ubuntu 22.04, AMD Ryzen 5600 mobile, no dedicated GPU)\n\n```py\nimport os\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom skimage import io\n\n# Directory containing the TIFF files\ndirectory = 'train/kidney_1_dense/labels'\n\n# List all TIFF files in the directory\ntiff_files = sorted([f for f in os.listdir(directory) if f.endswith('.tif')])\n\n# Load the images and convert them to numpy arrays\nprint(\"Loading images...\")\nimages = [io.imread(os.path.join(directory, file)).astype(np.uint8) for file in tiff_files]\n\n# Inspecting the first image dimensions and data type\nfirst_image_shape = images[0].shape\nfirst_image_dtype = images[0].dtype\n\nfirst_image_shape, first_image_dtype, tiff_files\n\n# Convert each image to a binary array and stack them to form a 3D array\nprint(\"Converting to binary...\")\nbinary_images = [np.where(image > 0, 1, 0).astype(np.uint8) for image in images]  # Convert to binary\nvoxel_grid = np.stack(binary_images, axis=0)  # Stack along a new axis\n\n# Delete images to save space\ndel images\ndel binary_images\n\n# # Visualizing the 3D structure\n# fig = plt.figure(figsize=(10, 10))\n# ax = fig.add_subplot(111, projection='3d')\n\n# # Plotting each voxel\nprint(\"Plotting...\")\nx, y, z = voxel_grid.nonzero()\n# ax.scatter(z, x, y, zdir='z', c='blue', marker='.', alpha=0.5)\n\n# # Setting labels and title\n# ax.set_xlabel('Z Axis')\n# ax.set_ylabel('X Axis')\n# ax.set_zlabel('Y Axis')\n# ax.set_title('3D Voxel Visualization')\n\n# # Show plot\n# plt.show()\n\n\nfrom skimage.measure import marching_cubes\nimport open3d as o3d\n\n# Generate a mesh using marching cubes algorithm\nprint(\"Applying marching cubes on the voxel grid...\")\nverts, faces, _, _ = marching_cubes(voxel_grid, level=0)\n\n# Delete the voxel grid to save space\ndel voxel_grid\n\n# Creating Open3D point cloud from non-zero voxel points\npoint_cloud = o3d.geometry.PointCloud()\npoint_cloud.points = o3d.utility.Vector3dVector(np.column_stack((x, y, z)))\n\n# Creating Open3D mesh from the vertices and faces\nprint(\"Creating mesh...\")\nmesh = o3d.geometry.TriangleMesh()\nmesh.vertices = o3d.utility.Vector3dVector(verts)\nmesh.triangles = o3d.utility.Vector3iVector(faces)\n\n# File paths for export\npoint_cloud_file = 'point_cloud.ply'\nmesh_file = 'mesh.obj'\n\n# Export point cloud and mesh\nprint(f\"Exporting point cloud to {point_cloud_file}...\")\no3d.io.write_point_cloud(point_cloud_file, point_cloud)\nprint(f\"Exporting mesh to {mesh_file}...\")\no3d.io.write_triangle_mesh(mesh_file, mesh)\n\nprint(\"Done!\")\npoint_cloud_file, mesh_file\n\n```\n\nHere is a render of the kidney_3_dense label folder.\n\n![render](https://storage.googleapis.com/kaggle-forum-message-attachments/2556712/20050/render.jpg)",
      "votes": null
    },
    {
      "id": "2556774",
      "postDate": "12/11/2023 01:24:55",
      "content": "<p>good work.<br>\ni am interested to know how to setup the render.<br>\nany scripts for that?</p>",
      "rawMarkdown": "good work.\ni am interested to know how to setup the render.\nany scripts for that?",
      "votes": null
    },
    {
      "id": "2557831",
      "postDate": "12/11/2023 17:38:54",
      "content": "<p>I wish there were. All the rendering work was manual</p>",
      "rawMarkdown": "I wish there were. All the rendering work was manual",
      "votes": null
    },
    {
      "id": "2557852",
      "postDate": "12/11/2023 17:44:40",
      "content": "<p>Cinematic rendering from simens healthineers …</p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F113660%2Fca2866ee76b7ebd8ff69060ec8e7c6dc%2FSelection_999(4366).png?generation=1702316644464135&amp;alt=media\" alt=\"\"></p>",
      "rawMarkdown": "Cinematic rendering from simens healthineers ...\n\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F113660%2Fca2866ee76b7ebd8ff69060ec8e7c6dc%2FSelection_999(4366).png?generation=1702316644464135&alt=media)",
      "votes": null
    },
    {
      "id": "2560871",
      "postDate": "12/14/2023 04:07:05",
      "content": "<p>Here's a template Blender project you can use. It comes with a matte studio-like background with two-point lighting, some simple shaders, and a little compositing pipeline. I created the file, and I'm releasing it into public domain.</p>\n<p>Setting up a render can be as easy as opening the file in Blender 4.0+ and swapping Suzanne the monkey with an asset of your choice.</p>\n<p>Enjoy!</p>",
      "rawMarkdown": "Here's a template Blender project you can use. It comes with a matte studio-like background with two-point lighting, some simple shaders, and a little compositing pipeline. I created the file, and I'm releasing it into public domain.\n\nSetting up a render can be as easy as opening the file in Blender 4.0+ and swapping Suzanne the monkey with an asset of your choice.\n\nEnjoy!",
      "votes": null
    },
    {
      "id": "2560897",
      "postDate": "12/14/2023 04:50:57",
      "content": "<p>thanks a lot!</p>",
      "rawMarkdown": "thanks a lot!",
      "votes": null
    }
  ],
  "comments": [
    {
      "id": 2556774,
      "author_name": "hengck23",
      "author_url": "",
      "post_date": "12/11/2023 01:24:55",
      "content": "<p>good work.<br>\ni am interested to know how to setup the render.<br>\nany scripts for that?</p>",
      "votes": null,
      "replies": [
        {
          "id": 2557831,
          "author_name": "cyberian516",
          "author_url": "",
          "post_date": "12/11/2023 17:38:54",
          "content": "<p>I wish there were. All the rendering work was manual</p>",
          "votes": null,
          "replies": [
            {
              "id": 2557852,
              "author_name": "hengck23",
              "author_url": "",
              "post_date": "12/11/2023 17:44:40",
              "content": "<p>Cinematic rendering from simens healthineers …</p>\n<p><img src=\"https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F113660%2Fca2866ee76b7ebd8ff69060ec8e7c6dc%2FSelection_999(4366).png?generation=1702316644464135&amp;alt=media\" alt=\"\"></p>",
              "votes": null,
              "replies": []
            }
          ]
        },
        {
          "id": 2560871,
          "author_name": "cyberian516",
          "author_url": "",
          "post_date": "12/14/2023 04:07:05",
          "content": "<p>Here's a template Blender project you can use. It comes with a matte studio-like background with two-point lighting, some simple shaders, and a little compositing pipeline. I created the file, and I'm releasing it into public domain.</p>\n<p>Setting up a render can be as easy as opening the file in Blender 4.0+ and swapping Suzanne the monkey with an asset of your choice.</p>\n<p>Enjoy!</p>",
          "votes": null,
          "replies": [
            {
              "id": 2560897,
              "author_name": "hengck23",
              "author_url": "",
              "post_date": "12/14/2023 04:50:57",
              "content": "<p>thanks a lot!</p>",
              "votes": null,
              "replies": []
            }
          ]
        }
      ]
    }
  ],
  "raw_markdown_by_id": {
    "2556712": "I find it helpful to gain different perspectives of the data we're analyzing. [Napari](https://napari.org/) can be helpful for visualization. ([Thanks, Bull!](https://www.kaggle.com/competitions/blood-vessel-segmentation/discussion/461920)) In addition, here's a Python script that converts a set of labels into a point cloud (point_cloud.ply) and 3D model (mesh.obj), so we can see the whole data set at once from different angles.\n\nThis script takes about three minutes and ~~50+~~ 15 GB of RAM to process `kidney_1_dense` (Ubuntu 22.04, AMD Ryzen 5600 mobile, no dedicated GPU)\n\n```py\nimport os\nimport numpy as np\nimport matplotlib.pyplot as plt\nfrom skimage import io\n\n# Directory containing the TIFF files\ndirectory = 'train/kidney_1_dense/labels'\n\n# List all TIFF files in the directory\ntiff_files = sorted([f for f in os.listdir(directory) if f.endswith('.tif')])\n\n# Load the images and convert them to numpy arrays\nprint(\"Loading images...\")\nimages = [io.imread(os.path.join(directory, file)).astype(np.uint8) for file in tiff_files]\n\n# Inspecting the first image dimensions and data type\nfirst_image_shape = images[0].shape\nfirst_image_dtype = images[0].dtype\n\nfirst_image_shape, first_image_dtype, tiff_files\n\n# Convert each image to a binary array and stack them to form a 3D array\nprint(\"Converting to binary...\")\nbinary_images = [np.where(image > 0, 1, 0).astype(np.uint8) for image in images]  # Convert to binary\nvoxel_grid = np.stack(binary_images, axis=0)  # Stack along a new axis\n\n# Delete images to save space\ndel images\ndel binary_images\n\n# # Visualizing the 3D structure\n# fig = plt.figure(figsize=(10, 10))\n# ax = fig.add_subplot(111, projection='3d')\n\n# # Plotting each voxel\nprint(\"Plotting...\")\nx, y, z = voxel_grid.nonzero()\n# ax.scatter(z, x, y, zdir='z', c='blue', marker='.', alpha=0.5)\n\n# # Setting labels and title\n# ax.set_xlabel('Z Axis')\n# ax.set_ylabel('X Axis')\n# ax.set_zlabel('Y Axis')\n# ax.set_title('3D Voxel Visualization')\n\n# # Show plot\n# plt.show()\n\n\nfrom skimage.measure import marching_cubes\nimport open3d as o3d\n\n# Generate a mesh using marching cubes algorithm\nprint(\"Applying marching cubes on the voxel grid...\")\nverts, faces, _, _ = marching_cubes(voxel_grid, level=0)\n\n# Delete the voxel grid to save space\ndel voxel_grid\n\n# Creating Open3D point cloud from non-zero voxel points\npoint_cloud = o3d.geometry.PointCloud()\npoint_cloud.points = o3d.utility.Vector3dVector(np.column_stack((x, y, z)))\n\n# Creating Open3D mesh from the vertices and faces\nprint(\"Creating mesh...\")\nmesh = o3d.geometry.TriangleMesh()\nmesh.vertices = o3d.utility.Vector3dVector(verts)\nmesh.triangles = o3d.utility.Vector3iVector(faces)\n\n# File paths for export\npoint_cloud_file = 'point_cloud.ply'\nmesh_file = 'mesh.obj'\n\n# Export point cloud and mesh\nprint(f\"Exporting point cloud to {point_cloud_file}...\")\no3d.io.write_point_cloud(point_cloud_file, point_cloud)\nprint(f\"Exporting mesh to {mesh_file}...\")\no3d.io.write_triangle_mesh(mesh_file, mesh)\n\nprint(\"Done!\")\npoint_cloud_file, mesh_file\n\n```\n\nHere is a render of the kidney_3_dense label folder.\n\n![render](https://storage.googleapis.com/kaggle-forum-message-attachments/2556712/20050/render.jpg)",
    "2556774": "good work.\ni am interested to know how to setup the render.\nany scripts for that?",
    "2557831": "I wish there were. All the rendering work was manual",
    "2557852": "Cinematic rendering from simens healthineers ...\n\n\n![](https://www.googleapis.com/download/storage/v1/b/kaggle-forum-message-attachments/o/inbox%2F113660%2Fca2866ee76b7ebd8ff69060ec8e7c6dc%2FSelection_999(4366).png?generation=1702316644464135&alt=media)",
    "2560871": "Here's a template Blender project you can use. It comes with a matte studio-like background with two-point lighting, some simple shaders, and a little compositing pipeline. I created the file, and I'm releasing it into public domain.\n\nSetting up a render can be as easy as opening the file in Blender 4.0+ and swapping Suzanne the monkey with an asset of your choice.\n\nEnjoy!",
    "2560897": "thanks a lot!"
  },
  "source": "meta"
}