【问题标题】:OpenCV imread doesn't workOpenCV imread 不起作用
【发布时间】:2014-04-20 02:38:57
【问题描述】:

我的 C++ 代码中有循环来打开和处理一组图像。其中之一是 http://i.stack.imgur.com/rUJnp.gif

imread 无法打开这个特定的。imread().dataNULL

FWIW,我在 CentOS 并使用最新的 opencv 2.4.8。有没有什么办法解决这一问题?如果不是,用另一个 C++ 图像库打开它并将其转换为 cv::Mat 的最简单方法是什么?

【问题讨论】:

  • 打开此特定图像时遇到的错误是什么?如果有任何与文件系统相关的问题(示例文件权限/未提供文件的完整名称(包括扩展名),没有库可以帮助我们...

标签: c++ opencv


【解决方案1】:

来自OpenCV关于imread的文档

函数imread 从指定文件加载图像并返回。如果无法读取图像(由于缺少文件、权限不正确、格式不受支持或无效),该函数将返回一个空矩阵 (Mat::data==NULL)。目前支持以下文件格式:

    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)

因此,OpenCV 不支持 .gif 格式,因此 imread 返回 NULL

【讨论】:

  • 以下是 OpenCV 3.1.0 支持的新图像格式:*.exr、*.hdr、*.pic、*.webp、*.pxm 和 *.pnm
  • 在这里查看 miguel.martin 的答案,它还提供了一种使用 VideoCapture 读取它的方法。 stackoverflow.com/a/56252616/648298
【解决方案2】:

据我所知,OpenCV 不支持 gif 格式,要直接在程序中读取,需要一些外部库。如http://freeimage.sourceforge.net/

祝你好运

【讨论】:

    【解决方案3】:

    正如其他人所说,cv2.imread 不允许您阅读 GIF。但是,您可以改为使用 cv2.VideoCapture 来阅读 GIF。例如(在 Python 中):

    def read_video(video_path):
        video = cv2.VideoCapture(str(video_path))
        while video.isOpened():
            ok, frame = video.read()
    
            if not ok:
                break
    
            yield frame
        video.release()
    
    # yes, it works with the URL (wow!)
    plane_seats = list(read_video('https://i.stack.imgur.com/rUJnp.gif'))[0]
    animated_gif = list(read_video('https://i.stack.imgur.com/rUJnp.gif'))
    

    将其转换为 C++ 应该很简单。

    【讨论】:

      猜你喜欢
      • 2011-11-17
      • 2014-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-06-19
      • 2012-07-04
      • 1970-01-01
      相关资源
      最近更新 更多