How to unify display windows?

Asked

Viewed 136 times

-1

Good afternoon, I’m using 2 cameras in a project, but when I run the code it opens in two different windows, I’m trying to make the two images open in the same window... Can someone help me? Thank you.

This is my current code...

import cv2
from datetime import datetime

print("Capturando o vídeo -- pressione <q> para encerrar...'")

direita = cv2.VideoCapture(0)
esquerda = cv2.VideoCapture(1)

if (direita.isOpened() == False):
    print("Camera DIREITA não conectada!")
if (esquerda.isOpened() == False):
    print("Camera ESQUERDA não conectada!")   


data = datetime.now().strftime("%H.%M.%S_%Y.%m.%d")
fourcc1 = cv2.VideoWriter_fourcc(*'DIVX')
fourcc2 = cv2.VideoWriter_fourcc(*'DIVX')
file1 = ('C:/Users/Car/DIREITA/Direita ({}).avi'.format(data))
file2 = ('C:/Users/Car/ESQUERDA/Esquerda ({}).avi'.format(data))

out1 = cv2.VideoWriter(file1,fourcc1,6.0,(640,480))
out2 = cv2.VideoWriter(file2,fourcc2,6.0,(640,480))




while(True):
    ret, frame1 = direita.read()
    ret, frame2 = esquerda.read()
    cv2.imshow("DIREITA",frame1)
    cv2.imshow ("ESQUERDA",frame2)
    if ret==True:
        out1.write(frame1)
        out2.write(frame2)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

direita.release()
esquerda.release()
out1.release()
out2.release()
cv2.destroyAllWindows()

1 answer

1

You can use the function hstack of Numpy to join the images. Because images are matrices.

So instead of:

cv2.imshow("DIREITA",frame1)
cv2.imshow ("ESQUERDA",frame2)

Two frames are joined together as a single frame:

import numpy as np

# [...]
# Código

cv2.imshow ("ESQUERDA/DIREITA",np.hstack([frame1, frame2]))

Note: The two images must have the same resolution and numbers channel.

If the color space is not the same, for example an grayscale and other BGR, convert to the same color space with cv2.cvtColor(cinza, cv2.COLOR_GRAY2BGR).

The function hstack stack the matrices horizontally, then height the image needs to be the same. If you want one below the other, use the vstack. Or just the function stack for very different images.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.