问题描述
我正在尝试使用 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()