(带Python的OpenCV)使用VideoCapture(1)/外部网络摄像头时出现意外的自动旋转行为

2024-09-27 21:28:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图用OpenCV在计算机上显示我的网络摄像头(GoPro 8)视频,但我不想使用自动旋转功能——我的意思是,当我从手持GoPro从横向移动到纵向(比如旋转90度)时,我希望计算机上显示的图像显示横向旋转的视图

Image displayed on computer when held on Landscape

Image displayed on computer when held on Portrait

上面的两张照片显示了is现在的功能,但我希望它看起来像下面的。 Ideal image displayed on computer when held on Portrait

这是我的密码:

video = cv2.VideoCapture(1)
cv2.namedWindow("window", cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty("window",cv2.WND_PROP_FULLSCREEN,cv2.WINDOW_FULLSCREEN)

while(True):
   ret, frame = video.read()
   if ret == True:
      flipped = cv2.flip(frame, 1) #flip frame vertically, I want it flipped for other reasons
      cv2.imshow('window', flipped)
   if cv2.waitKey(1) & 0xFF == ord('q') :
      break
cv2.destroyAllWindows()

是否有任何方法可以忽略外部网络摄像头的方向?我尝试使用cv2.rotate()旋转图像,但这不是我想要的


Tags: 功能网络on计算机windowcv2framecomputer
1条回答
网友
1楼 · 发布于 2024-09-27 21:28:13

我认为最好的解决方案是使用cv2.rotate,这样你就可以得到你想要的输出。顺便说一句,我使用的是Logitech 720p网络摄像头,当我把它放在纵向位置时,它会在不使用任何python函数的情况下为我提供所需的输出,下面是使用cv2.rotate()的输出代码

import cv2
import numpy as np
cap = cv2.VideoCapture (0)

width = 400
height = 350

while True:
    ret, frame = cap.read()
    frame = cv2.resize(frame, (width, height))
    flipped = cv2.flip(frame, 1)
    framerot = cv2.rotate(frame, cv2.ROTATE_90_COUNTERCLOCKWISE)
    framerot = cv2.resize(framerot, (width, height))
    StackImg = np.hstack([frame, flipped, framerot])
    cv2.imshow("ImageStacked", StackImg)
    if cv2.waitKey(1) & 0xff == ord('q'):
        break
cv2.destroyAllWindows()

相关问题 更多 >

    热门问题