如果读取图像的图像出现坏图,无法读取的怎么办?怎么自动跳过坏的图像,而读取下一张?

  最近处理无标签图像,但是,有的图像下载下来后,有的是坏的,matlab imread 函数无法读取,而且会报错,然后自动终止程序的执行;

  怎么破?

用 try catch 机制来解决这个问题:

path = "/home/wangxiao/imagefiles/" ;

files = dir(path, '*.png') ;

for i = 1:size(files, 1)

  disp(['==>> deal with: ', num2str(i), '/', num2str(size(files, 1)), ' waiting ... ']) ;

  try 

    image = imread([path, files(i).name]) ;  % if read the image failed, then jumped into part:  "catch"

  catch 

    i = i + 1; % jumped the current bad images, try to read the next image in the next iteration. 

  end 

  // your operation. 

end 

原文地址:https://www.cnblogs.com/wangxiaocvpr/p/5741560.html