Skip to content

Instantly share code, notes, and snippets.

@omaraflak
Last active July 11, 2022 19:42
Show Gist options
  • Save omaraflak/04d46c095d2158b9dda1e188b6163148 to your computer and use it in GitHub Desktop.
Save omaraflak/04d46c095d2158b9dda1e188b6163148 to your computer and use it in GitHub Desktop.
3D projection
import cv2
import numpy as np
import plotly.graph_objects as go
image = cv2.imread("earth.jpeg", cv2.IMREAD_COLOR)
height, width, _ = image.shape
image = cv2.resize(image, (300, int(height * 300 / width)))
height, width, _ = image.shape
theta = np.repeat(range(height), width) * np.pi / height
phi = np.tile(range(width), height) * 2 * np.pi / width
x = np.sin(theta) * np.cos(phi)
y = np.sin(theta) * np.sin(phi)
z = np.cos(theta)
b, g, r = np.dsplit(image.astype(np.uint32), 3)
c = np.vectorize(lambda i: f"#{i:06x}")(r << 16 | g << 8 | b).flatten()
fig = go.Figure(data=go.Scatter3d(x=x, y=y, z=z, mode="markers", marker=dict(color=c, size=3)))
fig.show()
@omaraflak
Copy link
Author

omaraflak commented Jul 10, 2022

earth.jpeg file:
earth

@omaraflak
Copy link
Author

omaraflak commented Jul 10, 2022

Result:
image

@omaraflak
Copy link
Author

But really you can render anything...
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment