简体   繁体   中英

Check if an image is valid(corrupted) javaCV

I have a list of directories and sub-directories with images. I am using JavaCV in order to read all images and by using cvShowImage to imshow those images. I am reading the files using listFilesForFolder function. My problem is that some images in those folders are not valid(corrupted). Thus I am getting Opencv exception errors:

OpenCV Error: Null pointer () in cvReleaseImage, file ..\..\..\..\opencv\modules\core\src\array.cpp, line 2983
Exception in thread "main" java.lang.RuntimeException: ..\..\..\..\opencv\modules\core\src\array.cpp:2983: error: (-27)  in function cvReleaseImage
at org.bytedeco.javacpp.opencv_core.cvReleaseImage(Native Method)

How can I check in my code if an image is valid(corrupted) or not in order to continue the for-loop in which I read all my images? I am using the following function in order to read images:

public IplImage openImage(String name) {


    IplImage img = cvLoadImage(name);
    cvShowImage("hello", img);
    cvWaitKey();
    cvReleaseImage(img);
    return img;
}

When I run the program I got issues in cvReleaseImage(img) line.

You have to check if the image is null or not.

public IplImage openImage(String name) {

    IplImage img = cvLoadImage(name);
    if(img != null)
    {
        cvShowImage("hello", img);
        cvWaitKey();
        cvReleaseImage(img);
    }
    return img;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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