OpenCV在有些电脑上无法打开AVI视频文件,cvCreateFileCapture函数返回NULL,原因是缺少解码库。
下载并安装XvidCodec即可解决。
下载地址:http://www.xvidmovies.com/codec/
附上官方Wiki的解决方法,里面提供了更多的信息
http://wiki.opencv.org.cn/index.php/%E8%A7%86%E9%A2%91%E8%AF%BB%E5%86%99%E6%A6%82%E8%BF%B0
代码:
# coding :utf-8
import cv2
cap = cv2.VideoCapture(r'C:\Users\Administrator\Videos\1.AVI') # 文件名及格式
while cap.isOpened():
# capture frame-by-frame
ret, frame = cap.read()
if ret == True:
# our operation on the frame come here
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# display the resulting frame
cv2.imshow('frame', frame)
else:
break
if cv2.waitKey(25) & 0xFF == ord('q'): # 按q键退出
break
# when everything done , release the capture
cap.release()
cv2.destroyAllWindows()