Warping video with Openframeworks. Part 2

This is a more simple but less accurate way of doing a warp. I will have to use this one instead of the previous one because looks like there is no way of getting pixels from a texture, or maybe it is too complicate.
(*EDIT*: There is actually a way, as you can see here)

However, this looks simple and clean, so I will implement it into the application and look how it works.

This is a short video running the code. As you can see, if we compare this one with the previous one, this is kind of weird because looks like it does transformations on every point at the same time, instead of doing transformations on the point that we are dragging.

Like always, this is a mix of several code. Code from csugrue.com and Theo Watson

(Download link at the end of the article)

testApp.cpp

 
#include "testApp.h"

//--------------------------------------------------------------
void testApp::setup(){
ofBackground(127,127,127);

camWidth = 320; // try to grab at this size.
camHeight = 240;
xVidPos = 10;
yVidPos = 40;

vidGrabber.setVerbose(true);
vidGrabber.initGrabber(camWidth, camHeight);

// allocate memory
colorImg.allocate(camWidth, camHeight);
grayImage.allocate(camWidth, camHeight);
giWarped.allocate(camWidth, camHeight);

guiIn = ofRectangle(xVidPos, yVidPos, camWidth, camHeight);

srcPositions = new ofPoint[4];
srcPositions[0].set(xVidPos, yVidPos, 0);
srcPositions[1].set(camWidth + xVidPos, yVidPos, 0);
srcPositions[2].set(camWidth + xVidPos, camHeight + yVidPos, 0);
srcPositions[3].set(xVidPos, camHeight + yVidPos, 0);

dstPositions = new ofPoint[4];
dstPositions[0].set(xVidPos, yVidPos, 0);
dstPositions[1].set(camWidth + xVidPos, yVidPos, 0);
dstPositions[2].set(camWidth + xVidPos, camHeight + yVidPos, 0);
dstPositions[3].set(xVidPos, camHeight + yVidPos, 0);

}

//--------------------------------------------------------------
void testApp::update(){
vidGrabber.grabFrame();

if(vidGrabber.isFrameNew()){

colorImg.setFromPixels(vidGrabber.getPixels(), 320,240);
grayImage.setFromColorImage(colorImg);
giWarped = grayImage;

// warpIntoMe(const ofxCvGrayscaleImage& mom, ofPoint src[4], ofPoint dst[4])
// src : changing values
// dst : original and fixed values
giWarped.warpIntoMe(grayImage, srcPositions, dstPositions);
// giWarped.warpPerspective(srcPositions[0], srcPositions[1], srcPositions[2], srcPositions[3]);
}
}

//--------------------------------------------------------------
void testApp::draw(){

// Draw Images
ofSetColor(255, 255, 255, 255);
grayImage.draw(xVidPos,yVidPos);
giWarped.draw( xVidPos, yVidPos + camHeight + 20);

// Draw lines between points
ofSetColor(0x33DD44);
ofNoFill();
ofBeginShape();
for (int j = 0; j < 4; j++){
ofVertex(srcPositions[j].x, srcPositions[j].y);
}
ofEndShape(true);

// Draw points
ofFill();
ofSetColor(0x3344FF);
for (int j = 0; j < 4; j++){
ofCircle(srcPositions[j].x, srcPositions[j].y, 3);
}

}


//--------------------------------------------------------------
void testApp::keyPressed (int key){
}

//--------------------------------------------------------------
void testApp::keyReleased (int key){
}

//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
}

//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){

if (activePointIn > -1){
srcPositions[activePointIn].x = x;
srcPositions[activePointIn].y = y;
}
}

//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){

//this is not the best way
activePointIn = -1;
activePointOut = -1;

float smallestDist = 999999;
float clickRadius = 10;

for (int j = 0; j < 4; j++){
ofPoint inputPt;
inputPt.x = srcPositions[j].x;
inputPt.y = srcPositions[j].y;
inputPt.z = 0;
float len = sqrt( (inputPt.x - x) * (inputPt.x - x) +
(inputPt.y - y) * (inputPt.y - y));
if (len < clickRadius && len < smallestDist){
activePointIn = j;
smallestDist = len;
}
}
}

//--------------------------------------------------------------
void testApp::mouseReleased(){

}

testApp.h

 
#ifndef _TEST_APP
#define _TEST_APP

#define OF_ADDON_USING_OFXOPENCV

#include "ofMain.h"
#include "ofAddons.h"

class testApp : public ofSimpleApp{

public:

void setup();
void update();
void draw();

void keyPressed (int key);
void keyReleased (int key);

void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased();

ofxCvColorImage colorImg;
ofxCvGrayscaleImage grayImage;
ofxCvGrayscaleImage giWarped;

ofRectangle guiIn;

ofVideoGrabber vidGrabber;
int camWidth, camHeight;
int activePointIn;
int xVidPos, yVidPos;

ofPoint * srcPositions;
ofPoint * dstPositions;
};

#endif

Download code