Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
tree: 497f841477
Fetching contributors…

Cannot retrieve contributors at this time

file 161 lines (127 sloc) 4.062 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
#include "videoMotionExample.h"

//------------
void videoMotionExample::setup(int width, int height){

color.allocate(width, height);
gray.allocate(width, height);
thresh.allocate(width, height);
prev.allocate(width, height);
motion.allocate(width, height);
threshF.allocate(width, height);

fadeAmnt = 0.66;
threshAmnt = 29;
}


//-----------
//some funky code that theo wrote
//goes through for every vector in the vector field
//and looks at the closest pixel and its neighbours.
//motion is then calculated as the sum of the vectors between
//pixel and its neighbours

//works on the motion history image (gradiant image)
void videoMotionExample::convertMotionHistoryToField(){
int numPixels = motion.width * motion.height;
unsigned char * pixels = motion.getPixels();

unsigned char gradientThresh = 10;
int index = 0;
int yPos = 0;
int yypos = 0;
unsigned char tmpVal = 0;
int pixX, pixY;

ofxVec2f vec;
ofxVec2f tmp;

float xPct = 0;
float yPct = 0;

float xStep = (float)motion.width / NUM_BINS_X;
float yStep = (float)motion.height / NUM_BINS_Y;

//go through for every vector in the vector field
for(int y = 0; y < NUM_BINS_Y; y++){
pixY = yStep * (float)y ;
yPos = pixY * motion.width;

for(int x = 0; x < NUM_BINS_X; x++){
pixX = xStep * (float)x;

index = pixX + yPos;

//if the pixel is close to black we don't want to use for motion information as it is old
if(pixels[index] < gradientThresh){
motionField.field[x][y] = 0;
}else{
tmp = 0;
//go through for the pixels neighbours
for(int yy = -1; yy < 2; yy++){
for(int xx = -1; xx < 2; xx++){
//if we are outside the image - we skip the pixel
if(pixX + xx < 0 || pixX + xx > motion.width || pixY + yy < 0 || pixY + yy > motion.height){
continue;
}else{
tmpVal = pixels[index + xx + (yy*motion.width) ];

//if the neighbour is dark enough to be considered black
if(tmpVal <= gradientThresh)continue;

//otherwise we add the value to our overall vector for the pixel we are looking at
if( pixels[index] < tmpVal ){
tmp.x += xx;
tmp.y += yy;
}else if( pixels[index] > tmpVal ){
tmp.x -= xx;
tmp.y -= yy;
}
}
}
}

//finally update the field
motionField.field[x][y] = tmp;
}
}
}
}

//-----------
void videoMotionExample::update(unsigned char * pixelsIn, int width, int height ){

prev = gray;
color.setFromPixels(pixelsIn, width, height);

//convert the color image to grayscale
gray = color;

//do absolute diff between the prev frame and current frame
//all pixels that are different will show up as non-black
thresh.absDiff(gray, prev);

//threshold to 0 or 255 value
thresh.threshold(threshAmnt);

//convert to float
threshF = thresh;

//fade the motion history image
motion *= fadeAmnt;

//add in the floating point version of the thresholded image
motion += threshF;

//do our motion tracking and blur the results a little
convertMotionHistoryToField();
motionField.blur(0.1);

}

//-----------
//here we add up all the directions from the
//vector field and average them out to an overall
//direction - this will be quite small so you will want
//to scale it up.
ofxVec2f videoMotionExample::getOverallMotionFromField(){

int numVecs = NUM_BINS_X * NUM_BINS_Y;

ofxVec2f avg = 0;
for(int y = 0; y < NUM_BINS_Y; y++){
for(int x = 0; x < NUM_BINS_X; x++){
avg += motionField.field[x][y];
}
}

avg /= numVecs;
return avg;
}

//-----------
void videoMotionExample::draw(float x, float y){
ofSetColor(0xFFFFFF);
gray.draw(x, y);
thresh.draw(x, y + gray.height);
motion.draw(x, y + gray.height + thresh.height);
motionField.draw(x, y, gray.width, gray.height, 4);
motionField.draw(x, y + gray.height + thresh.height, gray.width,gray.height, 10);
}

//-----------
void videoMotionExample::setFadeAmnt(float fadeVal){
fadeAmnt = fadeVal;
}

//-----------
void videoMotionExample::setThreshold(int threshVal){
threshAmnt = (int)threshVal;
}
Something went wrong with that request. Please try again.