3D Rendering Toolbox: Color image and depth map from mesh

A notable use case is to simulate RGB or Kinect (depth) camera images for computer vision applications.
96 Downloads
Updated 12 Mar 2024

3D Rendering Toolbox: Color images and depth maps from 3D meshes

View on GitHub View on File Exchange Open in MATLAB Online

Implementation of the computer graphics pipeline for triangulated meshes, in addition to a number of camera plotting functions. Handles both perspective and orthographic projection.

A notable use case is to simulate RGB or Kinect (depth) camera images for computer vision applications.

The codebase is compact, extensively documented, and uses only MATLAB built-in functions.

Functions and Classes

Name Description
world2image Project world points, edges, and faces into image space.
rasterize Rasterize projected mesh to form an image composed of pixels.
raycast Compute rays from the camera's optical center to its pixels.
clip Clip faces, edges, vertices in the clip space of the graphics pipeline. Used by world2image.
edgefcn Test whether 2D points are within triangular faces. Used by rasterize.
Camera Object for plotting a camera and storing its properties.
  • Properties
    • projectionMatrix : 4-by-4 projection matrix.
    • imageSize : Camera resolution, [width height].
    • t : Camera translation, [x y z].
    • R : 3-by-3 camera rotation matrix.
    • plotHandles : Graphics handles.
  • Methods
    • Constructor : Set and validate Camera properties.
    • plotcamera : Plot a mesh representing the camera.
    • plotframe : Plot the camera's Cartesian coordinate system.
    • plotfov : Plot a mesh representing the camera's field-of-view.
    • setview : Set the MATLAB axes' view to match the Camera object.
ProjectionMatrix Build and inspect a perspective or orthographic camera projection matrix.
  • Value : 4-by-4 projection matrix.
  • Methods
    • Constructor : Build the projection matrix, either with the camera's field-of-view and aspect ratio, by defining the frustum coordinates directly, or by converting from a camera intrinsic matrix.
    • decompose : Extract properties of the camera's view frustum.

For full documentation, please see the respective MATLAB file, or use the doc command, e.g., doc world2image.

Example

Please see example.m, which outputs the figure above.

Notes

  • The toolbox was designed to have only minimal internal dependencies. Therefore Camera, clip, edgefcn, etc. can largely be used independently of the rest of the toolbox. The toolbox's individual functions and classes could be useful for different rendering pipelines, for example, to rasterize 2D vector graphics, or beyond the computer graphics pipeline, for example, to simply plot the location of a camera within a scene.

  • As the renderer runs on the CPU and uses only MATLAB code, its speed is limited. On a standard laptop, for a scene with 8000 faces and at a resolution of 300x300, I get around 12 fps. Nevertheless, for applications which don't need a high speed, having everything within MATLAB is handy.

  • Does not currently implement interpolated face colors (from colored vertices) or lighting.

  • If the user has a camera intrinsic matrix, rather than a projection matrix, e.g., when simulating a camera calibrated in MATLAB, the ProjectionMatrix class can be used to convert this to the necessary format.

Compatibility

Created in 2022b. All files are compatible with MATLAB release 2022a and later, but see the documentation of individual files for their specific compatability. Compatible with all platforms. No external dependencies.

License and Citation

Published under MIT License (see LICENSE.txt).

Please cite George Abrahams (https://github.com/WD40andTape/MatlabRenderer, https://www.linkedin.com/in/georgeabrahams).

Cite As

George Abrahams (https://www.linkedin.com/in/georgeabrahams) (2024). 3D Rendering Toolbox: Color image and depth map from mesh, GitHub. https://github.com/WD40andTape/MatlabRenderer.

MATLAB Release Compatibility
Created with R2022b
Compatible with R2022a and later releases
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!
Version Published Release Notes
2.1.0

See release notes for this release on GitHub: https://github.com/WD40andTape/MatlabRenderer/releases/tag/2.1.0

1.2.0

See release notes for this release on GitHub: https://github.com/WD40andTape/MatlabRenderer/releases/tag/1.2.0

1.1.0

See release notes for this release on GitHub: https://github.com/WD40andTape/MatlabRenderer/releases/tag/1.1.0

1.0.0

To view or report issues in this GitHub add-on, visit the GitHub Repository.
To view or report issues in this GitHub add-on, visit the GitHub Repository.