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

Zei*_*rif 8 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
Run Code Online (Sandbox Code Playgroud)

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()
Run Code Online (Sandbox Code Playgroud)

Dan*_*ien 18

“错误:(-215)”表示断言失败。在这种情况下, cv::imshow 断言给定的图像是非空的:https : //github.com/opencv/opencv/blob/b0209ad7f742ecc22de2944cd12c2c9fed036f2f/modules/highgui/src/window.cpp#L281

图像入门OpenCV Python 教程中所述,如果该文件不存在,则 cv2.imread() 将返回None;它不会引发异常。

因此,以下代码也会导致“(-215) size.width>0 && size.height>0”错误:

img = cv2.imread('no-such-file.jpg', 0)
cv2.imshow('image', img)
Run Code Online (Sandbox Code Playgroud)

检查以确保该文件确实存在于指定的路径中。如果是,则可能是图像已损坏,或者是空图像。


小智 9

此错误显示时,

  1. 图片路径不对。
  2. 图片名称有误。

我们可以通过print(img)在“ img = cv2.imread('C:\\Utilisateurs\\Zeineb\\Bureau\\image.jpg',0)”之后使用“ ”命令来检查它,如果输出为“无”,则图像的路径或名称是错误的。如果输出是矩阵,则图像读取成功。
在代码 ' cv2.waitKey(0)' 中,零表示图像将出现在屏幕上的毫秒数,因此它应该大于零,例如 5000。