cv2.error: OpenCV(4.2.0) C:\projects\opencv-python\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'
This error happened because the image didn't load properly.
So you have problem with the previous line cv2.imread my suggestion is :
check if the images exist in the path you give
check the count variable if he have valid numberHello Everyone,
I had run into the same problem a few days back. Here's my perspective on why this error came up and how can it be fixed.
The reason behind the error:-
- ```cv2.imread() or cv2.VideoCapture()``` can't find where the file is and hence gives src.empty() since None is returned instead of image or video
- If you are sure that the path specified is correct, then chances are the file being read is corrupted either completely or in a few bits and pieces.
If you got into this problem when working with an image then here is the fix:-
- Change the image, literally. If you don't want to change it then try ```from PIL import Image and Image.open()``` and see if that works and try to display the image by using matplotlib.
- Changing or reproducing the same image is the best option in my opinion.
If you got into this problem when working with a video then here is the fix:-
- After using ```cv2.VideoCapture('video path')``` Try something like below example.
suppose this was the source
cap = cv2.VideoCapture('data/input.mpg')
Get width and height of the frame of video
width = cap.get(3) # float width height = cap.get(4) # float height
Make a video writer to see if video being taken as input inflict any changes you make
fourcc = cv2.VideoWriter_fourcc(*"MJPG") out_video = cv2.VideoWriter('result/output.avi', fourcc, 20.0, (int(width), int(height)), True)
Then try this
while(cap.isOpened()): # Read each frame where ret is a return boollean value(True or False) ret, frame = cap.read() # if return is true continue because if it isn't then frame is of NoneType in that case you cant work on that frame if ret: # Any preprocessing you want to make on the frame goes here gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # See if preprocessing is working on the frame it should cv2.imshow('frame', gray) # finally write your preprocessed frame into your output video out_video.write(gray) # write the modifies frame into output video # to forcefully stop the running loop and break out, if it doesnt work use ctlr+c if cv2.waitKey(1) & 0xFF == ord('q'): break # break out if frame has return NoneType this means that once script encounters such frame it breaks out # You will get the error otherwise else: break
this will tell you from which frame the video is corrupted and need to be changed
this could be due to an empty frame. Empty frame might be a result of mistake made while creating or editing video in tools #like adobe premier pro or other products alike
You can check you output video it will contains all frame before the error is encountered
I hope this solves your problem and you can better debug your video next time you encounter something similar.
Also in C++:
- Title
- never gonna give you up
- Category
- C++
- Title
- friend function in c++
- Category
- C++
- Title
- how to append to a vector c++
- Category
- C++
- Title
- peak in c++
- Category
- C++
- Title
- declaration vs. definition cpp
- Category
- C++
- Title
- create a 2d array c++
- Category
- C++
- Title
- random number generator c++
- Category
- C++
- Title
- pass by reference c++
- Category
- C++
- Title
- c++ constructor
- Category
- C++
- Title
- cut by delimiter c++
- Category
- C++
- Title
- c++ class constructor
- Category
- C++
- Title
- arrow operator c++
- Category
- C++
- Title
- how to sort vector in c++
- Category
- C++
- Title
- c++ map find
- Category
- C++
- Title
- how to find the size of a character array in c++
- Category
- C++
- Title
- find in vector in c++
- Category
- C++
- Title
- sorting of array in c++
- Category
- C++
- Title
- compile c++ linux
- Category
- C++
- Title
- how to declare a vector in c++
- Category
- C++
- Title
- pbds in c++
- Category
- C++
- Title
- to_string c++
- Category
- C++
- Title
- ceil in c++
- Category
- C++
- Title
- c++ yes no question
- Category
- C++
- Title
- print type cpp
- Category
- C++
- Title
- simple timer arduino blynk library error
- Category
- C++
- Title
- c++ looping through a vector
- Category
- C++
- Title
- c++ program how to let the user choose different game modes
- Category
- C++
- Title
- how to output to console c++
- Category
- C++
- Title
- first prime numbers less than
- Category
- C++
- Title
- COnvert string to char * C++
- Category
- C++
- Title
- how to read and write in a file c++
- Category
- C++
- Title
- how to convert int to string c++
- Category
- C++
- Title
- cpp create lambda with recursion
- Category
- C++
- Title
- c++ calculator program using switch case
- Category
- C++
- Title
- count function c++
- Category
- C++
- Title
- centos7 mlock2
- Category
- C++
- Title
- split 2d array into chunks in c++
- Category
- C++
- Title
- what is order in of preeendence in float, int, char, bool
- Category
- C++
- Title
- min in vector c++
- Category
- C++
- Title
- new c++
- Category
- C++
- Title
- c++ vector iterator
- Category
- C++
- Title
- c++ triple
- Category
- C++
- Title
- how to iterate through a map in c++
- Category
- C++
- Title
- c++ overload operator
- Category
- C++
- Title
- Runtime Error: Runtime ErrorBad memory access (SIGBUS)
- Category
- C++
- Title
- heredar constructor c++
- Category
- C++
- Title
- what is difference between single inverted and double inverted in programming languages
- Category
- C++
- Title
- bfs in C++
- Category
- C++
- Title
- sort function in cpp
- Category
- C++
- Title
- sort a string alphabetically c++
- Category
- C++
- Title
- string substr c++
- Category
- C++
- Title
- built in popcount c++
- Category
- C++
- Title
- 1d fixed length arrays c++
- Category
- C++
- Title
- loop through array c++
- Category
- C++
- Title
- C++ remove element from set
- Category
- C++
- Title
- c++ sort vector of objects by property
- Category
- C++
- Title
- c++ stack
- Category
- C++
- Title
- how to iterate over unordered_map c++
- Category
- C++
- Title
- including cpp header file in c++
- Category
- C++
- Title
- how to create a vector in c++
- Category
- C++
- Title
- modular exponentiation c++
- Category
- C++
- Title
- how to declare a function in c++
- Category
- C++
- Title
- iostream library in cpp
- Category
- C++
- Title
- how the theam are store in database
- Category
- C++
- Title
- how to pass an object by reference in c++
- Category
- C++
- Title
- reverse a linked list using recursion
- Category
- C++
- Title
- c++ evaluate expression
- Category
- C++
- Title
- calling a method on an object c++
- Category
- C++
- Title
- How to read a file in in C++
- Category
- C++
- Title
- constant variables in c++
- Category
- C++
- Title
- sfml default program
- Category
- C++
- Title
- hashmap in c++
- Category
- C++
- Title
- mkdir boost filesystem
- Category
- C++
- Title
- how to use winmain function
- Category
- C++
- Title
- c++ not greater than
- Category
- C++
- Title
- check for bst
- Category
- C++
- Title
- *max_element in c++
- Category
- C++
- Title
- cpp pi from acos
- Category
- C++
- Title
- how to end a c++ program early
- Category
- C++
- Title
- object slicing in c++
- Category
- C++
- Title
- graph using djacency matrix c++
- Category
- C++
- Title
- c++ menu selection with arrow keys
- Category
- C++
- Title
- building native binary with il2cpp unity
- Category
- C++
- Title
- c++ remove item from list
- Category
- C++
- Title
- sort function in c++
- Category
- C++
- Title
- C++ Syntax
- Category
- C++
- Title
- sort function in vector c++ stl
- Category
- C++
- Title
- min coin change problem dp
- Category
- C++
- Title
- generate random double c++
- Category
- C++
- Title
- matrix eigen c++ example
- Category
- C++
- Title
- basic ex of maps in c++
- Category
- C++
- Title
- linkedlist implementation in c++
- Category
- C++
- Title
- c++ remove text file
- Category
- C++
- Title
- pop from between string c++
- Category
- C++
- Title
- convert integer to string c++
- Category
- C++
- Title
- jump to case label c++
- Category
- C++
- Title
- calculate factorial
- Category
- C++
- Title
- power in c++
- Category
- C++
- Title
- user input c++
- Category
- C++
- Title
- c++ class method example
- Category
- C++