-2

The part of my code that I am having trouble on is the following.

if __name__ == '__main__':
    for n, image_file in enumerate(os.scandir(image_folder)):
        img = image_file
        fig, ax = plt.subplots(1)
#       mngr = plt.get_current_fig_manager()
#       mngr.window.setGeometry(250, 120, 1280, 1024)
        image = cv2.imread(image_file.path)
        image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

I receive the following error

 image = cv2.cvtColor(image,cv2.COLOR_BGR2RGB)
    cv2.error: OpenCV(3.4.4) C:\projects\opencv-python\opencv\module\imgproc\src\color.cpp:181:error:(-215:Assertion failed) !_src.empty() in function 'cv::cvtColor'

I understand that the imread() may be empty because the images it is trying to read are not correctly saved. I am trying to test this code with 3 images saved as 000001, 000002, 000003... I am confused on why the images that are saved in the folder in C:\imagez are not working. I have tried saving 3 new images and still get the same error. Any advise would be great!

James
  • 127
  • 2
  • 12
  • 2
    You can start with fixing your code - it seems it misses some `'` hence the weird coloring. You also seem to read a _directory_ - not a file ... where you possibly want to read a file .. add its name to the input of cv2.imread - and reads its dokumentation to fix other errors: https://docs.opencv.org/3.0-beta/modules/imgcodecs/doc/reading_and_writing_images.html#Mat%20imread(const%20String&%20filename,%20int%20flags) – Patrick Artner Jan 07 '19 at 16:33
  • 1
    @PatrickArtner my apologies I posted my code from the wrong version, the imread in the code has (image_file.path) inside the () – James Jan 07 '19 at 17:03
  • Most often it is a problem with the provided path/string. Test it this way: create a new image, save it with imwrite to some path/filename and load the same image-path with imread again. This should work and you can test whether the file is created at the path you expected it to be. – Micka Jan 07 '19 at 17:15

1 Answers1

1

If you read the documentation on cv2.imread() you can see that it may fail - in that case it returns None (thats what Mat::data==NULL translates to in python):

The function imread loads an image from the specified file and returns it. If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format), the function returns an empty matrix ( Mat::data==NULL ).

If you feed None as input to cv2.cvtColor(image, cv2.COLOR_BGR2RGB) - it will raise an error on checking the argument (-215:Assertion failed) !_src.empty() in function 'cv::cvtColor' and finding it to be None:

Fix:

if __name__ == '__main__':
    for n, image_file in enumerate(os.scandir(image_folder)):
        fig, ax = plt.subplots(1)
        image = cv2.imread(image_file.path)
        if image:
            image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
        else:
            print("Unable to load image at path {}".format(image_file))

Check the _exact_spelling of your paths that give you these problems - it may be simply a typo in your path/filename.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • Thank you Patrick, it seems after adding the if else statement the error does not appear. I also did as you stated to check for spelling typos as this happened to me before! Thank you very much for the guidance! – James Jan 07 '19 at 17:31
  • ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all() – Jürgen K. Jun 07 '19 at 11:23
  • @JürgenK. ... huh? ... sounds as if you are doing something incorrect with pandas/numpy series/arrays. Please create a new question with an [mre] of your code so SO's community can help you out. See [valueerror-the-truth-value-of-an-array-with-more-than-one-element-is-ambiguous](https://stackoverflow.com/questions/10062954/valueerror-the-truth-value-of-an-array-with-more-than-one-element-is-ambiguous) – Patrick Artner Jun 07 '19 at 11:44