如何从gif中使用giflib提取图像到cv中的cv :: Mat

问题描述:

我试图从gif中使用giflib提取图像,以便将它们绑定到Opencv Mat中。 我目前使用Opencv-2.4.5和giflib-4.1.6-10。如何从gif中使用giflib提取图像到cv中的cv :: Mat

我的问题是,我只能提取提取的第一个图像的gif。 第二个和其他人被划伤,我认为这是一个位元对齐的问题。

继DOC:http://giflib.sourceforge.net/gif_lib.html

SavedImage *SavedImages; /* Image sequence (high-level API) */ 

应该提供一个指针的图像位。

#include <gif_lib.h> 
#include <iostream> 
#include <assert.h> 
#include <string.h> 
#include <stdlib.h> 
#include "opencv2/opencv.hpp" 

using namespace std; 
using namespace cv; 


int main(int ac, char **av){ 
    int *err; 
    GifFileType *f = DGifOpenFileName(av[1]); 
    assert(f != NULL); 
    int ret = DGifSlurp(f); 
    assert(ret == GIF_OK); 
    int width = f->SWidth; 
    int height = f->SHeight; 
    cout << f->ImageCount << endl; 
    cout << width << " : " << height<< endl; 
    cout << f->SColorResolution << endl; 

    // SavedImage *image = &f->SavedImages[0]; Does actually works 
    SavedImage *image = &f->SavedImages[1]; // that compile but the result is a scratched img 


    Mat img = Mat(Size(width, height), CV_8UC1, image->RasterBits); 
    imwrite("test.png", img); 

    DGifCloseFile(f); 
    return 0; 
} 

我不想用ImageMagick来保留一小段代码并保持它“轻”。

感谢您的帮助。

你检查过你的GIF文件是否是交错的吗?如果是这样,则应在将rasterbits存储为位图格式之前考虑它。 同时检查“SavedImages”的顶部,左侧,宽度和高度,每一帧不需要覆盖所有画布,因此您应该只覆盖与最后一帧不同的像素。