|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import cv2 |
| 4 | +import numpy as np |
| 5 | +import depthai as dai |
| 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 |
| 15 | + |
| 16 | + |
| 17 | +def updateRgbBlendWeights(percent): |
| 18 | + """ |
| 19 | + Update the rgb weight used to blend rgb/depth/confidence image |
| 20 | +
|
| 21 | + @param[in] percent The rgb weight expressed as a percentage (0..100) |
| 22 | + """ |
| 23 | + global 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 |
| 34 | + |
| 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 |
| 43 | + |
| 44 | +# Optional. If set (True), the ColorCamera is downscaled from 1080p to 720p. |
| 45 | +# Otherwise (False), the aligned depth is automatically upscaled to 1080p |
| 46 | +downscaleColor = True |
| 47 | +fps = 30 |
| 48 | +# The disparity is computed at this resolution, then upscaled to RGB resolution |
| 49 | +monoResolution = dai.MonoCameraProperties.SensorResolution.THE_720_P |
| 50 | + |
| 51 | +# Create pipeline |
| 52 | +pipeline = dai.Pipeline() |
| 53 | +queueNames = [] |
| 54 | + |
| 55 | +# Define sources and outputs |
| 56 | +camRgb = pipeline.create(dai.node.ColorCamera) |
| 57 | +left = pipeline.create(dai.node.MonoCamera) |
| 58 | +right = pipeline.create(dai.node.MonoCamera) |
| 59 | +stereo = pipeline.create(dai.node.StereoDepth) |
| 60 | + |
| 61 | +rgbOut = pipeline.create(dai.node.XLinkOut) |
| 62 | +disparityOut = pipeline.create(dai.node.XLinkOut) |
| 63 | + |
| 64 | +rgbOut.setStreamName("rgb") |
| 65 | +queueNames.append("rgb") |
| 66 | +disparityOut.setStreamName("disp") |
| 67 | +queueNames.append("disp") |
| 68 | + |
| 69 | +#Properties |
| 70 | +camRgb.setBoardSocket(dai.CameraBoardSocket.RGB) |
| 71 | +camRgb.setResolution(dai.ColorCameraProperties.SensorResolution.THE_1080_P) |
| 72 | +camRgb.setFps(fps) |
| 73 | +if downscaleColor: camRgb.setIspScale(2, 3) |
| 74 | +# For now, RGB needs fixed focus to properly align with depth. |
| 75 | +# This value was used during calibration |
| 76 | +camRgb.initialControl.setManualFocus(130) |
| 77 | + |
| 78 | +left.setResolution(monoResolution) |
| 79 | +left.setBoardSocket(dai.CameraBoardSocket.LEFT) |
| 80 | +left.setFps(fps) |
| 81 | +right.setResolution(monoResolution) |
| 82 | +right.setBoardSocket(dai.CameraBoardSocket.RIGHT) |
| 83 | +right.setFps(fps) |
| 84 | + |
| 85 | +stereo.setDefaultProfilePreset(dai.node.StereoDepth.PresetMode.HIGH_DENSITY) |
| 86 | +# LR-check is required for depth alignment |
| 87 | +stereo.setLeftRightCheck(True) |
| 88 | +if 0: stereo.setSubpixel(True) # TODO enable for test |
| 89 | +stereo.setDepthAlign(dai.CameraBoardSocket.RGB) |
| 90 | + |
| 91 | +xoutConfMap = pipeline.create(dai.node.XLinkOut) |
| 92 | +xoutConfMap.setStreamName('confidence_map') |
| 93 | + |
| 94 | +# Linking |
| 95 | +camRgb.isp.link(rgbOut.input) |
| 96 | +left.out.link(stereo.left) |
| 97 | +right.out.link(stereo.right) |
| 98 | +stereo.disparity.link(disparityOut.input) |
| 99 | +stereo.confidenceMap.link(xoutConfMap.input) |
| 100 | + |
| 101 | +# Connect to device and start pipeline |
| 102 | +with dai.Device(pipeline) as device: |
| 103 | + |
| 104 | + frameRgb = None |
| 105 | + frameDisp = None |
| 106 | + frameC = None |
| 107 | + |
| 108 | + # Configure windows; trackbar adjusts blending ratio of rgb/depth |
| 109 | + rgbWindowName = "rgb" |
| 110 | + depthWindowName = "depth" |
| 111 | + confWindowName = "conf" |
| 112 | + blendedWindowName = "rgb-depth-conf" |
| 113 | + cv2.namedWindow(rgbWindowName) |
| 114 | + cv2.namedWindow(depthWindowName) |
| 115 | + cv2.namedWindow(confWindowName) |
| 116 | + cv2.namedWindow(blendedWindowName) |
| 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) |
| 120 | + while True: |
| 121 | + latestPacket = {} |
| 122 | + latestPacket["rgb"] = None |
| 123 | + latestPacket["disp"] = None |
| 124 | + latestPacket["confidence_map"] = None |
| 125 | + |
| 126 | + queueEvents = device.getQueueEvents(("rgb", "disp", "confidence_map")) |
| 127 | + for queueName in queueEvents: |
| 128 | + packets = device.getOutputQueue(queueName).tryGetAll() |
| 129 | + if len(packets) > 0: |
| 130 | + latestPacket[queueName] = packets[-1] |
| 131 | + |
| 132 | + if latestPacket["rgb"] is not None: |
| 133 | + frameRgb = latestPacket["rgb"].getCvFrame() |
| 134 | + cv2.imshow(rgbWindowName, frameRgb) |
| 135 | + |
| 136 | + if latestPacket["confidence_map"] is not None: |
| 137 | + frameC = latestPacket["confidence_map"].getCvFrame() |
| 138 | + cv2.imshow(confWindowName, frameC) |
| 139 | + |
| 140 | + if latestPacket["disp"] is not None: |
| 141 | + frameDisp = latestPacket["disp"].getFrame() |
| 142 | + maxDisparity = stereo.initialConfig.getMaxDisparity() |
| 143 | + # Optional, extend range 0..95 -> 0..255, for a better visualisation |
| 144 | + if 1: frameDisp = (frameDisp * 255. / maxDisparity).astype(np.uint8) |
| 145 | + # Optional, apply false colorization |
| 146 | + if 1: frameDisp = cv2.applyColorMap(frameDisp, cv2.COLORMAP_HOT) |
| 147 | + frameDisp = np.ascontiguousarray(frameDisp) |
| 148 | + cv2.imshow(depthWindowName, frameDisp) |
| 149 | + |
| 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 |
| 153 | + if len(frameDisp.shape) < 3: |
| 154 | + frameDisp = cv2.cvtColor(frameDisp, cv2.COLOR_GRAY2BGR) |
| 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) |
| 170 | + frameRgb = None |
| 171 | + frameDisp = None |
| 172 | + frameC = None |
| 173 | + |
| 174 | + if cv2.waitKey(1) == ord('q'): |
| 175 | + break |
0 commit comments