How to show content captured by camera in full screen in Opencv?

Asked

Viewed 1,617 times

4

Would you like to know how to show the content captured via webcam (by the Pture video) in a window that occupies the entire screen? Only appears in a small window.

You have to change something in imshow(" imagem ", frame)?

1 answer

4


Yes, there is a way. Create the window before using, setting its size to normal. Then use the function setWindowProperty to set the window as full screen.

Example of commented code:

import cv2

# Define a janela de exibição das imagens, com tamanho manual e em tela cheia
winName = 'Janela de Teste para o SOPT'
cv2.namedWindow(winName, cv2.WINDOW_NORMAL)
cv2.setWindowProperty(winName, cv2.WND_PROP_FULLSCREEN, cv2.WINDOW_FULLSCREEN)

# Abre a webcam para captura de vídeo
cam = cv2.VideoCapture(0)

# Laço de execução infinito (até que o usuário feche com a tecla 'q' ou ESC)
while True:

    # Captura um novo quadro da webcam
    ok, frame = cam.read()
    if not ok:
        break

    # Exibe a imagem na janela existente
    cv2.imshow(winName, frame)

    # Aguarda o pressionamento de uma tecla ou 30 milisegundos
    k = cv2.waitKey(10)
    if k == ord('q') or k == ord('Q') or k == 27:
        break

# Libera a memória do programa
cam.release()
cv2.destroyAllWindows()

Now, if what you want is not full screen even, but rather control the size of the window, what you need to do is scale the image before displaying it. Here’s an example of code that puts the image in a window only in the second half of the monitor (considering the Full HD resolution I use here):

import cv2

# Define o tamanho desejado para a janela
w = 960
h = 1080

# Define a janela de exibição das imagens, com tamanho automático
winName = 'Janela de Teste para o SOPT'
cv2.namedWindow(winName, cv2.WINDOW_AUTOSIZE)

# Posiciona a janela na metade direita do (meu) monitor
cv2.moveWindow(winName, 960, 0)

# Abre a webcam para captura de vídeo
cam = cv2.VideoCapture(0)

# Laço de execução infinito (até que o usuário feche com a tecla 'q' ou ESC)
while True:

    # Captura um novo quadro da webcam
    ok, frame = cam.read()
    if not ok:
        break

    # Altera o tamanho da imagem para o desejado
    frame = cv2.resize(frame, (w, h))

    # Exibe a imagem na janela existente
    cv2.imshow(winName, frame)

    # Aguarda o pressionamento de uma tecla ou 30 milisegundos
    k = cv2.waitKey(10)
    if k == ord('q') or k == ord('Q') or k == 27:
        break

# Libera a memória do programa
cam.release()
cv2.destroyAllWindows()

Browser other questions tagged

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