Skip to content

Instantly share code, notes, and snippets.

@yusuketomoto
Last active August 29, 2015 14:17
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yusuketomoto/eeb4251efcc85d75beeb to your computer and use it in GitHub Desktop.
Save yusuketomoto/eeb4251efcc85d75beeb to your computer and use it in GitHub Desktop.
#include "ofMain.h"
#include "ofxCv.h"
const string path_0 = "prev.tif";
const string path_1 = "cur.tif";
class ofApp : public ofBaseApp
{
ofVideoGrabber video;
ofxCv::FlowFarneback forward;
ofxCv::FlowFarneback backward;
ofImage prev, cur;
ofImage interpolate;
float amt = 0;
public:
void setup()
{
ofSetFrameRate(60);
ofSetVerticalSync(true);
ofBackground(0);
prev.loadImage(path_0);
cur.loadImage(path_1);
ofxCv::imitate(interpolate, cur, CV_8UC3);
{
forward.setPyramidScale(0.5);
forward.setNumLevels(3);
forward.setWindowSize(15);
forward.setNumIterations(3);
forward.setPolyN(5);
forward.setPolySigma(1.2);
}
}
int toIndex(int x, int y, int w, int ch) { return (y*w + x)*ch; }
void update()
{
amt = ofMap(mouseX, 0, ofGetWidth(), 0, 1, true);
forward.calcOpticalFlow(prev, cur);
backward.calcOpticalFlow(cur, prev);
int w = interpolate.width, h = interpolate.height;
unsigned char* pix = interpolate.getPixels();
unsigned char* prev_pix = prev.getPixels();
unsigned char* cur_pix = cur.getPixels();
for (int y=0; y<h; y++) {
for (int x=0; x<w; x++) {
const ofVec2f f = forward.getFlowOffset(x, y) * amt;
const int from_i = toIndex(x, y, w, 3);
const int to_i = toIndex(ofClamp(x+f.x, 0, w-1), ofClamp(y+f.y, 0, h-1), w, 3);
pix[from_i] = prev_pix[to_i];
pix[from_i+1] = prev_pix[to_i+1];
pix[from_i+2] = prev_pix[to_i+2];
}
}
interpolate.update();
}
void draw()
{
prev.draw(0, 0);
backward.draw();
ofTranslate(640, 0);
cur.draw(0, 0);
forward.draw();
interpolate.draw(0, 480);
}
void keyPressed(int key)
{
}
};
//========================================================================
int main( ) {
ofSetupOpenGL(1280,480*2,OF_WINDOW);
ofRunApp(new ofApp());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment