Skip to content

Commit e49c9d1

Browse files
Merge pull request #220 from luxonis/fix_demo_disparity
Fix demo disparity
2 parents 81f88e7 + 8946ad3 commit e49c9d1

File tree

3 files changed

+71
-45
lines changed

3 files changed

+71
-45
lines changed

examples/03_depth_preview.py

Lines changed: 34 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,24 @@
44
import depthai as dai
55
import numpy as np
66

7+
8+
'''
9+
If one or more of the additional depth modes (lrcheck, extended, subpixel)
10+
are enabled, then:
11+
- depth output is FP16. TODO enable U16.
12+
- median filtering is disabled on device. TODO enable.
13+
- with subpixel, either depth or disparity has valid data.
14+
Otherwise, depth output is U16 (mm) and median is functional.
15+
But like on Gen1, either depth or disparity has valid data. TODO enable both.
16+
'''
17+
18+
# Closer-in minimum depth, disparity range is doubled (from 95 to 190):
19+
extended_disparity = False
20+
# Better accuracy for longer distance, fractional disparity 32-levels:
21+
subpixel = False
22+
# Better handling for occlusions:
23+
lr_check = False
24+
725
# Start defining a pipeline
826
pipeline = dai.Pipeline()
927

@@ -19,25 +37,25 @@
1937
# Create a node that will produce the depth map (using disparity output as it's easier to visualize depth this way)
2038
depth = pipeline.createStereoDepth()
2139
depth.setConfidenceThreshold(200)
40+
depth.setOutputDepth(False)
2241
# Options: MEDIAN_OFF, KERNEL_3x3, KERNEL_5x5, KERNEL_7x7 (default)
2342
median = dai.StereoDepthProperties.MedianFilter.KERNEL_7x7 # For depth filtering
2443
depth.setMedianFilter(median)
2544

26-
'''
27-
If one or more of the additional depth modes (lrcheck, extended, subpixel)
28-
are enabled, then:
29-
- depth output is FP16. TODO enable U16.
30-
- median filtering is disabled on device. TODO enable.
31-
- with subpixel, either depth or disparity has valid data.
32-
Otherwise, depth output is U16 (mm) and median is functional.
33-
But like on Gen1, either depth or disparity has valid data. TODO enable both.
34-
'''
35-
# Better handling for occlusions:
36-
depth.setLeftRightCheck(False)
37-
# Closer-in minimum depth, disparity range is doubled:
38-
depth.setExtendedDisparity(False)
39-
# Better accuracy for longer distance, fractional disparity 32-levels:
40-
depth.setSubpixel(False)
45+
depth.setLeftRightCheck(lr_check)
46+
47+
# Normal disparity values range from 0..95, will be used for normalization
48+
max_disparity = 95
49+
50+
if extended_disparity: max_disparity *= 2 # Double the range
51+
depth.setExtendedDisparity(extended_disparity)
52+
53+
if subpixel: max_disparity *= 32 # 5 fractional bits, x32
54+
depth.setSubpixel(subpixel)
55+
56+
# When we get disparity to the host, we will multiply all values with the multiplier
57+
# for better visualization
58+
multiplier = 255 / max_disparity
4159

4260
left.out.link(depth.left)
4361
right.out.link(depth.right)
@@ -54,12 +72,10 @@
5472

5573
# Output queue will be used to get the disparity frames from the outputs defined above
5674
q = device.getOutputQueue(name="disparity", maxSize=4, blocking=False)
57-
5875
while True:
5976
inDepth = q.get() # blocking call, will wait until a new data has arrived
6077
frame = inDepth.getFrame()
61-
frame = cv2.normalize(frame, None, 0, 255, cv2.NORM_MINMAX)
62-
78+
frame = (frame*multiplier).astype(np.uint8)
6379
# Available color maps: https://docs.opencv.org/3.4/d3/d50/group__imgproc__colormap.html
6480
frame = cv2.applyColorMap(frame, cv2.COLORMAP_JET)
6581

examples/10_mono_depth_mobilenetssd.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,9 @@
5555
manip.out.link(nn.input)
5656

5757
# Create outputs
58-
depthOut = pipeline.createXLinkOut()
59-
depthOut.setStreamName("depth")
60-
61-
stereo.disparity.link(depthOut.input)
58+
disparityOut = pipeline.createXLinkOut()
59+
disparityOut.setStreamName("disparity")
60+
stereo.disparity.link(disparityOut.input)
6261

6362
xoutRight = pipeline.createXLinkOut()
6463
xoutRight.setStreamName("rectifiedRight")
@@ -79,7 +78,7 @@
7978

8079
# Output queues will be used to get the grayscale / depth frames and nn data from the outputs defined above
8180
qRight = device.getOutputQueue("rectifiedRight", maxSize=4, blocking=False)
82-
qDepth = device.getOutputQueue("depth", maxSize=4, blocking=False)
81+
qDisparity = device.getOutputQueue("disparity", maxSize=4, blocking=False)
8382
qDet = device.getOutputQueue("nn", maxSize=4, blocking=False)
8483

8584
rightFrame = None
@@ -104,11 +103,12 @@ def show(name, frame):
104103
# Show the frame
105104
cv2.imshow(name, frame)
106105

106+
disparity_multiplier = 255 / 95 # Disparity range is 0..95
107107
while True:
108108
# Instead of get (blocking), we use tryGet (nonblocking) which will return the available data or None otherwise
109109
inRight = qRight.tryGet()
110110
inDet = qDet.tryGet()
111-
inDepth = qDepth.tryGet()
111+
inDisparity = qDisparity.tryGet()
112112

113113
if inRight is not None:
114114
rightFrame = inRight.getCvFrame()
@@ -124,13 +124,13 @@ def show(name, frame):
124124
detection.xmin = 1 - detection.xmax
125125
detection.xmax = 1 - swap
126126

127-
if inDepth is not None:
128-
# Frame is transformed, the color map will be applied to highlight the depth info
127+
if inDisparity is not None:
128+
# Frame is transformed, normalized, and color map will be applied to highlight the depth info
129+
disparityFrame = inDisparity.getFrame()
130+
disparityFrame = (disparityFrame*disparity_multiplier).astype(np.uint8)
129131
# Available color maps: https://docs.opencv.org/3.4/d3/d50/group__imgproc__colormap.html
130-
depthFrame = cv2.applyColorMap(inDepth.getFrame(), cv2.COLORMAP_JET)
131-
132-
if depthFrame is not None:
133-
show("depth", depthFrame)
132+
disparityFrame = cv2.applyColorMap(disparityFrame, cv2.COLORMAP_JET)
133+
show("disparity", disparityFrame)
134134

135135
if rightFrame is not None:
136136
show("rectified right", rightFrame)

examples/12_rgb_encoding_mono_mobilenet_depth.py

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import depthai as dai
77
import numpy as np
88

9+
flipRectified = True
10+
911
# Get argument first
1012
nnPath = str((Path(__file__).parent / Path('models/mobilenet-ssd_openvino_2021.2_6shave.blob')).resolve().absolute())
1113
if len(sys.argv) > 1:
@@ -40,13 +42,16 @@
4042
depth.setConfidenceThreshold(255)
4143
# Note: the rectified streams are horizontally mirrored by default
4244
depth.setOutputRectified(True)
45+
depth.setRectifyMirrorFrame(False)
4346
depth.setRectifyEdgeFillColor(0) # Black, to better see the cutout
4447
camLeft.out.link(depth.left)
4548
camRight.out.link(depth.right)
49+
# Disparity range is 0..95, used for normalization
50+
disparity_multiplier = 255 / 95
4651

47-
depthOut = pipeline.createXLinkOut()
48-
depthOut.setStreamName("depth")
49-
depth.disparity.link(depthOut.input)
52+
disparityOut = pipeline.createXLinkOut()
53+
disparityOut.setStreamName("disparity")
54+
depth.disparity.link(disparityOut.input)
5055

5156
nn = pipeline.createMobileNetDetectionNetwork()
5257
nn.setConfidenceThreshold(0.5)
@@ -85,14 +90,14 @@
8590

8691
queueSize = 8
8792
qRight = device.getOutputQueue("right", queueSize)
88-
qDepth = device.getOutputQueue("depth", queueSize)
93+
qDisparity = device.getOutputQueue("disparity", queueSize)
8994
qManip = device.getOutputQueue("manip", queueSize)
9095
qDet = device.getOutputQueue("nn", queueSize)
9196
qRgbEnc = device.getOutputQueue('h265', maxSize=30, blocking=True)
9297

9398
frame = None
9499
frameManip = None
95-
frameDepth = None
100+
frameDisparity = None
96101
detections = []
97102
offsetX = (camRight.getResolutionWidth() - camRight.getResolutionHeight()) // 2
98103
croppedFrame = np.zeros((camRight.getResolutionHeight(), camRight.getResolutionHeight()))
@@ -111,21 +116,26 @@ def frameNorm(frame, bbox):
111116
inRight = qRight.tryGet()
112117
inManip = qManip.tryGet()
113118
inDet = qDet.tryGet()
114-
inDepth = qDepth.tryGet()
119+
inDisparity = qDisparity.tryGet()
115120

116121
while qRgbEnc.has():
117122
qRgbEnc.get().getData().tofile(videoFile)
118123

119124
if inRight is not None:
120125
frame = cv2.flip(inRight.getCvFrame(), 1)
126+
if flipRectified:
127+
frame = cv2.flip(frame, 1)
121128

122129
if inManip is not None:
123130
frameManip = inManip.getCvFrame()
124131

125-
if inDepth is not None:
126-
frameDepth = cv2.flip(inDepth.getFrame(), 1)
127-
frameDepth = cv2.normalize(frameDepth, None, 0, 255, cv2.NORM_MINMAX)
128-
frameDepth = cv2.applyColorMap(frameDepth, cv2.COLORMAP_JET)
132+
if inDisparity is not None:
133+
# Flip disparity frame, normalize it and apply color map for better visualization
134+
frameDisparity = inDisparity.getFrame()
135+
if flipRectified:
136+
frameDisparity = cv2.flip(frameDisparity, 1)
137+
frameDisparity = (frameDisparity*disparity_multiplier).astype(np.uint8)
138+
frameDisparity = cv2.applyColorMap(frameDisparity, cv2.COLORMAP_JET)
129139

130140
if inDet is not None:
131141
detections = inDet.detections
@@ -139,14 +149,14 @@ def frameNorm(frame, bbox):
139149
cv2.putText(frame, f"{int(detection.confidence * 100)}%", (bbox[0] + 10, bbox[1] + 40), cv2.FONT_HERSHEY_TRIPLEX, 0.5, 255)
140150
cv2.imshow("right", frame)
141151

142-
if frameDepth is not None:
152+
if frameDisparity is not None:
143153
for detection in detections:
144154
bbox = frameNorm(croppedFrame, (detection.xmin, detection.ymin, detection.xmax, detection.ymax))
145155
bbox[::2] += offsetX
146-
cv2.rectangle(frameDepth, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (255, 0, 0), 2)
147-
cv2.putText(frameDepth, labelMap[detection.label], (bbox[0] + 10, bbox[1] + 20), cv2.FONT_HERSHEY_TRIPLEX, 0.5, 255)
148-
cv2.putText(frameDepth, f"{int(detection.confidence * 100)}%", (bbox[0] + 10, bbox[1] + 40), cv2.FONT_HERSHEY_TRIPLEX, 0.5, 255)
149-
cv2.imshow("depth", frameDepth)
156+
cv2.rectangle(frameDisparity, (bbox[0], bbox[1]), (bbox[2], bbox[3]), (255, 0, 0), 2)
157+
cv2.putText(frameDisparity, labelMap[detection.label], (bbox[0] + 10, bbox[1] + 20), cv2.FONT_HERSHEY_TRIPLEX, 0.5, 255)
158+
cv2.putText(frameDisparity, f"{int(detection.confidence * 100)}%", (bbox[0] + 10, bbox[1] + 40), cv2.FONT_HERSHEY_TRIPLEX, 0.5, 255)
159+
cv2.imshow("disparity", frameDisparity)
150160

151161
if frameManip is not None:
152162
for detection in detections:

0 commit comments

Comments
 (0)