How to build Depthmap with python using stereoSGBM?

Hi, now i’m use raspberry pi 4 and ELP Dual Lens Camera(1.3MP 960P USB2.0 dual lens usb camera module Synchronization camera for 3D depth detection [ELP-960P2CAM-V90] - $0.00 : Surveillance Equipment,CCTV Systems,USB Camera Module Supplier, Best USB Cameras Module,Network IP Cameras,Analog CCTV Cameras,IP Network Video Recorders ,Digital Video Recorders,IP Network Camera Systems,Security Camera Systems,Mobile DVR Systems ,Security System Accessories,Color LCD/LED Monitor/Car Video,Wireless Tranmission Module ,Digital camera lens for building, city,airport,police,bank,shop,home and office etc.) for build depth map.

i’m working with a stereo camera n capture frames with opencv.
when i capture from camera its the results.

How can i splitting camera frame into left and right?

import cv2

CamL = cv2.VideoCapture(2 + cv2.CAP_DSHOW)
CamL.set(cv2.CAP_PROP_FRAME_WIDTH, 400)
CamL.set(cv2.CAP_PROP_FRAME_HEIGHT, 300)

CamR = cv2.VideoCapture(3 + cv2.CAP_DSHOW)
CamR.set(cv2.CAP_PROP_FRAME_WIDTH, 400)
CamR.set(cv2.CAP_PROP_FRAME_HEIGHT, 300)

while True:
    __, imgL =
    __, imgR =
    cv2.imshow('L', imgL)
    cv2.imshow('R', imgR)
    k = cv2.waitKey(30) & 0xff
    if k == 27:

i’m trying to do this script on my pi but error.
this is the error

Traceback (most recent call last):
File "/home/pi/Desktop/capture/", line 14 in <module>
cv2.imshow('L' imgL)
cv2.error : OpenCV(4.5.1) /home/pi/opencv/opencv-4.5.1/modules/highgui/src/window.cpp:384: error: (-215:Assertion failed) size.width>0 && size.height>0 function 'imshow'

what’s the problem?

your left image is invalid / empty. please do NOT throw away the return values from , but check them !

sorry, but this is a bit unclear. where is the problem, exactly ? can it be, you get both streams concatenated horizontally from a single capture, like the image you show above ? in that case, you have to split it into 2, not use 2 VideoCaptures

Resolution & frame
MJPEG:2560X960@ 60fps/2560X720@60fps /
1280X480@60fps /640X240@60fps

^^ this looks pretty much like it !

thx for reply but i have some more questions…

take photo with camera and split image for using this code for left image and right image
import cv2

import numpy as np

file = ‘capture47.jpg’

src = cv2.imread(file,0)

imgl = src[0:240, 0:320].copy()

imgr = src[0:240, 320:640].copy()

cv2.imshow(‘imgl’, imgl)

cv2.imshow(‘imgr’, imgr)

cv2.imwrite(‘imgl.jpg’, imgl)

cv2.imwrite(‘imgr.jpg’, imgr)

is it best way?

and i cant understand about ur reply
your left image is invalid / empty. please do NOT throw away the return values from , but check them !
is it mean i have to use command on my script?

well, IF you use a VideoCapture, make sure you have proper error handling

should have been:

ok, imgL =`
if (not ok):
     print("no image !")


there is Directshow on rpi4? Which version do you use?

imgL = cv2.imread(‘imgl.jpg’) # downscale images for faster processing

if imgL is None:

print('no image')

after add this command it keeps working

1 Like

i use pi 4 model B 1.2