'cv2.error: OpenCV(4.5.2) .error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

import cv2 #for image processing
import easygui #to open the filebox
import numpy as np #to store image
import imageio #to read image stored at particular path

import sys
import matplotlib.pyplot as plt
import os
import tkinter as tk
from tkinter import filedialog
from tkinter import *
from PIL import ImageTk, Image



top=tk.Tk()
top.geometry('400x400')
top.title('Cartoonify Your Image !')
top.configure(background='white')
label=Label(top,background='#CDCDCD', font=('calibri',20,'bold'))

def upload():
    ImagePath=easygui.fileopenbox()
    cartoonify(ImagePath)


def cartoonify(ImagePath):
    
    # read the image
    originalmage = cv2.imread(ImagePath)
    
    originalmage = cv2.cvtColor(originalmage, cv2.COLOR_BGR2RGB)
    #print(image)  # image is stored in form of numbers
cv2.error: OpenCV(4.5.2) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-vi271kac\opencv\modules\imgproc\src\color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'


Solution 1:[1]

Check the image address again. This usually happens when the image is not loaded correctly in any way. Try giving the address directly; something like "C:\\test.jpg"

import cv2
im = cv2.imread("WRONG IMAGE ADDRESS.jpg", 1)
im = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY)

enter image description here


Update
You can also get the current folder path of your script and load your image from that.
Imagine your files structure are like this:

--RootProject
  |-img.jpg
  |-script.py

Then you can also do something like this:

script.py

    import cv2
    import sys
    im = cv2.imread(sys.path[0]+"/img.jpg", 1)
    im = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY)

Solution 2:[2]

Try giving the image as a path, and one thing to be careful is about the slashes. Use \\ instead of \. Your path must look like D:\\file\\file1\\file2. To check if it worker print type(cv2.imread(path)). If it prints <class 'numpy.ndarray'>, then you are good to go.

Solution 3:[3]

This may happen if your image file path is wrong, add your working folder then use as below:

image = cv2.imread('eye_face.jpg')
type(image)

then your image type will indicate as numpy.ndarray, if your image file path is wrong then the type will be NoneType.

Solution 4:[4]

This error is {wrong image location}. If suppose your image in another folder means use like this:

img=cv2.imread("../images/car.jpg",1)

Solution 5:[5]

This seems to be the path issue in windows. I changed it to a full path like this and it worked.

filename = "D:\Sandbox\Github\opencv-project\Resources\Photos\cats.jpg"

Solution 6:[6]

I was trying to read images from a folder and having the same trouble. I had a non-image in one of the folders that was the problem.

Solution 7:[7]

I solved the same problem by adding:

data = cv2.imread('path_to_your_image', as_grey =True)

and then try to also add the flags = cv2.DFT_COMPLEX_OUTPUT to this line

dft = cv2.dft(np.float32('your_image'),flags = cv2.DFT_COMPLEX_OUTPUT)

there are many solutions for this issue out there.

Solution 8:[8]

This might be because of your camera issue or the camera driver issue. If you are using any USB camera then cross-check its connection and ensure that no other programs are using the same camera.

If your camera is working then you might put the wrong image path. Verify the image path. Also, try to put a hardcoded path like C:\\image1.png if needed.

import cv2
im = cv2.imread("C:\\image1.png", 1)
im = cv2.cvtColor(im, cv2.COLOR_RGB2GRAY)

Solution 9:[9]

Check if your camera has been disabled in the device manager, or else upadate it.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 noobie
Solution 3 Tomerikoo
Solution 4 bad_coder
Solution 5 Syed Amir Raza
Solution 6 ouflak
Solution 7
Solution 8 Codemaker
Solution 9 Ishaan Apte