简体   繁体   English

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

[英]cv2.imread: checking if image is being read

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 (eg if the file does not exist) img is equal to None (as expected).发生的情况是,如果图像没有打开(例如,如果文件不存在) img等于None (如预期的那样)。 However, when imread works, the condition, breaks:但是,当imread工作时,条件会中断:

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

ie the returned numpy.ndarray cannot be used as a boolean.即返回的numpy.ndarray不能用作布尔值。 The problem seems to be that imread returns numpy.ndarray if success and False (boolean) otherwise.问题似乎是 imread 如果成功imread返回 numpy.ndarray , imread返回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吗?

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 .如果您确定img的值在您的情况下是None ,您可以简单地使用if not img is None ,或者等效地, if img is not None You don't need to check the type explicitly.您不需要明确检查类型。

Note that None and False are not the same value.请注意, NoneFalse不是相同的值。 However, bool(None)==False , which is why if None fails.但是, 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.但是,OpenCV 2 和 3 的imread文档指出,出现错误时应返回空矩阵。 You can check for that using if img.size ==0您可以使用if img.size ==0

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()返回一个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 docs for detailed implementation of each method and basic image operations.每种方法的详细实现和基本的图像操作也可以参考文档

from documentation, we may use从文档中,我们可以使用

retval  =   cv.haveImageReader (filename)

source https://docs.opencv.org/master/d4/da8/group__imgcodecs.htmlhttps://docs.opencv.org/master/d4/da8/group__imgcodecs.html

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

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