0

I am writing a simple program that reads and does some operations with a series of images. One of the input images has some faults (I think it's the result of disconnecting thumb drive during data transfer).

So the only statement involving the input image that does not cause the program to freeze is cv2.imread(). The moment my program reaches any statement that involves the input, it freezes. I tried putting these statements in try-catch blocks (so that error is thrown and this image is skipped) but nothing changes. Is there anything I can do to make my program see the error in the input then ignore it and go to the next image instead of freezing?

1
  • cv2.imread() returns None if it failed. easy to check for that.
    – berak
    May 28, 2015 at 16:53

1 Answer 1

0

As per 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 ).

Therefore, just check for None value, for example:

for fname in fnames:

    img = cv2.imread(fname)
    if img is None:
        continue

    # Do your processing here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.