list file extensions supported by OpenCV

2019-02-17 22:42发布

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;
}

3条回答
我欲成王,谁敢阻挡
2楼-- · 2019-02-17 23:11

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.

查看更多
\"骚年 ilove
3楼-- · 2019-02-17 23:19

Just update:

cv::VideoCapture cap("D:\\test.mp4")

works for me.

查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-02-17 23:37

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.

查看更多
登录 后发表回答