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

We Are Going To Discuss About cv2.error: OpenCV(4.5.2) .error: (-215:Assertion failed) !_src.empty() in function ‘cv::cvtColor’. So lets Start this Python Article.

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

  1. cv2.error: OpenCV(4.5.2) .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)

  2. cv2.error: OpenCV(4.5.2) .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)

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)

Original Author Shamshirsaz.Navid Of This Content

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.

Original Author noobie Of This Content

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.

Original Author Roshan Wickramaarachchi Of This Content

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)

Original Author jawahar.s Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

ittutorial team

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment