OpenCV imread无法工作

4
我在我的C++代码中有一个循环来打开和处理一组图像。其中之一是sample inputhttp://i.stack.imgur.com/rUJnp.gif
imread在打开这个特定的图像时出现问题。imread().dataNULL
值得一提的是,我使用最新的opencv 2.4.8,在CentOS上运行。有没有办法修复这个问题?如果没有,也许用另一个C++图像库打开它并将其转换为cv::Mat的最简单方法是什么?

你在打开这张特定的图片时遇到了什么错误?如果涉及到文件系统问题(例如文件权限/未提供包括扩展名在内的完整文件名),那么没有任何库能够帮助我们解决。 - Mantosh Kumar
3个回答

16

根据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。 - Greg
请查看此处的miguel.martin答案,它还提供了使用VideoCapture读取的方法。 https://dev59.com/Kn7aa4cB1Zd3GeqPwOQG#56252616 - Eran W

3

2

如其他人所述,cv2.imread无法读取GIF。但是,您可以使用cv2.VideoCapture来读取GIF。例如(在Python中):

Original Answer翻译成:最初的回答

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://istack.dev59.com/rUJnp.gif'))[0]
animated_gif = list(read_video('https://istack.dev59.com/rUJnp.gif'))

将这个转换成C++应该很简单。最初的回答。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接