Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Save grayscale video in OpenCV?

Tags:

python

opencv

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.

Is there any specific codec i need to use?

Right now i tried with 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')
like image 906
Nithin Avatar asked Apr 26 '18 07:04

Nithin


People also ask

How do I make a color into grayscale using OpenCV?

Step 1: Import OpenCV. Step 2: Read the original image using imread(). Step 3: Convert to grayscale using cv2. cvtcolor() function.

How do I convert RGB to grayscale in OpenCV?

The conversion from a RGB image to gray is done with: cvtColor(src, bwsrc, cv::COLOR_RGB2GRAY); More advanced channel reordering can also be done with cv::mixChannels.


1 Answers

For Windows OS try:

out = cv2.VideoWriter(outfilename, fourcc, fps, (width, height), 0)
like image 157
bujikan Avatar answered Sep 28 '22 04:09

bujikan