Skip to content

Instantly share code, notes, and snippets.

@Luiz-Monad
Last active January 29, 2017 23:17
Show Gist options
  • Save Luiz-Monad/0512db3a543d16810790f2136a7887b8 to your computer and use it in GitHub Desktop.
Save Luiz-Monad/0512db3a543d16810790f2136a7887b8 to your computer and use it in GitHub Desktop.
extract gif
#include "cv.h"
#include "highgui.h"
#include <iostream>
#include <string>
using namespace std;
void help(const string& exec_name) {
cout << exec_name << " src tgt w h" << endl;
}
int main(int argc, char ** argv)
{
string src = "";
string tgt = "";
int w;
int h;
if (argc != 5) return 0;
src = argv[1];
tgt = argv[2];
stringstream(string(argv[3])) >> w;
stringstream(string(argv[4])) >> h;
if (src == "" || tgt == "") {
help(argv[0]);
return 0;
}
auto capture = cvCreateFileCapture(src.c_str());
auto ix = 0;
if (capture == nullptr) return -2;
while (true) {
if (cvGrabFrame(capture) == 0) break;
auto frame = cvRetrieveFrame(capture);
if (frame == nullptr) break;
auto outf = ostringstream();
outf << tgt << "_" << ix << ".png";
auto h2 = w * ((float)frame->height / frame->width);
auto w2 = h * ((float)frame->width / frame->height);
auto s = h2 <= h ? CvSize(w, h2) : CvSize(w2, h);
auto o = cvCreateImage(s, frame->depth, frame->nChannels);
cvResize(frame, o, CV_INTER_AREA);
cvSaveImage(outf.str().c_str(), o);
cvReleaseImage(&o);
ix++;
}
cvReleaseCapture(&capture);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment