python - (使用 Python 的 OpenCV)使用 VideoCapture(1)/外部网络摄像头时出现不需要的自动旋转行为

标签 python opencv rotation video-capture gopro

我正在尝试使用 OpenCV 将网络摄像头(GoPro 8)的视频显示到计算机上,但我不想要自动旋转功能 - 我的意思是当我从横向握住 GoPro 转向纵向时(例如旋转) 90 度),我希望计算机上显示的图像以横向方式显示旋转 View 。

Image displayed on computer when held on Landscape

Image displayed on computer when held on Portrait

所以上面的两张照片显示了它现在正在做什么,但我希望它看起来像下面这样。 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() 旋转图像,但这不是我想要的。

最佳答案

我认为最好的解决方案是使用 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()

关于python - (使用 Python 的 OpenCV)使用 VideoCapture(1)/外部网络摄像头时出现不需要的自动旋转行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65419336/

相关文章:

python - 如何构造一个适合 numpy 排序的数组?

python - 如何缩短这个二维数组代码?

python - 读取 pandas csv 文件时如何重命名值

c++ - 在图像上应用蒙版以裁剪前景会产生不完整的输出

animation - 四元数的二次插值 (SQUAD)

css - 以相反的方向旋转 div 和文本,同时保持它们对齐?

python - 解码和unicode之间的区别?

python-3.x - 导入 opencv 时出现 Python3 重新链接问题

python - 如何在 OpenCV 的阈值操作中使用循环

javascript - 是否可以通过 JavaScript/jQuery 阻止 iPhone 网站中的 "flipping"?