luxonis / depthai-python

DepthAI Python Library
MIT License
361 stars 193 forks source link

Depth Preview code and stereo matching fine-turning #528

Open zpsandzzc opened 2 years ago

zpsandzzc commented 2 years ago

When I run Depth Preview(SGBM stereo matching in introduction) I get some troubles in Source code(your provided in Depth Preview part in 720P) list as follow:

import cv2
import depthai as dai
import numpy as np
extended_disparity = False
subpixel = False
lr_check = True
pipeline = dai.Pipeline()
monoLeft = pipeline.create(dai.node.MonoCamera)
monoRight = pipeline.create(dai.node.MonoCamera)
depth = pipeline.create(dai.node.StereoDepth)
xout = pipeline.create(dai.node.XLinkOut)
xout.setStreamName("disparity")
monoLeft.setResolution(dai.MonoCameraProperties.SensorResolution.THE_720_P)
monoLeft.setBoardSocket(dai.CameraBoardSocket.LEFT)
monoRight.setResolution(dai.MonoCameraProperties.SensorResolution.THE_720_P)
monoRight.setBoardSocket(dai.CameraBoardSocket.RIGHT)
depth.setDefaultProfilePreset(dai.node.StereoDepth.PresetMode.HIGH_DENSITY)
depth.initialConfig.setMedianFilter(dai.MedianFilter.KERNEL_7x7)
depth.setLeftRightCheck(lr_check)
depth.setExtendedDisparity(extended_disparity)
depth.setSubpixel(subpixel)
monoLeft.out.link(depth.left)
monoRight.out.link(depth.right)
depth.disparity.link(xout.input)
with dai.Device(pipeline) as device:
    q = device.getOutputQueue(name="disparity", maxSize=4, blocking=False)
    while True:
        inDisparity = q.get()  # blocking call, will wait until a new data has arrived
        frame = inDisparity.getFrame()
        frame = (frame * (255 / depth.initialConfig.getMaxDisparity())).astype(np.uint8)
        cv2.imshow("disparity", frame)
        frame = cv2.applyColorMap(frame, cv2.COLORMAP_JET)
        cv2.imshow("disparity_color", frame)
        if cv2.waitKey(1) == ord('q'):
            break

Then I got nice disparity: image

but in my rewrite for other application, it seems no changes in my rewrite from:

import cv2
import depthai as dai
import numpy as np
extended_disparity = False
subpixel=False
lr_check=True
pipeline = dai.Pipeline()
monoLeft = pipeline.create(dai.node.MonoCamera)
monoRight = pipeline.create(dai.node.MonoCamera)
depth = pipeline.create(dai.node.StereoDepth)
xout = pipeline.create(dai.node.XLinkOut)
xout.setStreamName('disparity')
monoLeft.setResolution(dai.MonoCameraProperties.SensorResolution.THE_720_P)
monoLeft.setBoardSocket(dai.CameraBoardSocket.LEFT)
monoRight.setResolution(dai.MonoCameraProperties.SensorResolution.THE_720_P)
monoRight.setBoardSocket(dai.CameraBoardSocket.RIGHT)
depth.setDefaultProfilePreset(dai.node.StereoDepth.PresetMode.HIGH_DENSITY)
depth.initialConfig.setMedianFilter(dai.MedianFilter.KERNEL_7x7)
depth.setLeftRightCheck(lr_check)
depth.setExtendedDisparity(extended_disparity)
depth.setSubpixel(subpixel)
monoRight.out.link(depth.left)
monoLeft.out.link(depth.right)
depth.disparity.link(xout.input)
with dai.Device(pipeline) as device:
    q = device.getOutputQueue(name="disparity", maxSize=4, blocking=False)
    count=0
    while True:
        indepth = q.get()
        frame = indepth.getFrame()
        frame = (frame * (255 / depth.initialConfig.getMaxDisparity())).astype(np.uint8)
        cv2.imshow("depth", frame)
        if cv2.waitKey(1) == ord('q'):
            break

I got the terrible disparity map like this: image

I'm curious about why, I tried to adjust the SGBM parameters myself in anther Python file which similar images pair, but I couldn't get a high-quality disparity map like Depth Preview Source Code. Could please provide your SGBM fine-turning parameters, and do me a favor to find out why my rewrite form in such poor performance?

Thanks Best regards

Luxonis-Brandon commented 2 years ago

Sorry about the trouble. Asking.

zpsandzzc commented 2 years ago

Sorry I find the left and right in wrong place

monoRight.out.link(depth.left)
monoLeft.out.link(depth.right)

When I fixed the nice disparity was shown. Could still help me about SGBM fine-turning? It's really great under your adjustment :D

Best regards

SzabolcsGergely commented 2 years ago

@zpsandzzc what do you mean by fine-tuned SGBM parameters like in depth_preview? Your code (after fixing left and right swap) is identical to depth_preview. The default config already has fine tuned parameters. For further improvements you may check: https://github.com/luxonis/depthai-python/blob/main/examples/StereoDepth/depth_post_processing.py