سؤال

I'm loading .jpg files residing in a folder to the workspace and perform some operations on them later. However, on some files, I get a warning about Corrupt JPEG files. I do not need to do an inspection on those files by eye, however, they should be excluded from the process. Hence, I need to be able to determine if a given image is corrupt in the aforementioned way, or not.

Although imread throws out such warning, it does not provide a flag. I want to use a function, similar to this fashion.

Here is the sample code

path=dir('*.jpg');

for i=1:length(dir)

image=imread(dir(i,1).name);
flag=iscorrupt(MException.last);

if (not(flag))
    ...
end
end

function out=iscorrupt(exception)
exception.message, out=isempty(strfind(exception.message,'corrupt'));

However, this partial code won't fulfill the purpose, as it depends on the last exception. Thus, after a corrupt warning is thrown, it will always set True flag. I'd like to know, if it possible to capture the required knowledge using the above approach.

هل كانت مفيدة؟

المحلول 2

I've come up to this. Leaving here for future references.

flag=iscorrupt(lastwarn);

...


function out=iscorrupt(exception)
out=not(isempty(strfind(lower(exception),'corrupt')));
warning('Image is ignored')
return;

نصائح أخرى

I would encapsulate your imread in a try/catch idiom. That way every time an exception is thrown, you can simply continue in the loop without even bothering to read from the corrupt image. Also there are some slight typos when you are calling dir and the way you are accessing the actual names for each of the files. I'll fix that for you though.

Try something like this:

path = dir('*.jpg');

for i = 1 : length(path)
    try 
        im = imread(path(i).name);
        %//Continue your code here
        %...
    catch ME %//Skip to next iteration if corrupt message occurs for image i
        if (~isempty(strfind(ME.message,'corrupt')))
             continue;
        end
    end
 end

Aside: I'm not sure if this will work, but looking at the MATLAB documentation, you can reset the most recent uncaught exception by doing MException.last('reset');. If you did this in each iteration of your loop, would this also solve your problem?

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top