Search code examples
pythonnumpymatplotlibpython-imaging-libraryopen3d

How should I plot XYZ data points to create a depth image in RGB in python


I am working on a small project where I am given a 2D array of XYZ data. Something in this manner:

a = [[1,7,13,3,4],
     [6,21,32,11,2]]

where x represents the column of the array, y represents the row of the array, and z represents the content of the array, which is the distance. What I am trying to accomplish is to use the 2d array and plot a depth image in RGB. To elaborate, the closer the distance (z) value, I want the point to be plotted as red. And as the distance (z) values increase, I want to plot it yellow, green or blue depending on how large the distance is.

I added a sample picture for better clarification.
Depth image

I am trying to accomplish this in python. I tried researching into it, but most of the time I found how to extract XYZ data from a depth image or from a point cloud, instead of using XYZ data to plot a depth image.

Please let me know if this is possible or what python libraries are available to achieve this.

Thank you.

EDIT: I believe it might help noting that the 2D array consists of point cloud data generated by a rangefinder sensor. I want to be able to construct a still 2d image of the scanned area using the data points. I also want to make use of colormaps in order to allow me to visualize the depth of the image.


Solution

  • something along the lines of this?

    from matplotlib.pylab import plt
    a = [[1,7,13,3,4],
         [6,21,32,11,2]]
    plt.matshow(a, cmap=plt.cm.viridis)
    plt.colorbar()
    

    enter image description here

    You can pass large arrays such as images. Here I use an example image from matplotlib, I clip the colors to get a (120, 560) and not a (120, 560, 3) array and I display it:

    from matplotlib.pylab import plt
    from matplotlib.cbook import get_sample_data
    fn = get_sample_data("logo2.png", asfileobj=False)
    img = plt.imread(fn, format='png')[...,0] #get single color channel
    plt.matshow(img,cmap=plt.cm.jet,interpolation='bicubic')#see imshow for more arguments
    plt.colorbar()
    

    enter image description here

    If I am not mistaken plt.matshow is a subclass of plt.imshow