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

116,011

Solution 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

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

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

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

img=cv2.imread("../images/car.jpg",1)
Share:
116,011
gamer darker
Author by

gamer darker

Updated on July 09, 2022

Comments

  • gamer darker
    gamer darker almost 2 years
    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'
    
  • Eric Aya
    Eric Aya about 3 years
    This is already what the other answer is saying: Try giving the address directly; something like "C:\\test.jpg"
  • Markus Bawidamann
    Markus Bawidamann over 2 years
    I have had a ton of trouble with trying this on Windows, imread just does not work well, no matter what I did. On Linux it works flawlessly, so I figure not many people use this on Windows and therefore there are bugs that never will get fixed.