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
cv2.imread: checking if image is being read
提问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.imread
returns False
if 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) img
is equal to None
(as expected). However, when imread
works, 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.ndarray
cannot be used as a boolean. The problem seems to be that imread
returns numpy.ndarray if success and False
(boolean) otherwise.
即返回的numpy.ndarray
不能用作布尔值。问题似乎是imread
如果成功则返回 numpy.ndarray ,False
否则返回(布尔值)。
My solution so far involves using the type
of 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 img
is None
in 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 None
and False
are notthe same value. However, bool(None)==False
, which is why if None
fails.
请注意,None
并False
是不相同的值。然而,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 bool
value 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.
每种方法的详细实现和基本的图像操作也可以参考文档。