简体   繁体   中英

Python: Convert 2d point cloud to grayscale image

I have an array of variable length filled with 2d coordinate points (coming from a point cloud) which are distributed around (0,0) and i want to convert them into a 2d matrix (=grayscale image).

# have
array = [(1.0,1.1),(0.0,0.0),...]
# want
matrix = [[0,100,...],[255,255,...],...]

how would i achieve this using python and numpy

Looks like matplotlib.pyplot.hist2d is what you are looking for.

It basically bins your data into 2-dimensional bins (with a size of your choice). here the documentation and a working example is given below.

import numpy as np
import matplotlib.pyplot as plt
data = [np.random.randn(1000), np.random.randn(1000)]
plt.scatter(data[0], data[1])

在此处输入图片说明

Then you can call hist2d on your data, for instance like this

plt.hist2d(data[0], data[1], bins=20)

在此处输入图片说明

note that the arguments of hist2d are two 1-dimensional arrays, so you will have to do a bit of reshaping of our data prior to feed it to hist2d .

Quick solution using only numpy without the need for matplotlib and therefor plots:

import numpy as np
# given a 2dArray "array" and a desired image shape "[x,y]"
matrix = np.histogram2d(array[:,0], array[:,1], bins=[x,y])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM