0

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.

zhizha
  • 11
  • 3
  • Please add your code, and the questions you've been through – Behdad Abdollahi Moghadam Oct 08 '21 at 11:31
  • 1. create a numpy array of your desired windows size (canvas-array). 2. resize and copy your images to any sub-array of that canvas-array. 3. imshow the canvas-array – Micka Oct 08 '21 at 13:39
  • "but it doesn't work with cameras" of course not, because it works with images (numpy arrays), not with cameras. that link you found works, as evidenced by the answer below using the same functions as in that link. – Christoph Rackwitz Oct 08 '21 at 16:47

1 Answers1

1

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

Roy Amoyal
  • 717
  • 4
  • 14