简体   繁体   English

使用 cv2.imread() 时出现 Python openCV 错误

[英]Python openCV error while using cv2.imread()

import cv2
import numpy as np

#load color of an image in grayscale
img1 = cv2.imread('Tarun.jpg',0)

cv2.imshow('Hey its me Tarun yellogi',img1)

error: C:\projects\opencv-python\opencv\modules\highgui\src\window.cpp:331: error: (-215) size.width>0 && size.height>0 in function cv::imshow

img1 = cv2.imread('Tarun.jpg',0)

As you know, imread reads in the specified image.如您所知, imread读取指定的图像。 With just the filename, as here, python tries to read the image from the current working directory.仅使用文件名,如下所示,python 尝试从当前工作目录读取图像。 If the file does not exist, no error is reported and img1 will have a value of None .如果该文件不存在,则不会报告错误并且img1的值为None

That this line:这行:

cv2.imshow('Hey its me Tarun yellogi',img1)

returns an error of:返回错误:

error: C:\\projects\\opencv-python\\opencv\\modules\\highgui\\src\\window.cpp:331: error: (-215) size.width>0 && size.height>0 in function cv::imshow

means that the image specified in the imshow command (ie img1 ) has a width and/or a height of 0. What the error message is saying is that the image in function imshow must have a size where the width is greater than zero and the height is greater than zero.表示imshow命令中指定的图像(即img1 )的宽度和/或高度为 0。错误消息是说函数imshow中的图像必须具有宽度大于零的大小,并且高度大于零。

You can check the image size with img1.shape .您可以使用img1.shape检查图像大小。 In this case you will probably get an error message AttributeError: 'NoneType' object has no attribute 'shape' .在这种情况下,您可能会收到错误消息AttributeError: 'NoneType' object has no attribute 'shape' You can further check if img1 is None with img1 == None .您可以进一步检查img1Noneimg1 == None If you get a response of True then you will know that img1 does not contain image data.如果您得到True响应,那么您就会知道img1不包含图像数据。

TL;DR TL; 博士

The file 'Tarun.jpg' was not found thus img1 == None .未找到文件“Tarun.jpg”,因此img1 == None

  • These kind of error occur when the path to the image is wrong当图像路径错误时会发生这种错误
  • Or the image does not exist或者图片不存在
  • Or the image is corrupted或者图片损坏
  • If the image is not in the current directory then you have to mention the absolute path to the image如果图像不在当前目录中,则必须提及图像的绝对路径
  • Use os module to make the path work in any other machine instead of hard-coding the absolute path使用os模块使路径在任何其他机器上工作,而不是硬编码绝对路径
  • But this error message means that imshow() is not getting the right image to display但是这个错误信息意味着imshow()没有得到正确的图像来显示
  • I would check the shape and the values of the image to make sure that the image is not corrupt or image is not None : print(img1.shape) and print(img1)我会检查图像的形状和值以确保图像没有损坏或图像不是Noneprint(img1.shape)print(img1)
  • If the image with the path does not exist then cv2.imread() returns None如果带有路径的图像不存在,则cv2.imread()返回None
  • The error also indicates that the width and heights of the image is zero which is not possible该错误还表明图像的宽度和高度为零,这是不可能的

I think you can try this in reading the pictures in a loop to prevent the pause by Nonetype error!我认为您可以尝试在循环中读取图片以防止因 Nonetype 错误而暂停! It helps me in my case(Some jpg files is Nonetype in my case)它对我有帮助(在我的情况下,一些 jpg 文件是 Nonetype)

if img is None:
        continue

If the image is not in the current directory then you have to mention the absolute path to the image.如果图像不在当前目录中,则必须提及图像的绝对路径。

Use os module to make the path work in any other machine instead of hard-coding the absolute path.使用os模块使路径在任何其他机器上工作,而不是硬编码绝对路径。

But this error message means that imshow() is not getting the right image to display.但是这个错误信息意味着imshow()没有得到正确的图像来显示。 I would check the shape and the values of the image to make sure that the image is not corrupt.我会检查图像的形状和值以确保图像没有损坏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM