是否在OpenCV中保存灰度视频?

Nit*_*hin 2 python opencv

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

有什么codec我需要使用的吗?

现在我尝试了 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')
Run Code Online (Sandbox Code Playgroud)

小智 7

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

out = cv2.VideoWriter(outfilename, fourcc, fps, (width, height), 0)
Run Code Online (Sandbox Code Playgroud)

  • 也可以在 Linux (Ubuntu) 上工作,通过在末尾添加“零”参数 (2认同)

Sar*_*wal 6

有可能.DIVX正在寻找要写入的 3 通道 BGR 图像,但您只提供单通道图像,因为您正在尝试写入灰度图像

尝试这样做:

frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR)

本质上,这将尝试将您的灰度图像转换为 BGR 图像。虽然您的像素值将保持灰色,但这将更改为frame3 通道图像