Skip to content

Instantly share code, notes, and snippets.

Created August 16, 2017 06:48
What would you like to do?
OpenCV sample stereo camera usage
import numpy as np
import cv2
# Set up specifically for the ELP 1.0 Megapixel Dual Lens Usb Camera Module ...
# This camera enumerates as two separate cameras, hence the separate `left` and
# `right` VideoCapture instances. For a proper stereo camera with a common
# clock, use one VideoCapture instance and pass in whether you want the 0th or
# 1st camera in retrieve().
# TODO: Use more stable identifiers
left = cv2.VideoCapture(0)
right = cv2.VideoCapture(1)
# Increase the resolution
left.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
left.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
right.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
right.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
# Use MJPEG to avoid overloading the USB 2.0 bus at this resolution
left.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*"MJPG"))
right.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*"MJPG"))
# Grab both frames first, then retrieve to minimize latency between cameras
while(left.grab() and right.grab()):
_, leftFrame = left.retrieve()
leftWidth, leftHeight = leftFrame.shape[:2]
_, rightFrame = right.retrieve()
rightWidth, rightHeight = rightFrame.shape[:2]
# TODO: Calibrate the cameras and correct the images
cv2.imshow('left', leftFrame)
cv2.imshow('right', rightFrame)
if cv2.waitKey(1) & 0xFF == ord('q'):
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment