IAM

OPENSOURCEFAN STUDYING
STUDYINGCOMPUTERSCIENCEANDMATH COMPUTERSCIENCE

Check out the latest superpixel benchmark — Superpixel Benchmark (2016) — and let me know your opinion! @david_stutz
15thAPRIL2017

SNIPPET

Visualizing 3D occupancy volumes in Python using either Mayavi or mplot3d.

off_io.py
#!/usr/bin/env python
"""
Visualize 3d volumes.
"""

import mayavi.mlab
from matplotlib import pyplot as plt
import numpy as np

def plot_volume_mayavi(volume):
    """
    Plots a voxel volume using Mayavi.

    :param volume: 3D volume where pixels > 0.5 are identified as occupied
    :type volume: numpy.ndarray
    """

    xx, yy, zz = np.where(volume > 0.5)
    mayavi.mlab.points3d(xx, yy, zz, mode = 'cube', color = (0, 1, 0), scale_factor = 1)
    mayavi.mlab.show()

def plot_volume_scatter(volume):
    """
    Plot a volume as scatter plot.

    :param volume: 3D volume where pixels > 0.5 are identified as occupied
    :type volume: numpy.ndarray
    """

    fig = plt.figure()
    ax = fig.add_subplot(111, projection = '3d')
    xx, yy, zz = np.where(volume > 0.5)
    ax.scatter(xx, yy, zz, c = zz)
    plt.show()

if __name__ == '__main__':

    volume = np.zeros((32, 32, 32))
    volume[12:20, 12:20, 12:20] = 1

    plot_volume_mayavi(volume)
    plot_volume_scatter(volume)

What is your opinion on the code snippet? Is it working? Let me know your thoughts in the comments below or using the following platforms: