WeGi
WeGi

Reputation: 1946

Stereo-Image and Depthmap to 3D-Scatterplot with Python and Matplotlib

I have a stereo-image and a depthmap of said image. I would like to make a scatterplot representing a 3d-Image of the picture. This is what i tried, but I get several errors, like the dimensions not fitting, etc.

The Problem is: the Scatter plot wants quadratic inputs. So I use the same length and widht. When i plot the picture I only see a line of points instead of the picture. What am I doing wrong?

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

Here are the used Pictures as reference:
depthmap: https://i.sstatic.net/ZSLvZ.png
stereo-image: https://i.sstatic.net/X4zQY.jpg

Upvotes: 1

Views: 4263

Answers (1)

Molly
Molly

Reputation: 13610

The numpy function meshgrid might be what you're looking for. That will give you the x and y values for a grid the size of your image. If you plot every point in the image with scatter, you won't see your original image and it will be slow. Here's an example of plotting points from an image over an image:

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

Upvotes: 1

Related Questions