Python Forum
python opencv grayscale conversion error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
python opencv grayscale conversion error
#1
Hi, I am running below script. I tried with different video files format, avi/mp4 but the error is at line where each video frame is converted into grayscale. I tried this code by providing images from folder it works but when used video. There is error for grayscale conversion. Please advice.

Error:
nframe_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) cv2.error: OpenCV(4.0.0) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:181: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
import numpy as np
import cv2

path = 'file_example_AVI_480_750kB.avi'

# below code works with OpenCV

cap = cv2.VideoCapture(path)

while(cap.isOpened()):
    ret, frame = cap.read()
    frameNum = frameNum+1;
# to implement gray conversion of frame
    nframe_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)


    #Below code, shows each frame in a moving video
    #if ret == True:
        # Display the resulting frame
    #    cv2.imshow('Frame', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()
Reply
#2
Your code runs well with the below change:

    if ret == True:
        nframe_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Problem with using opencv in python Raunak1023984765 21 1,633 Feb-21-2024, 04:25 PM
Last Post: Pedroski55
  I need to add data types to cython conversion python to c Good_AI_User 1 993 Aug-19-2022, 07:52 AM
Last Post: Gribouillis
  Screen capture opencv - grab error Kalman15 1 1,595 Jan-27-2022, 12:22 PM
Last Post: buran
  Help for the shortest way to install a suitable version of Python, Numpy, and OpenCV. Ezzat 2 2,274 Dec-23-2021, 12:34 PM
Last Post: snippsat
  Converting an RGBA image to Grayscale and Binary Joni_Engr 3 4,620 Aug-23-2021, 11:54 AM
Last Post: Pedroski55
  Python OpenCV window not opening in fullscreen mode Zman350x 0 3,289 Apr-29-2021, 07:54 PM
Last Post: Zman350x
  Time conversion error tester_V 1 2,016 Oct-28-2020, 10:48 PM
Last Post: tester_V
  Python IDE doesn't see opencv-python package on my Jetson Nano sadhaonnisa 1 3,328 Oct-11-2020, 01:04 AM
Last Post: Larz60+
  pip3 install opencv-python fails on 'skbuild' Pedroski55 2 5,701 Sep-15-2020, 11:33 AM
Last Post: snippsat
  Maximas in grayscale image yliats 2 2,013 Aug-13-2020, 01:11 PM
Last Post: yliats

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020