11

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'

ProMine
  • 53
  • 1
  • 10
gamer darker
  • 111
  • 1
  • 1
  • 3
  • 1
    I don't think this problem is caused by `tkinter`. If so please remove the `tkinter` tag. – TheLizzard May 07 '21 at 19:16
  • Does this answer your question? [(-215:Assertion failed) !\_src.empty() in function 'cv::cvtColor'](https://stackoverflow.com/questions/53926657/215assertion-failed-src-empty-in-function-cvcvtcolor) – Shamshirsaz.Navid May 08 '21 at 11:40

13 Answers13

15

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)
Shamshirsaz.Navid
  • 2,224
  • 3
  • 22
  • 36
  • 1
    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. – Markus Bawidamann Sep 29 '21 at 08:49
3

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.

noobie
  • 41
  • 1
  • 4
2

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.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
1

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

img=cv2.imread("../images/car.jpg",1)
bad_coder
  • 11,289
  • 20
  • 44
  • 72
jawahar.s
  • 11
  • 1
0

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"

Syed Amir Raza
  • 101
  • 1
  • 1
  • 3
    This is already what the other answer is saying: `Try giving the address directly; something like "C:\\test.jpg"` – Eric Aya Jun 10 '21 at 11:48
0

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.

ouflak
  • 2,458
  • 10
  • 44
  • 49
0

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.

Hamid
  • 41
  • 6
0

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)
Codemaker2015
  • 12,190
  • 6
  • 97
  • 81
0

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

  • This does not provide an answer to the question. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation) you will be able to [comment on any post](https://stackoverflow.com/help/privileges/comment); instead, [provide answers that don't require clarification from the asker](https://meta.stackexchange.com/questions/214173/why-do-i-need-50-reputation-to-comment-what-can-i-do-instead). - [From Review](/review/late-answers/31963368) – lemon Jun 09 '22 at 02:25
0

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.

Khawar Islam
  • 2,556
  • 2
  • 34
  • 56
0

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))    
0

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

  • As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 29 '23 at 15:25
-1

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