Skip to content

Commit f8adf6e

Browse files
Update the rgb_depth_confidence_aligned.py example: adding new Trackbars for percentage config of the weights used to blend the RGB, depth, and confidence images
1 parent 090cd4a commit f8adf6e

File tree

2 files changed

+57
-18
lines changed

2 files changed

+57
-18
lines changed

examples/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@ add_python_example(depth_crop_control StereoDepth/depth_crop_control.py)
168168
add_python_example(depth_preview StereoDepth/depth_preview.py)
169169
add_python_example(depth_post_processing StereoDepth/depth_post_processing.py)
170170
add_python_example(rgb_depth_aligned StereoDepth/rgb_depth_aligned.py)
171+
add_python_example(rgb_depth_confidence_aligned StereoDepth/rgb_depth_confidence_aligned.py)
171172
add_python_example(stereo_depth_from_host StereoDepth/stereo_depth_from_host.py)
172173
add_python_example(stereo_depth_video StereoDepth/stereo_depth_video.py)
173174

examples/StereoDepth/rgb_depth_confidence_aligned.py

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

7-
# Weights to use when blending depth/rgb image (should equal 1.0)
8-
rgbWeight = 0.4
9-
depthWeight = 0.6
7+
# Weights to use when blending rgb/depth/confidence image
8+
rgbWeight = 0.3
9+
depthWeight = 0.3
10+
confWeight = 0.3
11+
# Normalized weights to use when blending rgb/depth/confidence image (should equal 1.0)
12+
rgbWeightNorm = 0.3
13+
depthWeightNorm = 0.3
14+
confWeightNorm = 0.3
1015

1116

12-
def updateBlendWeights(percent_rgb):
17+
def updateRgbBlendWeights(percent):
1318
"""
14-
Update the rgb and depth weights used to blend depth/rgb image
19+
Update the rgb weight used to blend rgb/depth/confidence image
1520
16-
@param[in] percent_rgb The rgb weight expressed as a percentage (0..100)
21+
@param[in] percent The rgb weight expressed as a percentage (0..100)
1722
"""
18-
global depthWeight
1923
global rgbWeight
20-
rgbWeight = float(percent_rgb)/100.0
21-
depthWeight = 1.0 - rgbWeight
24+
rgbWeight = float(percent)/100.0
25+
26+
def updateDepthBlendWeights(percent):
27+
"""
28+
Update the depth weight used to blend rgb/depth/confidence image
29+
30+
@param[in] percent The depth weight expressed as a percentage (0..100)
31+
"""
32+
global depthWeight
33+
depthWeight = float(percent)/100.0
2234

35+
def updateConfBlendWeights(percent):
36+
"""
37+
Update the confidence weight used to blend rgb/depth/confidence image
38+
39+
@param[in] percent The confidence weight expressed as a percentage (0..100)
40+
"""
41+
global confWeight
42+
confWeight = float(percent)/100.0
2343

2444
# Optional. If set (True), the ColorCamera is downscaled from 1080p to 720p.
2545
# Otherwise (False), the aligned depth is automatically upscaled to 1080p
@@ -83,16 +103,20 @@ def updateBlendWeights(percent_rgb):
83103

84104
frameRgb = None
85105
frameDisp = None
106+
frameC = None
86107

87108
# Configure windows; trackbar adjusts blending ratio of rgb/depth
88109
rgbWindowName = "rgb"
89110
depthWindowName = "depth"
90-
blendedWindowName = "rgb-depth"
111+
confWindowName = "conf"
112+
blendedWindowName = "rgb-depth-conf"
91113
cv2.namedWindow(rgbWindowName)
92114
cv2.namedWindow(depthWindowName)
115+
cv2.namedWindow(confWindowName)
93116
cv2.namedWindow(blendedWindowName)
94-
cv2.createTrackbar('RGB Weight %', blendedWindowName, int(rgbWeight*100), 100, updateBlendWeights)
95-
117+
cv2.createTrackbar('RGB Weight %', blendedWindowName, int(rgbWeight*100), 100, updateRgbBlendWeights)
118+
cv2.createTrackbar('Depth Weight %', blendedWindowName, int(depthWeight*100), 100, updateDepthBlendWeights)
119+
cv2.createTrackbar('Confidence Weight %', blendedWindowName, int(confWeight*100), 100, updateConfBlendWeights)
96120
while True:
97121
latestPacket = {}
98122
latestPacket["rgb"] = None
@@ -111,7 +135,7 @@ def updateBlendWeights(percent_rgb):
111135

112136
if latestPacket["confidence_map"] is not None:
113137
frameC = latestPacket["confidence_map"].getCvFrame()
114-
cv2.imshow("conf", frameC)
138+
cv2.imshow(confWindowName, frameC)
115139

116140
if latestPacket["disp"] is not None:
117141
frameDisp = latestPacket["disp"].getFrame()
@@ -123,15 +147,29 @@ def updateBlendWeights(percent_rgb):
123147
frameDisp = np.ascontiguousarray(frameDisp)
124148
cv2.imshow(depthWindowName, frameDisp)
125149

126-
# Blend when both received
127-
if frameRgb is not None and frameDisp is not None:
128-
# Need to have both frames in BGR format before blending
150+
# Blend when all three frames received
151+
if frameRgb is not None and frameDisp is not None and frameC is not None:
152+
# Need to have all three frames in BGR format before blending
129153
if len(frameDisp.shape) < 3:
130154
frameDisp = cv2.cvtColor(frameDisp, cv2.COLOR_GRAY2BGR)
131-
blended = cv2.addWeighted(frameRgb, rgbWeight, frameDisp, depthWeight, 0)
132-
cv2.imshow(blendedWindowName, blended)
155+
if len(frameC.shape) < 3:
156+
frameC = cv2.cvtColor(frameC, cv2.COLOR_GRAY2BGR)
157+
sumWeight = rgbWeight + depthWeight + confWeight
158+
# Normalize the weights so their sum to be <= 1.0
159+
if sumWeight <= 1.0:
160+
rgbWeightNorm = rgbWeight
161+
depthWeightNorm = depthWeight
162+
confWeightNorm = confWeight
163+
else :
164+
rgbWeightNorm = rgbWeight / sumWeight
165+
depthWeightNorm = depthWeight / sumWeight
166+
confWeightNorm = confWeight / sumWeight
167+
blended1 = cv2.addWeighted(frameRgb, rgbWeightNorm, frameDisp, depthWeightNorm, 0)
168+
blended2 = cv2.addWeighted(blended1, rgbWeightNorm + depthWeightNorm, frameC, confWeightNorm, 0)
169+
cv2.imshow(blendedWindowName, blended2)
133170
frameRgb = None
134171
frameDisp = None
172+
frameC = None
135173

136174
if cv2.waitKey(1) == ord('q'):
137175
break

0 commit comments

Comments
 (0)