如何解决使用 Python 的 OpenCV使用 VideoCapture(1)/外部网络摄像头时不希望的自动旋转行为
我正在尝试使用 OpenCV 在计算机上显示我的网络摄像头(GoPro 8)视频,但我不想要自动旋转功能——我的意思是当我从握住 GoPro 时从横向转向纵向(比如旋转90 度),我希望在我的计算机上显示的图像以横向显示旋转视图。
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,height))
StackImg = np.hstack([frame,flipped,framerot])
cv2.imshow("ImageStacked",StackImg)
if cv2.waitKey(1) & 0xff == ord('q'):
break
cv2.destroyAllWindows()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。