使用OpenCV分别操作立体相机的两个相机

问题描述 投票:0回答:1

我有一个立体摄像头,我希望两个摄像头单独工作,这样我就可以执行摄像头校准和其他技术来实现立体视觉。显示它的代码是基本的 OpenCV,如果有人想看的话。

这是代码:

import cv2

def capture_dual_camera_module(camera_index_left=1, camera_index_right=2):
    
    """
    Captures video streams from two cameras (stereo camera) simultaneously.

    Args:
        camera_index_left (int, optional): Index of the left camera (default: 0).
        camera_index_right (int, optional): Index of the right camera (default: 1).

    Returns:
        None
    """

    cap_left = cv2.VideoCapture(camera_index_left)
    cap_right = cv2.VideoCapture(camera_index_right)

    if not (cap_left.isOpened() and cap_right.isOpened()):
        print("Error: One or both camera streams could not be opened.")
        return

    while True:
        # Capture frames from both cameras
        ret_left, frame_left = cap_left.read()
        ret_right, frame_right = cap_right.read()

        if not ret_left or not ret_right:
            print("Error: Couldn't read frames from one or both camera streams.")
            break

        # Display the resulting frames (adjust window names for clarity):
        cv2.imshow('Left Camera', frame_left)
        cv2.imshow('Right Camera', frame_right)

        # Break the loop on 'q' key press
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

    # Release capture resources
    cap_left.release()
    cap_right.release()
    cv2.destroyAllWindows()

if __name__ == "__main__":
    capture_dual_camera_module()  

我有一个立体摄像头,在作为 USB 摄像头正常操作时工作得很好。但我想要的是分别控制这两个模块。我通过传递相机索引使用 OpenCV 进行了尝试。这里的问题是我的笔记本电脑的网络摄像头的索引为0,而USB立体摄像头的索引为1。当我传递索引[0 1]时,它会释放网络摄像头和立体摄像头。当我传递索引 [1 2] 时,它显示错误:

[ERROR:[email protected]] global obsensor_uvc_stream_channel.cpp:159 cv::obsensor::getStreamChannelGroup Camera index out of range

[ERROR:[email protected]] global obsensor_uvc_stream_channel.cpp:159 cv::obsensor::getStreamChannelGroup Camera index out of range

Error: One or both camera streams could not be opened.

这是我正在使用的相机

python opencv object-detection camera-calibration stereo-3d
1个回答
0
投票

我有使用立体相机的经验。该相机上的处理器会自动将左右相机的结果融合成一张大图像,使得立体相机仅由1个索引表示。要分割图像,你可以这样做

camera_index = 1

cap = cv2.VideoCapture(camera_index)

if not (cap.isOpened()):
    print("Not found")
    return

while True:
    ref, frame = cap.read()
    h, w, channels = frame.shape
    half = w//2

    # left camera
    left_part = frame[:, :half] 

    # right camera
    right_part = frame[:, half:] 

另一个提示:

当您重新启动计算机时,相机索引有时会发生变化。所以不要忘记检查立体相机的索引。

© www.soinside.com 2019 - 2024. All rights reserved.