参考:
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()
从相机捕获视频
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.
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()