Python cv2.imread:检查图像是否正在被读取

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23628325/
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 03:15:22  来源:igfitidea点击:

cv2.imread: checking if image is being read

pythonopencvnumpy

提问by Яois

I'm writing an OpenCV program in python, and at some point I have something like

我正在用 python 编写 OpenCV 程序,在某些时候我有类似的东西

import cv2
import numpy as np
... 
img = cv2.imread("myImage.jpg")

# do stuff with image here 

The problem is that I have to detect if the image file is being correctly read before continuing. cv2.imreadreturns Falseif not able to open the image, so I think of doing something like:

问题是我必须在继续之前检测图像文件是否被正确读取。如果无法打开图像,则cv2.imread返回False,因此我想执行以下操作:

if (img):
   #continue doing stuff

What happens is that if the image is not opened (e.g. if the file does not exist) imgis equal to None(as expected). However, when imreadworks, the condition, breaks:

发生的情况是,如果图像未打开(例如,如果文件不存在)img等于None(如预期的那样)。但是,当imread工作时,条件会中断:

ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()

i.e. the returned numpy.ndarraycannot be used as a boolean. The problem seems to be that imreadreturns numpy.ndarray if success and False(boolean) otherwise.

即返回的numpy.ndarray不能用作布尔值。问题似乎是imread如果成功则返回 numpy.ndarray ,False否则返回(布尔值)。

My solution so far involves using the typeof the returned value as follows:

到目前为止,我的解决方案涉及使用type返回值的 ,如下所示:

if (type(img) is np.ndarray): 
     #do stuff with image

But I was wondering: isn't there a nicer solution, closer to the initial check if(img): #do stuff?

但我想知道:没有更好的解决方案,更接近初始检查if(img): #do stuff吗?

采纳答案by goncalopp

If you're sure that the value of imgis Nonein your case, you can simply use if not img is None, or, equivalently, if img is not None. You don't need to check the type explicitly.

如果您确定 的值img适用None于您的情况,则可以简单地使用if not img is None,或者等效地使用if img is not None. 您不需要明确检查类型。

Note that Noneand Falseare notthe same value. However, bool(None)==False, which is why if Nonefails.

请注意,NoneFalse相同的值。然而,bool(None)==False这就是if None失败的原因。

The documentation for imread, both for OpenCV 2 and 3, states, however, that a empty matrix should be returned on error. You can check for that using if img.size ==0

imread但是,OpenCV 2 和 3的 文档指出,出现错误时应返回空矩阵。您可以使用if img.size ==0

回答by ZdaR

If you want to write the contents as soon as the image file is being generated then you can use os.path.isfile()which return a boolvalue depending upon the presence of a file in the given directory.

如果您想在生成图像文件后立即写入内容,那么您可以使用os.path.isfile()which 返回bool值,具体取决于给定目录中文件的存在。

import cv2 
import os.path

while not os.path.isfile("myImage.jpg"):
    #ignore if no such file is present.
    pass

img = cv2.imread("myImage.jpg", 0)

cv2.imwrite("result.jpg", img)

You can also refer to docsfor detailed implementation of each method and basic image operations.

每种方法的详细实现和基本的图像操作也可以参考文档