Monday, 21 April 2025

3D Volume Rendering Pattern using Python

 


import numpy as np

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

data = np.random.rand(50, 50, 50)

fig = plt.figure(figsize=(10, 8))

ax = fig.add_subplot(111, projection='3d')

x, y, z = np.indices(data.shape)

threshold = 0.5

ax.scatter(x[data > threshold], y[data > threshold], z[data > threshold],

           c=data[data > threshold], cmap='inferno', marker='o')

ax.set_title("3D Volume Rendering")

ax.set_xlabel("X axis")

ax.set_ylabel("Y axis")

ax.set_zlabel("Z axis")

plt.show()

#source code --> clcoding.com 

Code Explanation:

1. Importing Libraries:

 import numpy as np

import matplotlib.pyplot as plt

from mpl_toolkits.mplot3d import Axes3D

numpy: A powerful library used for numerical computations in Python, particularly for creating and manipulating arrays.

 matplotlib.pyplot: A plotting library for creating static, animated, and interactive visualizations in Python.

 mpl_toolkits.mplot3d: A module within matplotlib that provides tools for creating 3D plots.

 2. Generating Random 3D Data:

 data = np.random.rand(50, 50, 50)

data is a 3D numpy array of shape (50, 50, 50), filled with random floating-point numbers between 0 and 1.

 This represents a 3D volume where each value corresponds to a voxel (volume element), and each voxel has a random intensity value.

 3. Creating a 3D Plot:

fig = plt.figure(figsize=(10, 8))

ax = fig.add_subplot(111, projection='3d')

A figure object is created with a size of 10 by 8 inches.

 The add_subplot(111, projection='3d') creates a 3D subplot (ax) for visualizing the data in 3D space.

 4. Generating Indices for the Volume:

x, y, z = np.indices(data.shape)

np.indices(data.shape) generates 3D arrays for the x, y, and z indices corresponding to the shape of the data array, i.e., (50, 50, 50). This creates 3D grids for each dimension (x, y, and z).

 So, x, y, and z are 3D arrays, where each element represents the index position in the respective dimension.

 5. Thresholding:

threshold = 0.5

This is the threshold value. Any voxel (point) in the volume with a value greater than 0.5 will be considered for rendering.

 6. Scatter Plot of Voxel Points:

ax.scatter(x[data > threshold], y[data > threshold], z[data > threshold], c=data[data > threshold], cmap='inferno', marker='o')

ax.scatter: This creates a scatter plot of the 3D data points.

 x[data > threshold]: Selects the x-coordinates of the voxels where the value in data is greater than the threshold (0.5).

 y[data > threshold]: Selects the y-coordinates of the voxels where the value in data is greater than the threshold.

 z[data > threshold]: Selects the z-coordinates of the voxels where the value in data is greater than the threshold.

 The condition data > threshold creates a boolean array where True corresponds to values greater than the threshold (0.5), and False corresponds to values below the threshold. Only the voxels where this condition is True are included in the scatter plot.

 c=data[data > threshold]: Specifies the colors of the points based on the values in the data array that are above the threshold. These values are mapped to colors using the specified colormap ('inferno').

 cmap='inferno': This applies a colormap to the scatter plot. The 'inferno' colormap is a perceptually uniform colormap that ranges from dark purple to yellow.

 marker='o': This sets the marker for the scatter plot points to be circular ('o').

 7. Setting Plot Labels and Title:

ax.set_title("3D Volume Rendering")

ax.set_xlabel("X axis")

ax.set_ylabel("Y axis")

ax.set_zlabel("Z axis")

set_title: Sets the title of the plot as "3D Volume Rendering".

 set_xlabel, set_ylabel, and set_zlabel: Set labels for the X, Y, and Z axes, respectively.

 8. Displaying the Plot:

plt.show()

This line displays the 3D plot to the screen.

 

 


0 Comments:

Post a Comment

Popular Posts

Categories

100 Python Programs for Beginner (118) AI (152) Android (25) AngularJS (1) Api (6) Assembly Language (2) aws (27) Azure (8) BI (10) Books (251) Bootcamp (1) C (78) C# (12) C++ (83) Course (84) Coursera (298) Cybersecurity (28) Data Analysis (24) Data Analytics (16) data management (15) Data Science (217) Data Strucures (13) Deep Learning (68) Django (16) Downloads (3) edx (21) Engineering (15) Euron (30) Events (7) Excel (17) Finance (9) flask (3) flutter (1) FPL (17) Generative AI (47) Git (6) Google (47) Hadoop (3) HTML Quiz (1) HTML&CSS (48) IBM (41) IoT (3) IS (25) Java (99) Leet Code (4) Machine Learning (186) Meta (24) MICHIGAN (5) microsoft (9) Nvidia (8) Pandas (11) PHP (20) Projects (32) Python (1218) Python Coding Challenge (884) Python Quiz (342) Python Tips (5) Questions (2) R (72) React (7) Scripting (3) security (4) Selenium Webdriver (4) Software (19) SQL (45) Udemy (17) UX Research (1) web application (11) Web development (7) web scraping (3)

Followers

Python Coding for Kids ( Free Demo for Everyone)