Skip to content

Instantly share code, notes, and snippets.

@mayosuke
Forked from edz-o/depth_conversion.py
Last active June 14, 2020 17:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mayosuke/7bc20e084e5a4326931ec38fca2fd77f to your computer and use it in GitHub Desktop.
Save mayosuke/7bc20e084e5a4326931ec38fca2fd77f to your computer and use it in GitHub Desktop.
FOV = 90.0
plane_depth = DepthConversion(depth, FOV)
plt.imshow(applyColorMap(plane_depth))
H, W = depth.shape
x = np.arange(W)
y = H / 2
plt.plot(x, depth[y,x],label='distance from camera center')
plt.plot(x, plane_depth[y,x],label='distance from camera plane')
plt.legend()
plt.title(f'depth(y={y})')
plt.show()
# Refs: https://github.com/unrealcv/unrealcv/issues/14
import numpy as np
def DepthConversion(PointDepth, FOV):
H = PointDepth.shape[0]
W = PointDepth.shape[1]
f = W / 2 / np.tan(np.radians(FOV / 2))
i_c = np.float(H) / 2 - 1
j_c = np.float(W) / 2 - 1
columns, rows = np.meshgrid(np.linspace(0, W-1, num=W), np.linspace(0, H-1, num=H))
DistanceFromCenter = ((rows - i_c)**2 + (columns - j_c)**2)**(0.5)
PlaneDepth = PointDepth / (1 + (DistanceFromCenter / f)**2)**(0.5)
return PlaneDepth
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np
import cv2
from unrealcv import client
from io import BytesIO
def applyColorMap(npy, alpha=50):
return cv2.applyColorMap(cv2.convertScaleAbs(npy, alpha=alpha), cv2.COLORMAP_TURBO)
client.connect()
res = client.request('vget /camera/0/depth npy')
depth = np.load(BytesIO(res))
plt.imshow(applyColorMap(depth))
client.disconnect()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment