是否在OpenCV中保存灰度视频? [英] Save grayscale video in OpenCV?

查看:156
本文介绍了是否在OpenCV中保存灰度视频?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将视频保存为.avi格式,但是我一直收到错误"could not demultiplex stream".我主要想保存灰度视频.

I am trying to save a video as .avi format but i keep getting the error "could not demultiplex stream". I primarily want to save grayscale videos.

我需要使用任何特定的codec吗?

Is there any specific codec i need to use?

现在我尝试使用XVID, DIVX

import imutils
import cv2
import numpy as np

interval = 30
outfilename = 'output.avi'
threshold=100.
fps = 10

cap = cv2.VideoCapture("video.mp4")

ret, frame = cap.read()
height, width, nchannels = frame.shape

fourcc = cv2.cv.CV_FOURCC(*'DIVX')
out = cv2.VideoWriter( outfilename,fourcc, fps, (width,height))

ret, frame = cap.read()
frame = imutils.resize(frame, width=500)
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)

while(True):

  frame0 = frame

  ret, frame = cap.read()
  frame = imutils.resize(frame, width=500)
  frame = cv2.cvtColor(frame, cv2.COLOR_RGB2GRAY)

  if not ret:
    deletedcount +=1
    break

  if np.sum( np.absolute(frame-frame0) )/np.size(frame) > threshold:
    out.write(frame)
  else:
    print "Deleted"

  cv2.imshow('Feed - Press "q" to exit',frame)

  key = cv2.waitKey(interval) & 0xFF

  if key == ord('q'):
    print('received key q' )
    break

cap.release()
out.release()
print('Successfully completed')

推荐答案

对于Windows操作系统,请尝试:

For Windows OS try:

out = cv2.VideoWriter(outfilename, fourcc, fps, (width, height), 0)

这篇关于是否在OpenCV中保存灰度视频?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆