PCA in Processing for real time object orientation tracking

  • Download Gist
pca_interactive_object_orientation.pde
Processing
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
import Jama.Matrix;
import pca_transform.*;
import hypermedia.video.*;
 
PCA pca;
 
PVector axis1;
PVector axis2;
PVector mean;
 
OpenCV opencv;
 
PImage img;
 
int imgWidth = 640/4;
int imgHeight = 480/4;
 
PVector centroid;
 
void setup() {
size(640, 480);
opencv = new OpenCV(this);
opencv.capture(imgWidth, imgHeight);
 
centroid = new PVector();
}
 
Matrix toMatrix(PImage img) {
ArrayList<PVector> points = new ArrayList<PVector>();
for (int x = 0; x < img.width; x++) {
for (int y = 0; y < img.height; y++) {
int i = y*img.width + x;
if (brightness(img.pixels[i]) > 0) {
points.add(new PVector(x, y));
}
}
}
 
Matrix result = new Matrix(points.size(), 2);
 
float centerX = 0;
float centerY = 0;
 
for (int i = 0; i < points.size(); i++) {
result.set(i, 0, points.get(i).x);
result.set(i, 1, points.get(i).y);
 
centerX += points.get(i).x;
centerY += points.get(i).y;
}
centerX /= points.size();
centerY /= points.size();
centroid.x = centerX;
centroid.y = centerY;
 
return result;
}
 
 
 
void imageInGrid(PImage img, String message, int row, int col) {
int currX = col*img.width;
int currY = row*img.height;
image(img, currX, currY);
fill(255, 0, 0);
text(message, currX + 5, currY + imgHeight - 5);
}
 
void draw() {
background(125);
 
opencv.read();
opencv.convert(GRAY);
imageInGrid(opencv.image(), "GRAY", 0, 0);
 
opencv.absDiff();
imageInGrid(opencv.image(), "DIFF", 0, 1);
 
opencv.brightness(60);
imageInGrid(opencv.image(), "BRIGHTNESS: 60", 0, 2);
 
opencv.threshold(40);
imageInGrid(opencv.image(), "THRESHOLD: 40", 0, 3);
 
opencv.contrast(120);
imageInGrid(opencv.image(), "CONTRAST: 120", 1, 3);
 
Matrix m = toMatrix(opencv.image());
 
if (m.getRowDimension() > 0) {
pca = new PCA(m);
Matrix eigenVectors = pca.getEigenvectorsMatrix();
 
axis1 = new PVector();
axis2 = new PVector();
if (eigenVectors.getColumnDimension() > 1) {
 
axis1.x = (float)eigenVectors.get(0, 0);
axis1.y = (float)eigenVectors.get(1, 0);
 
axis2.x = (float)eigenVectors.get(0, 1);
axis2.y = (float)eigenVectors.get(1, 1);
 
axis1.mult((float)pca.getEigenvalue(0));
axis2.mult((float)pca.getEigenvalue(1));
}
image(opencv.image(), 0, opencv.image().height, opencv.image().width*3, opencv.image().height*3);
 
 
 
stroke(200);
pushMatrix();
translate(0, imgHeight);
scale(3, 3);
 
translate(centroid.x, centroid.y);
 
stroke(0, 255, 0);
line(0, 0, axis1.x, axis1.y);
stroke(255, 0, 0);
line(0, 0, axis2.x, axis2.y);
 
popMatrix();
fill(0, 255, 0);
text("PCA Object Axes:\nFirst two principle components centered at blob centroid", 10, height - 20);
}
}
 
void keyPressed() {
opencv.remember();
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.