简体   繁体   English

Python:将二维点云转换为灰度图像

[英]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).我有一个长度可变的数组,其中填充了分布在 (0,0) 附近的二维坐标点(来自点云),我想将它们转换为二维矩阵(=灰度图像)。

# 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我将如何使用 python 和 numpy 实现这一点

Looks like matplotlib.pyplot.hist2d is what you are looking for.看起来matplotlib.pyplot.hist2d就是你要找的。

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然后你可以在你的数据上调用hist2d ,例如这样

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 .请注意, hist2d的参数是两个一维数组,因此在将数据提供给hist2d之前,您必须对数据进行一些hist2d

Quick solution using only numpy without the need for matplotlib and therefor plots:仅使用 numpy 的快速解决方案,无需 matplotlib 及其绘图:

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])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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