如何处理输入图像中的错误
How to deal with error in input image
我正在编写一个简单的程序来读取一系列图像并对其进行一些操作。其中一张输入图像有一些错误(我认为这是 在数据传输过程中断开拇指驱动器连接的结果)。
所以唯一不会导致程序冻结的涉及输入图像的语句是cv2.imread()
。当我的程序到达任何涉及输入的语句时,它就会冻结。我尝试将这些语句放在 try-catch
块中(以便抛出错误并跳过此图像)但没有任何变化。我能做些什么让我的程序看到输入中的错误然后忽略它并转到下一张图像而不是冻结?
根据OpenCV doc:
The function imread loads an image from the specified file and returns it. If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format), the function returns an empty matrix ( Mat::data==NULL ).
因此,只需检查 None
值,例如:
for fname in fnames:
img = cv2.imread(fname)
if img is None:
continue
# Do your processing here
我正在编写一个简单的程序来读取一系列图像并对其进行一些操作。其中一张输入图像有一些错误(我认为这是 在数据传输过程中断开拇指驱动器连接的结果)。
所以唯一不会导致程序冻结的涉及输入图像的语句是cv2.imread()
。当我的程序到达任何涉及输入的语句时,它就会冻结。我尝试将这些语句放在 try-catch
块中(以便抛出错误并跳过此图像)但没有任何变化。我能做些什么让我的程序看到输入中的错误然后忽略它并转到下一张图像而不是冻结?
根据OpenCV doc:
The function imread loads an image from the specified file and returns it. If the image cannot be read (because of missing file, improper permissions, unsupported or invalid format), the function returns an empty matrix ( Mat::data==NULL ).
因此,只需检查 None
值,例如:
for fname in fnames:
img = cv2.imread(fname)
if img is None:
continue
# Do your processing here