forked from microsoft/AirSim
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoint_cloud.py
59 lines (50 loc) · 2.07 KB
/
point_cloud.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# use open cv to create point cloud from depth image.
from AirSimClient import *
import cv2
import time
import sys
import math
import numpy as np
############################################
########## This is work in progress! #######
############################################
# file will be saved in PythonClient folder (i.e. same folder as script)
# point cloud ASCII format, use viewers like CloudCompare http://www.danielgm.net/cc/ or see http://www.geonext.nl/wp-content/uploads/2014/05/Point-Cloud-Viewers.pdf
outputFile = "cloud.asc"
color = (0,255,0)
rgb = "%d %d %d" % color
projectionMatrix = np.array([[-0.501202762, 0.000000000, 0.000000000, 0.000000000],
[0.000000000, -0.501202762, 0.000000000, 0.000000000],
[0.000000000, 0.000000000, 10.00000000, 100.00000000],
[0.000000000, 0.000000000, -10.0000000, 0.000000000]])
def printUsage():
print("Usage: python point_cloud.py [cloud.txt]")
def savePointCloud(image, fileName):
f = open(fileName, "w")
for x in range(image.shape[0]):
for y in range(image.shape[1]):
pt = image[x,y]
if (math.isinf(pt[0]) or math.isnan(pt[0])):
# skip it
None
else:
f.write("%f %f %f %s\n" % (pt[0], pt[1], pt[2]-1, rgb))
f.close()
for arg in sys.argv[1:]:
cloud.txt = arg
client = MultirotorClient()
while True:
rawImage = client.simGetImage(0, AirSimImageType.DepthPerspective)
if (rawImage is None):
print("Camera is not returning image, please check airsim for error messages")
sys.exit(0)
else:
png = cv2.imdecode(np.frombuffer(rawImage, np.uint8) , cv2.IMREAD_UNCHANGED)
gray = cv2.cvtColor(png, cv2.COLOR_BGR2GRAY)
Image3D = cv2.reprojectImageTo3D(gray, projectionMatrix)
savePointCloud(Image3D, outputFile)
print("saved " + outputFile)
sys.exit(0)
key = cv2.waitKey(1) & 0xFF;
if (key == 27 or key == ord('q') or key == ord('x')):
break;