opencv-视频入门

参考:

1、http://docs.opencv.org/3.3.0/  官方文档api

2、http://docs.opencv.org/3.3.0/d6/d00/tutorial_py_root.html 官方英文教程

3、https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_tutorials.html

4、https://github.com/makelove/OpenCV-Python-Tutorial# 进阶教程

5、https://docs.opencv.org/3.3.0/index.html  官方英文教程

6、https://github.com/abidrahmank/OpenCV2-Python-Tutorials

7、https://www.learnopencv.com/

8、http://answers.opencv.org/questions/ OpenCV论坛


注:安装的版本 opencv_python-3.3.0-cp36-cp36m-win_amd64.whl



参考:https://opencv-python-tutroals.readthedocs.io/en/latest/py_tutorials/py_tutorials.html



视频入门

目标

  • Learn to read video, display video and save video.
  • Learn to capture from Camera and display it.
  • You will learn these functions : cv2.VideoCapture()cv2.VideoWriter()

从相机捕获视频

To capture a video, you need to create a  VideoCapture  object.其参数可以是设备索引(device index)或一个视频文件的名称。设备指数只是指定哪个相机。通常一个摄像头将被连接(就我而言)。所以我只是通过设置设备索引0(-1)。您可以选择第二个相机通过1等等。在那之后,你可以捕捉帧。但最后,别忘了释放捕获

import numpy as np
import cv2

# cap = cv2.VideoCapture(0) # 打开摄像头
cap=cv2.VideoCapture('TownCentreXVID.mp4') # 打开视频文件

if cap.isOpened():
    pass
else:
    cap.open()

# cap.get(3) # 返回 frame width  默认为640
# cap.get(4) # 返回frame  height 默认为480
ret = cap.set(3, 320)  # 修改frame width  320
ret = cap.set(4, 240)  # 修改frame height  240

while(True):
    
    # Capture frame-by-frame
    ret, frame = cap.read() # ret TRUE 成功读取,FALSE 读取失败

    # Our operations on the frame come here
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    # Display the resulting frame
    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()  # 释放捕捉
cv2.destroyAllWindows()


从文件播放视频

import numpy as np
import cv2

cap = cv2.VideoCapture('vtest.avi')

while(cap.isOpened()):
    ret, frame = cap.read()

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    cv2.imshow('frame',gray)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()


保存视频

VideoWriter(filename, fourcc, fps, frameSize[, isColor])

FourCC is a 4-byte code used to specify the video codec. The list of available codes can be found infourcc.org. It is platform dependent. Following codecs works fine for me.

  • In Fedora: DIVX, XVID, MJPG, X264, WMV1, WMV2. (XVID is more preferable. MJPG results in high size video. X264 gives very small size video)
  • In Windows: DIVX (More to be tested and added)
  • In OSX : (I don’t have access to OSX. Can some one fill this?)

FourCC code is passed as cv2.VideoWriter_fourcc('M','J','P','G') orcv2.VideoWriter_fourcc(*'MJPG) forMJPG.

Then number of frames per second (fps) and frame size should be passed.
 And last one is  isColor  flag. If it is True, encoder expect color frame, otherwise it works with grayscaleframe.


import numpy as np
import cv2

# cap = cv2.VideoCapture(0)  # 捕捉摄像头
cap=cv2.VideoCapture('TownCentreXVID.mp4') # 打开现有的视频文件

# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:
        frame = cv2.flip(frame,0) # 上下翻转

        # write the flipped frame
        out.write(frame)

        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值