简体   繁体   中英

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'

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)

在此处输入图像描述


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)

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.

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 .

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

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

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"

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.

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.

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)

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

i got the same problem, but in using google colab vr. 3.7.1 and i installed CV 4.6.0.66, but it still doesn't work. Then i have uploade one image that im gonna work with, after that i tried to use this code. gray = cv2.cvtColor(pic, cv2.COLOR_RGB2GRAY)

after... ._.

error: OpenCV(4.6.0) /io/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed)._src.empty() in function 'cvtColor'

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM