12

I am trying to run a simple program that reads an image from OpenCV. However, I am getting this error:

error: ......\modules\highgui\src\window.cpp:281: error: (-215) size.width>0 && size.height>0 in function cv::imshow

Any idea what this error means?

Here is my code:

from matplotlib import pyplot as plt
import numpy as np
import cv2

img = cv2.imread('C:\\Utilisateurs\\Zeineb\\Bureau\\image.jpg',0)
cv2.imshow('image',img)
cv2.waitKey(0)
cv2.destroyAllWindows()
Daniel Trebbien
  • 38,421
  • 18
  • 121
  • 193
Zeineb Arif
  • 181
  • 1
  • 1
  • 3
  • 1
    Welcome to Stack Overflow! Sorry, this site requires that you post your questions in *English* only. Please translate this yourself; someone else translating for you will not help you understand comments and answers, or respond to feedback. – Martijn Pieters Apr 23 '17 at 14:28
  • I got the same error and later realised that I am reading a HTML file type which is stored with jpg extention. – Sandeep Apr 10 '18 at 10:19

11 Answers11

21

"error: (-215)" means that an assertion failed. In this case, cv::imshow asserts that the given image is non-empty: https://github.com/opencv/opencv/blob/b0209ad7f742ecc22de2944cd12c2c9fed036f2f/modules/highgui/src/window.cpp#L281

As noted in the Getting Started with Images OpenCV Python tutorial, if the file does not exist, then cv2.imread() will return None; it does not raise an exception.

Thus, the following code also results in the "(-215) size.width>0 && size.height>0" error:

img = cv2.imread('no-such-file.jpg', 0)
cv2.imshow('image', img)

Check to make sure that the file actually exists at the specified path. If it does, it might be that the image is corrupted, or is an empty image.

Daniel Trebbien
  • 38,421
  • 18
  • 121
  • 193
9

This error shows when either,

  1. path of the image is wrong.
  2. name of the image is wrong.

We can check it by using 'print(img)' command after 'img = cv2.imread('C:\\Utilisateurs\\Zeineb\\Bureau\\image.jpg',0)' if output is 'None' then either path or name of the image is wrong. If output is matrix then image read is successful.
In code 'cv2.waitKey(0)' zero shows the millisecond for which image will appear on the screen so it should be greater than zero something like 5000.

Angel F Syrus
  • 1,984
  • 8
  • 23
  • 43
Saurabh Kumar
  • 91
  • 1
  • 1
4

Make sure you have given the correct path of image. This error comes only when you have given wrong path.

2
    img=cv2.imread('testpaper01-01.png')
    cv2.imshow('image',img)
    cv2.waitKey(0)
    cv2.destroyAllWindows()

You have to mention the same format of your image file in the code(eg:-.jpg/.png/.jpeg/.tif etc.) and the directory of the image file must be same as the source code, I also got the same error but I rectified this way

Roshan
  • 101
  • 1
  • 1
  • 6
  • 1
    Please describe in your answer, what was the problem, and how will this snippet solve it, to help others understand this answer – FZs Nov 05 '19 at 19:37
1
import numpy as np
import cv2

cap = cv2.VideoCapture(0)
while(True):
    # Capture frame-by-frame
    ret,frame = cap.read()
    cv2.rectangle(frame, (100, 100), (200, 200), [255, 0, 0], 2)
    # Display the resulting frame
    cv2.imshow('frame',frame)
    if cv2.waitKey(25) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows() 

**If the camera access for devices is OFF, this code gives an error ;kind of this: cv2.imshow('frame',frame) cv2.error: OpenCV(4.0.0) C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:350: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'

So You should turn ON it**
Serhat
  • 21
  • 3
1

I got the same error on Windows. However, none of answers helps me. I realised that the issue is single backslash (\) instead of double backslashes (\\) on image path. For the difference, see the link.

Problematic scenario:

import cv2
mypath = "D:\temp\temp.png"
img = cv2.imread(mypath, 1)
cv2.imshow('test', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

Traceback (most recent call last):
  File "<input>", line 1, in <module>
cv2.error: OpenCV(4.4.0) C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-2b5g8ysb\opencv\modules\highgui\src\window.cpp:376: error: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'

Fixed scenario:

import cv2
mypath = "D:\\temp\\temp.png"
img = cv2.imread(mypath, 1)
cv2.imshow('test', img)
cv2.waitKey(0)
cv2.destroyAllWindows()

I hope it helps others who have similar experiences.

ozturkib
  • 1,493
  • 16
  • 28
0

I think it depends on the IDE + path of the image location issues

  1. In python IDLE you can directly run file from same folder where picture is there
example 
img = cv2.imread("kang34.jpg", 0)  # direct use 
  1. In IntelliJ IDEA you have to give full path of the image where picture is located
example 
img = cv2.imread("C:\\Users\\DEBASISH\\AppData\\Local\\Programs\\Python\\Python36\\Projects\\kang34.jpg", 0)  # use proper syntax and full path of image location
89f3a1c
  • 1,430
  • 1
  • 14
  • 24
0

This error occurs when you are trying to show an empty image. The image was probably not loaded correctly and could be due to a non-existent image, incorrect file path, or incorrect file extension when using cv2.imread(). You can check if the image was loaded properly by printing out its shape

image = cv2.imread('picture.png')
print(image.shape)

You will get something like this which returns a tuple of the number of rows, columns, and channels

(400, 391, 3)

If OpenCV was unable to load the image, it will throw this error when trying to print the shape

AttributeError: 'NoneType' object has no attribute 'shape'

nathancy
  • 42,661
  • 14
  • 115
  • 137
0

Try Reinstalling the IDE that you use, with the proper python PATH settings.

0

i have the same problem when i compile the code with the inlcude header file and library are not matched. Hope it will help!

Paul Peter
  • 31
  • 3
-1
import numpy as np
import cv2
img=cv2.imread('E:\itsme\Camera\pic.jpg',10)
cv2.imshow('image',img)
cv2.waitkey(0)
cv2.destroyallwindows()

just add fill the full directory of your picture in string.

Sociopath
  • 13,068
  • 19
  • 47
  • 75
Abhishek
  • 1
  • 1