Han
Han

Reputation: 23

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.

Upvotes: 2

Views: 4025

Answers (2)

amzon-ex
amzon-ex

Reputation: 1744

Here's an example using seaborn:

Generating sample data:

pip install seaborn  # for those who have not yet installed seaborn
---
AND
---
import seaborn as sns

def func(x, y):
    return np.exp(-x**2-y**2)

xaxis = np.linspace(-1, 1, 100)
yaxis = np.linspace(-1, 1, 200)
result = func(xaxis[:,None], yaxis[None,:])

Plotting:

sns.heatmap(result, cmap=sns.color_palette("Spectral_r", as_cmap=True))
plt.yticks([],[])
plt.xticks([],[])

Result: enter image description here

Upvotes: 2

Mecgrad
Mecgrad

Reputation: 64

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

Upvotes: 1

Related Questions