使用Python和Matplotlib将立体图像和深度图绘制到3D散点图

时间:2015-05-06 14:50:04

标签: python opencv matplotlib

我有一个立体图像和所述图像的深度图。我想制作一个散点图来表示图片的三维图像。这是我尝试过的,但是我得到了一些错误,比如尺寸不合适等等。

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

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

1 个答案:

答案 0 :(得分:0)

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