python - 使用 Python 和 Matplotlib 将立体图像和深度图转换为 3D 散点图

标签 python opencv matplotlib

我有立体图像和所述图像的深度图。我想制作一个代表图片的 3d 图像的散点图。这是我尝试过的,但我遇到了几个错误,比如尺寸不合适等。

问题是:散点图需要二次输入。所以我使用相同的长度和宽度。当我绘制图片时,我只看到一行点而不是图片。我究竟做错了什么?

import matplotlib as mpl
import numpy as np
import matplotlib.pyplot as plt
import cv2

from mpl_toolkits.mplot3d import Axes3D


mpl.rcParams['legend.fontsize'] = 10
fig = plt.figure()
ax = fig.gca(projection='3d')
img = cv2.imread('helmet.jpg', 1)
dmap = cv2.imread('dmap_real.png', 1)

xarr = np.arange(3632)
yarr = np.arange(3632)
c = img[xarr,yarr,:] / 256
z = dmap[xarr, yarr, 0]

ax.scatter(xarr, xarr, z, c=c, label='point cloud')
ax.legend()
plt.show()

以下是使用过的图片作为引用:
深度图:http://i.imgur.com/1OzNBIn.png
立体图像:http://i.imgur.com/LMiek3H.jpg

最佳答案

numpy 函数 meshgrid可能是你正在寻找的。这将为您提供图像大小的网格的 x 和 y 值。如果您使用散点图绘制图像中的每个点,您将看不到原始图像并且速度会很慢。这是一个从图像上绘制点的示例:

import numpy as np
import matplotlib.pyplot as plt
import matplotlib.cbook as cbook

# Example image
image_file = cbook.get_sample_data('grace_hopper.png')
image = plt.imread(image_file)

(r, c, b) = np.shape(image)

# X and Y coordinates of points in the image, spaced by 10.
(X, Y) = np.meshgrid(range(0, c, 10), range(0, r, 10))

# Display the image
plt.imshow(image)
# Plot points from the image.
plt.scatter(X, Y, image[Y,X])
plt.show()

关于python - 使用 Python 和 Matplotlib 将立体图像和深度图转换为 3D 散点图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30080227/

相关文章:

python - 网页抓取返回空白结果,python 代码

python - 使用自定义签名 defs 保存 TF2 keras 模型

python - 需要框架来处理 Redshift 和 python 之间的交互

python - 模块 'cv2.ml' 没有属性 'dtree_create'

c++ - cv::warpAffine - 负图像坐标

python - xpath 按 <br> 标签分割字符串

image - 如何比较两个边缘图像(在 OpenCV 中)?

python - 由于 Coacoa 错误,在 Mac OS X Mavericks 上通过 pip 安装 Matplotlib 失败?

python - 如何更改 Matplotlib 中刻度标签的文本方向?

python - 在 Matplotlib 3 中设置具有共享轴的物理方形子图