Assertion failure : size.width>0 && size.height>0 in function imshow

前端 未结 10 1131
失恋的感觉
失恋的感觉 2020-11-30 12:19

i am using opencv2 and python on raspberry pi. and i am new with python and opencv. i tried to read a jpeg image and display image it shows the following error:

<         


        
相关标签:
10条回答
  • 2020-11-30 13:07

    I am also getting a similar error, so instead of opening a new question, I thought maybe it would be a good idea to gather it all here since there's already some helpful answers...

    My code (textbook code to open a video using OpenCV in Python):

    import cv2 as cv
    import os
    
    path = 'C:/Users/username/Google Drive/Master/THESIS/uva_nemo_db/videos/'
    os.chdir(path)
    video_file = '001_deliberate_smile_2.mp4'
    cap = cv.VideoCapture(video_file) 
    
    
    if not cap.isOpened():
        print("Error opening Video File.")
    
    while True:
        # Capture frame-by-frame
        ret, frame = cap.read()
        cv.imshow('frame',frame)
    
        if cv.waitKey(1) & 0xFF == ord('q'):
            break
    
        # if frame is read correctly, ret is True
        if not ret:
            print("Can't retrieve frame - stream may have ended. Exiting..")
            break
    
    # When everything done, release the capture
    cap.release()
    cv.destroyAllWindows()
    

    The reason why I am dumbfounded is that I am getting the same error - BUT - the video is actually played... When running the code the Python interpreter opens up an instance of Python running the video. Once the video ends, it breaks out of the loop, closes the video and throws the error:

    Traceback (most recent call last): File "C:/Users/username/Documents/smile-main/video-testing.py", line 24, in cv.imshow('frame',frame) cv2.error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-wwma2wne\opencv\modules\highgui\src\window.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'

    I'd appreciate any input!

    **

    EDIT: How I fixed my error!

    **

    I encased my code in a try/except like this:

    # Import required libraries
    import cv2 as cv
    import os
    
    path = 'C:/Users/username/Google Drive/Master/THESIS/uva_nemo_db/videos/'
    # test_path = 'C:/Users/username/Downloads'
    os.chdir(path)
    os.getcwd()
    video_file = '001_deliberate_smile_2.mp4'
    
    cap = cv.VideoCapture(video_file) #cap for "Video Capture Object"
       
    
    if not cap.isOpened():
        print("Error opening Video File.")
    try:
        while True:
            # Capture frame-by-frame
            ret, frame = cap.read()
            cv.imshow('frame',frame)
    
            if cv.waitKey(1) & 0xFF == ord('q'):
                break
    
            # if frame is read correctly, ret is True
            if not ret:
                print("Can't retrieve frame - stream may have ended. Exiting..")
                break
    except:
        print("Video has ended.")
    
    
    # When everything done, release the capture
    cap.release()
    cv.destroyAllWindows()
    

    I'd still appreciate any input on why this error popped up even though the video played fine, and why the try/except eliminated it.

    Thank you!

    0 讨论(0)
  • 2020-11-30 13:12

    In my case, I had forgotten to change the working directory of my terminal to that of my code+testImage. Hence, it failed to find the image there.

    Finally, this is what worked for me:

    I saved the image and Python file on Desktop. I changed my cmd directory to it,

    cd Desktop
    

    And then checked for my file:

    ls
    

    And this was my code that worked:

    import cv2
    import numpy as np
    
    im = cv2.imread('unnamed.jpg')
    #Display the image
    cv2.imshow('im',im)
    cv2.waitKey(2000) #Milliseconds
    
    0 讨论(0)
  • 2020-11-30 13:14

    One of the reasons, this error is caused is when there is no file at the path specified. So, a good practice will be to verify the path like this ( If you are on a linux based machine ):

    ls <path-provided-in-imread-function>
    

    You will get an error if the path is incorrect or the file is missing.

    0 讨论(0)
  • 2020-11-30 13:18

    The image fails to load (probably because you forgot the leading / in the path). imread then returns None. Passing None to imshow causes it to try to create a window of size 0x0, which fails.

    The poor error handling in cv probably owes to its quite thin wrapper layer on the C++ implementation (where returning NULL on error is a common practice).

    0 讨论(0)
提交回复
热议问题