Search code examples
pythonopencvcomputer-visionrobotics

Show Multiple Cameras in One Window


how can I display multiple cameras in one window?(OpenCv) Using this code: Capturing video from two cameras in OpenCV at once , I open multiple cameras in separate windows, but I want to show them in one. I found code for concanating images https://answers.opencv.org/question/188025/is-it-possible-to-show-two-video-feed-in-one-window/ but it doesn't work with cameras. Same question was asked here previously, but no answer was given.


Solution

  • You can do this using numpy methods.

    Option 1: np.vstack/np.hstack Option 2: np.concatenate

    Note 1: The methods will fail if you have different frames sizes because you are trying to do operation on matrices of different dimensions. That's why I resized one of the frames to fit the another.

    Note 2: OpenCV also has hconcat and vconcat methods but I didn't try to use them in python.

    Example Code: (using my Camera feed and a Video)

    import cv2
    import numpy as np
    
    capCamera = cv2.VideoCapture(0)
    capVideo = cv2.VideoCapture("desk.mp4")
    
    
    
    while True:
        isNextFrameAvail1, frame1 = capCamera.read()
        isNextFrameAvail2, frame2 = capVideo.read()
        if not isNextFrameAvail1 or not isNextFrameAvail2:
            break
        frame2Resized = cv2.resize(frame2,(frame1.shape[0],frame1.shape[1]))
    
        # ---- Option 1 ----
        #numpy_vertical = np.vstack((frame1, frame2))
        numpy_horizontal = np.hstack((frame1, frame2))
    
        # ---- Option 2 ----
        #numpy_vertical_concat = np.concatenate((image, grey_3_channel), axis=0)
        #numpy_horizontal_concat = np.concatenate((frame1, frame2), axis=1)
    
        cv2.imshow("Result", numpy_horizontal)
        cv2.waitKey(1)
    

    Result: (for horizontal concat)

    enter image description here