useruser123
useruser123

Reputation: 89

opencv python converting frame to gray and writing to new video doesn't work

I am using OpenCV 3.1.0.4 and Python 3.6 and I am trying to read a video, convert each frame to gray and write it to a new video. This is my code:

capture = cv2.VideoCapture(video_path)
length = int(capture.get(cv2.CAP_PROP_FRAME_COUNT))

size = (
  int(capture.get(cv2.CAP_PROP_FRAME_WIDTH)),
  int(capture.get(cv2.CAP_PROP_FRAME_HEIGHT))
)

codec = cv2.VideoWriter_fourcc(*'DIVX')
output = cv2.VideoWriter('videofile_masked1.avi', codec, 15.0, size)

while(True):
   # Capture frame-by-frame
   ret, frame = capture.read()
   if frame is None:
     break

   frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
   output.write(frame)

capture.release()
cv2.destroyAllWindows()

When I run this code, I get this error: OpenCV Error: Assertion failed (scn == 3 || scn == 4) in ipp_cvtColor

It works fine when I don't convert the frame to gray!

Upvotes: 1

Views: 1690

Answers (1)

John_Sharp1318
John_Sharp1318

Reputation: 1049

Your issue seem simply related to the fact you have keep the last parameter of VideoWriter to its default value.

The constructor you are using have this signature:

VideoWriter (const String &filename, int fourcc, double fps, Size frameSize, bool isColor=true)

Put the last parameter to false should fix your issues.

Upvotes: 1

Related Questions