A simple Python project that converts a 2D grayscale image into a 3D voxel representation using OpenCV, NumPy, and Matplotlib.
The project demonstrates how pixel intensity values can be transformed into height values to generate a 3D voxel grid and visualize it using Matplotlib.
This project follows a simple image-to-3D conversion pipeline:
- Load an image in grayscale using OpenCV.
- Resize the image to 50 × 50 pixels.
- Normalize pixel intensity values between 0 and 1.
- Convert normalized pixel values into height values.
- Generate a 3D voxel grid using NumPy.
- Visualize the voxel structure using Matplotlib.
- Python – Programming language
- OpenCV – Image loading and preprocessing
- NumPy – Numerical operations and voxel grid creation
- Matplotlib – 3D visualization
Install the required libraries:
pip install opencv-python numpy matplotlibLoad the input image in grayscale mode using OpenCV.
Resize the image to 50 × 50 pixels to simplify visualization and reduce computational requirements.
Normalize pixel intensity values by dividing them by 255.
Convert normalized pixel intensities into integer height values ranging from 0 to 10.
Create a 3D Boolean array with dimensions 50 × 50 × 10.
Each pixel determines the height of its corresponding voxel column.
Use Matplotlib's ax.voxels() function to visualize the generated 3D voxel structure.
- Clone the repository.
- Install the required dependencies.
- Place your input image in the project directory.
- Update the image path in the Python script.
- Run the Python file.
python image_to_voxel.pyThe program generates a 3D voxel visualization based on the grayscale intensity of the input image.
- Brighter pixels produce taller voxel columns.
- Darker pixels produce shorter voxel columns.
- Black pixels produce no voxels.
- Image processing with OpenCV
- NumPy array manipulation
- Grayscale image normalization
- Height map generation
- 3D voxel representation
- 3D visualization using Matplotlib
- Basic 3D image representation
- Height map visualization
- Image-based 3D modeling concepts
- Computer Vision learning
- Understanding voxel-based representations
Md. Arafat Hossain
Research Assistant | Deep Learning | Computer Vision | Medical Image Analysis
GitHub: @Arafat-shuvo
⭐ If you find this project useful, consider giving it a star!