python - openCV Error in cvtColor function: Assertion failed (scn == 3 || scn == 4) -
i want load video file, convert grayscale , display it. here code:
import numpy np import cv2 cap = cv2.videocapture('cars.mp4') while(cap.isopened()): # capture frame-by-frame ret, frame = cap.read() #print frame.shape # our operations on frame come here gray = cv2.cvtcolor(frame, cv2.color_bgr2gray) # display resulting frame cv2.imshow('frame',gray) if cv2.waitkey(1) & 0xff == ord('q'): break # when done, release capture cap.release() cv2.destroyallwindows()
the video plays in grayscale till end. freezes , window becomes not responding , following error in terminal:
opencv error: assertion failed (scn == 3 || scn == 4) in cvtcolor, file /home/clive/downloads/opencv/opencv-2.4.9/modules/imgproc/src/color.cpp, line 3737 traceback (most recent call last): file "cap.py", line 13, in <module> gray = cv2.cvtcolor(frame, cv2.color_bgr2gray) cv2.error: /home/clive/downloads/opencv/opencv-2.4.9/modules/imgproc/src/color.cpp:3737: error: (-215) scn == 3 || scn == 4 in function cvtcolor
i uncommented statement print frame.shape. keeps printing 720,1028,3. after video plays till end, freezes , after while closes , returns
print frame.shape attributeerror: 'nonetype' object has no attribute 'shape'
i understand assertion failure massage means i'm trying convert empty image. added check empty image before starting process using if(ret): statement. (any other way of doing it??)
import numpy np import cv2 cap = cv2.videocapture('cars.mp4') while(cap.isopened()): # capture frame-by-frame ret, frame = cap.read() #print frame.shape if(ret): #if cam read successfull # our operations on frame come here gray = cv2.cvtcolor(frame, cv2.color_bgr2gray) # display resulting frame cv2.imshow('frame',gray) if cv2.waitkey(1) & 0xff == ord('q'): break else: break # when done, release capture cap.release() cv2.destroyallwindows()
this time video plays till end , window still freezes , closes after few seconds. time don't error. why window freeze? how can fix that?
the waitkey() part should not depend on validity of frame, move out of condition:
import numpy np import cv2 cap = cv2.videocapture('cars.mp4') while(cap.isopened()): # capture frame-by-frame ret, frame = cap.read() #print frame.shape if(ret): #if cam read successfull # our operations on frame come here gray = cv2.cvtcolor(frame, cv2.color_bgr2gray) # display resulting frame cv2.imshow('frame',gray) else: break # should called always, frame or not. if cv2.waitkey(1) & 0xff == ord('q'): break
Comments
Post a Comment