-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlidar.py
65 lines (61 loc) · 2.72 KB
/
lidar.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
60
61
62
63
64
65
# Copyright 2018 MicroVision, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Written by Chao Chen
import cv2 as cv
import numpy as np
# Capture from camera at location 1. The number could change depending on how many cameras are connected to this computer.
foundLidar = False
for i in range (0, 3) :
cam = cv.VideoCapture(i)
if cam :
width = cam.get(3)
height = cam.get(4)
if (width == 120 or width == 160 or width == 240) and height == 720 :
# cam.set(cv.cv.CV_CAP_PROP_FOURCC, cv.cv.CV_FOURCC('B', 'G', 'R', '3')) #This line is needed for OpenCV2.*
cam.set(3, 160) # set the width cv.cv.CV_CAP_PROP_FRAME_WIDTH 160
cam.set(4, 720) # set the height cv.cv.CV_CAP_PROP_FRAME_HEIGHT 720
foundLidar = True
break;
cam.release()
if foundLidar :
print "Start Lidar camera. Hit Esc key to quit"
else :
print "Couldn't detect MicroVision Lidar camera. Exit..."
# grab camera frames and display
while foundLidar:
ret, img = cam.read()
# Our depth sensing camera output format has 120*720 pixels. Each pixel has 4 byes, 2 for depth, 2 for amplitude.
# Since python OpenCV only support BRG3 format, our lidar will report 160*720 BGR3 format with 3 bytes/pixel.
# Reshaped img from 720*160*3 to 720*120*4 format
img = np.reshape(img, [720, 120, 4])
# img[:, :, 0:1] are depth data. Squeeze to 8 bits for gray scale display. The real depth data could be out of displayed range.
depth0 = img[:, :, 0]
depth1 = img[:, :, 1]
depth= (depth1 & 0x01)*128 + depth0/2
depthImg = np.dstack((depth, depth, depth))
# img[:, :, 2:3] are amplitude data. Squeeze to 8 bits for gray scale display. The real amp data could be out of displayed range.
amp0 = img[:, :, 2]
amp1 = img[:, :, 3]
amp = amp1*16 + amp0/16
ampImg = np.dstack((amp, amp, amp))
# stack depth image on top of amplitude image
combined = np.vstack((depthImg, ampImg))
rimg = cv.resize(combined, (960, 720), interpolation = cv.INTER_CUBIC)
cv.imshow("Lidar Camera", rimg)
key = cv.waitKey(10)
if key == 27: # Esc key
break
cv.destroyAllWindows()
cam.release()