gamer darker
gamer darker

Reputation: 111

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

Here is my code:

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

When I run these lines of code I get the following error:

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'

Upvotes: 11

Views: 255537

Answers (13)

Gltshhh
Gltshhh

Reputation: 1

this could also be related to folder name with non english letters like ą ę

Upvotes: 0

Samandar Xamidov
Samandar Xamidov

Reputation: 31

Please check the picture "link" again

 import cv2
 from matplotlib import pyplot as plt
 import numpy as np
 import imutils
 import easyocr

 img = cv2.imread('Wrong_link.jpg')
 gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
 plt.imshow(cv2.cvtColor(gray, cv2.COLOR_BGR2RGB))    

Upvotes: 0

Khawar Islam
Khawar Islam

Reputation: 2924

March 2023: Wasted lots of time on solving this error cv2.error: OpenCV(4.5.5) /io/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

Solution: Actually, the image I am reading through OpenCV is not present in the directory after giving the path of the new image error solved.

Upvotes: 0

Shafin Junayed
Shafin Junayed

Reputation: 97

Please check the extension you give to the path. JPEG,PNG or anything else.

Upvotes: 0

Codemaker2015
Codemaker2015

Reputation: 15726

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)

Upvotes: 0

Ishaan Apte
Ishaan Apte

Reputation: 19

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

Upvotes: -1

Hamid
Hamid

Reputation: 41

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.

Upvotes: 0

Shamshirsaz.Navid
Shamshirsaz.Navid

Reputation: 2362

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)

Upvotes: 15

noobie
noobie

Reputation: 41

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.

Upvotes: 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.

Upvotes: 2

Gabriel Miranda
Gabriel Miranda

Reputation: 1

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.

Upvotes: 0

jawahar.s
jawahar.s

Reputation: 11

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

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

Upvotes: 1

Syed Amir Raza
Syed Amir Raza

Reputation: 141

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"

Upvotes: 0

Related Questions