Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

list file extensions supported by OpenCV

In OpenCV, I see imread() and VideoCapture() both take a string to a file path of multiple extensions. Is there a way to get a list of extensions that are supported by them? For example, getting a list of "jpg", "png", "mov", "mpg", etc.? I assume this is system dependent and others have needed to query this at runtime.

Furthermore, how is support determined? If have something like the below code and the Mat I get back always seems partially corrupted (I can see a bit of the image). It doesn't seem to change regardless of the frame number I ask for. I can play this video in my video player "totem", but I'm not even sure if totem and OpenCV are even using the same codec for this file.

Mat fromVideo(std::string _videoPath, int frame) {
    VideoCapture capture(_videoPath);

    Mat f;
    for (int i = 0; i < frame; i++) {
        capture >> f;
    }
    return f;
}
like image 540
voodoogiant Avatar asked Oct 02 '22 00:10

voodoogiant


2 Answers

For imread() (more info here):

  • Windows bitmaps - *.bmp, *.dib (always supported)
  • JPEG files - *.jpeg, *.jpg, *.jpe (see the Notes section)
  • JPEG 2000 files - *.jp2 (see the Notes section)
  • Portable Network Graphics - *.png (see the Notes section)
  • Portable image format - *.pbm, *.pgm, *.ppm (always supported)
  • Sun rasters - *.sr, *.ras (always supported)
  • TIFF files - *.tiff, *.tif (see the Notes section)

For VideoCapture():

  • AVI files - *.avi

    It seems that AVI is the only format with decent cross-platform support. See here for more info.

like image 64
herohuyongtao Avatar answered Oct 13 '22 11:10

herohuyongtao


Use the method cv::VideoCapture::isOpened() to make sure that the constructor was successful in initializing the VideoCapture object.

Note that even if it was possible to get a list of supported container formats from OpenCV (AVI, MKV for instance) with their typical filename extensions, you would still need to know the exact list of supported codecs (and even then the exact file you want to open might be corrupted, etc...). So a list of filename extensions is not enough to accurately describe what is internally supported by OpenCV, and the simplest solution at the OpenCV API level is this isOpened() method.

like image 21
user3146587 Avatar answered Oct 13 '22 11:10

user3146587