1 回答

TA貢獻1804條經驗 獲得超8個贊
我想要實現(xiàn)的是將正在播放的視頻窗口移動到屏幕上的另一個位置
如果上述陳述是您主要關心的問題,那么請使用FileVideoStream
.
VideoCapture 管道將大部分時間花在讀取和解碼下一幀上。當讀取、解碼并返回下一幀時,OpenCV 應用程序被完全阻止。
這意味著當您移動視頻時,應用程序會被阻止,因為管道無法解碼下一幀。
例如:下面是顯示框架,同時手動拖動窗口。
import cv2
import time
from imutils.video import FileVideoStream
vs = FileVideoStream('result.mp4').start()
time.sleep(0.2)
while True:
? ? frame = vs.read()
? ? cv2.imshow("out", frame)
? ? if cv2.waitKey(25) & 0xFF == ord('q'):
? ? ? ? break
vs.stop()
cv2.destroyAllWindows()
現(xiàn)在,如果我們將代碼與一些變量合并:
import cv2
import time
from imutils.video import FileVideoStream
vs = FileVideoStream('result.mp4').start()
time.sleep(0.2)
count = 0
x_pos = 0
y_pos = 0
a_x = 180
a_y = 180
frames = 60
while True:
? ? frame = vs.read()
? ? scale_percent = 200
? ? width = int(frame.shape[1] * scale_percent / 100)
? ? height = int(frame.shape[0] * scale_percent / 100)
? ? dim = (width, height)
? ? if count in range(0, 55):
? ? ? ? resized = cv2.resize(frame, dim, interpolation=cv2.INTER_AREA)
? ? ? ? cv2.imshow('Frame', resized)
? ? ? ? x_pos = x_pos + int((a_x / frames) * (count - 50))
? ? ? ? y_pos = y_pos + int((a_y / frames) * (count - 50))
? ? ? ? cv2.moveWindow('Frame', x_pos, y_pos)
? ? cv2.imshow("out", frame)
? ? if cv2.waitKey(25) & 0xFF == ord('q'):
? ? ? ? break
vs.stop()
cv2.destroyAllWindows()
您將看到兩個窗口,一個正在顯示,第二個窗口正在從窗口的右側位置移動到左側位置。
添加回答
舉報