如何保存视频Python 3.5

时间:2017-03-22 13:08:50

标签: python-3.x anaconda

我有一个视频256x300 uint8(灰色)。我正在跟踪一个对象并在视频中显示一个矩形。我想保存该视频(显示矩形)。我尝试了一些例子,但它们都是用于RGB视频的。

import cv2
import sys
import subprocess as sp

if __name__ == '__main__' :


# Set up tracker.
# Instead of MIL, you can also use
# BOOSTING, KCF, TLD, MEDIANFLOW or GOTURN

tracker = cv2.Tracker_create("MIL")

# Read video
video = cv2.VideoCapture("videotest.avi")

# Exit if video not opened.
if not video.isOpened():
    print("Could not open video")
    sys.exit()

# Read first frame.
ok, frame = video.read()
if not ok:
    print("Cannot read video file")
    sys.exit()

# Define an initial bounding box
bbox = (90, 10, 30, 30)

# Uncomment the line below to select a different bounding box
# bbox = cv2.selectROI(frame, False)

# Initialize tracker with first frame and bounding box
ok = tracker.init(frame, bbox)

while True:
    # Read a new frame
    ok, frame = video.read()
    if not ok:
        break

    # Update tracker
    ok, bbox = tracker.update(frame)

    # Draw bounding box
    if ok:
        p1 = (int(bbox[0]), int(bbox[1]))
        p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3]))
        cv2.rectangle(frame, p1, p2, (0,0,255))

    # Display result
    cv2.imshow("Tracking", frame)

    # Exit if ESC pressed
    k = cv2.waitKey(1) & 0xff
    if k == 27 : break

video.release()
cv2.destroyAllWindows()

0 个答案:

没有答案