Python 尝试使用 OpenCV 显示图像时出现错误 (-215) size.width>0 && size.height>0

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43572387/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 23:11:44  来源:igfitidea点击:

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

pythonopencv

提问by Zeineb Arif

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

我正在尝试运行一个从 OpenCV 读取图像的简单程序。但是,我收到此错误:

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()

回答by Daniel Trebbien

"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

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

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

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

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

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

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.

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

回答by veer varun singh

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

确保您提供了正确的图像路径。仅当您提供错误的路径时才会出现此错误。

回答by Saurabh Kumar

This error shows when either,

此错误显示时,

  1. path of the image is wrong.
  2. name of the image is wrong.
  1. 图片路径不对。
  2. 图片名称有误。

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.

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

回答by Roshan

    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

你必须在代码中提到你的图像文件的相同格式(例如:-.jpg/.png/.jpeg/.tif等)并且图像文件的目录必须与源代码相同,我也得到了同样的错误,但我以这种方式纠正

回答by AnandhuVV

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

尝试使用正确的 python PATH 设置重新安装您使用的 IDE。

回答by debasishdeb

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

我认为这取决于图像位置问题的 IDE + 路径

  1. In python IDLE you can directly run file from same folder where picture is there
  1. 在python IDLE中,您可以直接从图片所在的同一文件夹中运行文件
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
  1. 在 IntelliJ IDEA 中,您必须提供图片所在图像的完整路径
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

回答by nathancy

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

当您尝试显示空图像时会发生此错误。图像可能未正确加载,可能是由于图像不存在、文件路径不正确或使用cv2.imread(). 您可以通过打印其形状来检查图像是否正确加载

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)

(400, 391, 3)

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

如果 OpenCV 无法加载图像,它会在尝试打印形状时抛出此错误

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

AttributeError: 'NoneType' 对象没有属性 'shape'

回答by Serhat Yarat

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**

**如果设备的相机访问关闭,此代码会给出错误;类似这样的: cv2.imshow('frame',frame) cv2.error: OpenCV(4.0.0) C:\projects\opencv-python\ opencv\modules\highgui\src\window.cpp:350: 错误: (-215:Assertion failed) size.width>0 && size.height>0 in function 'cv::imshow'

So You should turn ON it**

回答by Abhishek

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.

只需在字符串中添加填充图片的完整目录。

回答by albert ivanov

my problem was the same while i found the endswitch of jpg - it was jpEg. )

当我找到 jpg 的结束开关时,我的问题是一样的 - 它是 jpEg。)