Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error (-215) size.width>0 && size.height>0 occurred when attempting to display an image using OpenCV

Tags:

python

opencv

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()
like image 934
Zeineb Arif Avatar asked Apr 23 '17 14:04

Zeineb Arif


2 Answers

"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.

like image 130
Daniel Trebbien Avatar answered Oct 07 '22 14:10

Daniel Trebbien


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.

like image 38
Saurabh Kumar Avatar answered Oct 07 '22 12:10

Saurabh Kumar