Last active
June 22, 2016 11:23
-
-
Save fuyufjh/ce7a32dcbaf580dc6324426a1d094b04 to your computer and use it in GitHub Desktop.
Decode GIF animaion into images with OpenCV and GIFLIB
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <opencv2/opencv.hpp> | |
#include <gif_lib.h> | |
int main(int ac, char **av) | |
{ | |
int err; | |
GifFileType *f = DGifOpenFileName("test.gif", &err); | |
assert(f != NULL); | |
int ret = DGifSlurp(f); | |
assert(ret == GIF_OK); | |
int width = f->SWidth; | |
int height = f->SHeight; | |
std::cout << "Total Frames: " << f->ImageCount << std::endl; | |
std::cout << "Image Size: " << width << " X " << height << std::endl; | |
std::cout << "Color Resolution: " << f->SColorResolution << std::endl; | |
GifColorType *colorMap = f->SColorMap->Colors; | |
cv::namedWindow("image"); | |
cv::Mat img(height, width, CV_8UC3); | |
// Play for 100 frames ... | |
for (int t = 0; t < 100; t++) { | |
int frameNo = t % f->ImageCount; | |
std::cout << frameNo << " / " << f->ImageCount << std::endl; | |
SavedImage *image = &f->SavedImages[frameNo]; | |
auto *ptr = img.ptr<cv::Vec3b>(); | |
for (int i = 0; i < width * height; i++, ptr++) { | |
const GifColorType &color = colorMap[image->RasterBits[i]]; | |
*ptr = cv::Vec3b(color.Red, color.Green, color.Blue); | |
} | |
cvtColor(img, img, CV_RGB2BGR); | |
imshow("image", img); | |
cv::waitKey(100); | |
} | |
DGifCloseFile(f, &err); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment