-1

I have been struggling for some time with the following lines of code on mac, which send me back the error

error: (-215) scn == 3 || scn == 4 in function ipp_cvtColor"

at the line where I try to write my video out.write(out_frame)

Here is my code:

import numpy as np
import cv2
from scipy import ndimage, misc

dir_vid='/Users/qandre/Pictures/Videos/video_input.mp4'
cap = cv2.VideoCapture(dir_vid)

fps = cap.get(cv2.CAP_PROP_FPS)
w = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
h = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)

fourcc = cv2.VideoWriter_fourcc('m','p','4','v')
out = cv2.VideoWriter('/Users/qandre/Pictures/Videos/video_output.mp4', fourcc, int(fps), (int(w), int(h)), isColor=False)
if not out :
    print("!!! Failed VideoWriter: invalid parameters")
    sys.exit(1)

while(cap.isOpened()):
    ret, frame = cap.read()

    if ret is True:
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    else:
        continue

    edges = cv2.Canny(gray,50,150)
    out.write(edges)

    # Display the resulting frame
    cv2.imshow('frame',edges)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

out.release()
cap.release()
cv2.destroyAllWindows()

So... What can possibly be wrong?

I must precise that I only get this error when turning the frame in grey scale thanks to the cvtColor() function.

qandre
  • 1
  • 1
  • 1
    Your path to video source is incomplete. Check out this [answer](https://stackoverflow.com/questions/30506126/open-cv-error-215-scn-3-scn-4-in-function-cvtcolor) – shubh diwase Sep 20 '18 at 11:44
  • Possible duplicate of [open cv error: (-215) scn == 3 || scn == 4 in function cvtColor](https://stackoverflow.com/questions/30506126/open-cv-error-215-scn-3-scn-4-in-function-cvtcolor) – Dan Mašek Sep 20 '18 at 12:05
  • SORRY HERE IS MY COMPLETE COMMENT: I know this post but this does not solve the problem unfortunately! I tried to do: dir_vid='/Volumes/Macintosh\ HD/Users/qandre/Pictures/Videos/zelie_test.mp4' (the absoute path of my video) but I get: "WARNING: Couldn't read movie file /Volumes/Macintosh\ HD/Users/qandre/Pictures/Videos/zelie_test.mp4" I must precise that the saving of the video works when I don't it to be turned into black & white and then display the edges with Canny()... Any other idea please..? – qandre Sep 20 '18 at 12:21
  • In fact by setting dir_vid="/Volumes/Macintosh HD/Users/qandre/Pictures/Videos/zelie_test.mp4" the video is correctly read, but this does not fix my problem though... – qandre Sep 20 '18 at 12:33

1 Answers1

0

I found the answer to my problem:

.MP4 was looking for a 3-channel BGR image to write, but I was only providing it a single channel image, since I was trying to write a grayscale image

Trying doing this:

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

converted my greyscale image to BGR image. Whilemy pixel values remain gray, this changes edges to a 3-channel image and thus now it works.

qandre
  • 1
  • 1