wow! thank you so much for taking the time to tackle this problem. This is extremely helpful.
I am trying to implement your example into my blobtracking sketch:
I do get the NDI input in!, Everything seems to be working, Except somehow the video does not end up being blobtracked.
What am I missing here?
// Daniel Shiffman
// http://codingtra.in
// http://patreon.com/codingtrain
// Code for: https://youtu.be/o1Ob28sF0N8
import processing.video.*;
import spout.*;
PImage pgr, video;
// DECLARE A SPOUT OBJECT
Spout spout;
int blobCounter = 0;
int maxLife = 50;
color trackColor;
float threshold = 40;
float distThreshold = 50;
ArrayList<Blob> blobs = new ArrayList<Blob>();
void setup() {
size(640, 360, P3D);
//String[] cameras = Capture.list();
// printArray(cameras);
video = createImage(width, width, RGB);
spout = new Spout(this);
// 183.0 12.0 83.0
trackColor = color(183, 12, 83);
}
void captureEvent(Capture video) {
video.read();
}
void keyPressed() {
if (key == 'a') {
distThreshold+=5;
} else if (key == 'z') {
distThreshold-=5;
}
if (key == 's') {
threshold+=5;
} else if (key == 'x') {
threshold-=5;
}
}
void draw() {
video = spout.receiveTexture(video);
video.loadPixels();
image(video, 0, 0);
ArrayList<Blob> currentBlobs = new ArrayList<Blob>();
// Begin loop to walk through every pixel
for (int x = 0; x < video.width; x++ ) {
for (int y = 0; y < video.height; y++ ) {
int loc = x + y * video.width;
// What is current color
color currentColor = video.pixels[loc];
float r1 = red(currentColor);
float g1 = green(currentColor);
float b1 = blue(currentColor);
float r2 = red(trackColor);
float g2 = green(trackColor);
float b2 = blue(trackColor);
float d = distSq(r1, g1, b1, r2, g2, b2);
if (d < threshold*threshold) {
boolean found = false;
for (Blob b : currentBlobs) {
if (b.isNear(x, y)) {
b.add(x, y);
found = true;
break;
}
}
if (!found) {
Blob b = new Blob(x, y);
currentBlobs.add(b);
}
}
}
}
for (int i = currentBlobs.size()-1; i >= 0; i--) {
if (currentBlobs.get(i).size() < 500) {
currentBlobs.remove(i);
}
}
// There are no blobs!
if (blobs.isEmpty() && currentBlobs.size() > 0) {
println("Adding blobs!");
for (Blob b : currentBlobs) {
b.id = blobCounter;
blobs.add(b);
blobCounter++;
}
} else if (blobs.size() <= currentBlobs.size()) {
// Match whatever blobs you can match
for (Blob b : blobs) {
float recordD = 1000;
Blob matched = null;
for (Blob cb : currentBlobs) {
PVector centerB = b.getCenter();
PVector centerCB = cb.getCenter();
float d = PVector.dist(centerB, centerCB);
if (d < recordD && !cb.taken) {
recordD = d;
matched = cb;
}
}
matched.taken = true;
b.become(matched);
}
// Whatever is leftover make new blobs
for (Blob b : currentBlobs) {
if (!b.taken) {
b.id = blobCounter;
blobs.add(b);
blobCounter++;
}
}
} else if (blobs.size() > currentBlobs.size()) {
for (Blob b : blobs) {
b.taken = false;
}
// Match whatever blobs you can match
for (Blob cb : currentBlobs) {
float recordD = 1000;
Blob matched = null;
for (Blob b : blobs) {
PVector centerB = b.getCenter();
PVector centerCB = cb.getCenter();
float d = PVector.dist(centerB, centerCB);
if (d < recordD && !b.taken) {
recordD = d;
matched = b;
}
}
if (matched != null) {
matched.taken = true;
// Resetting the lifespan here is no longer necessary since setting `lifespan = maxLife;` in the become() method in Blob.pde
// matched.lifespan = maxLife;
matched.become(cb);
}
}
for (int i = blobs.size() - 1; i >= 0; i--) {
Blob b = blobs.get(i);
if (!b.taken) {
if (b.checkLife()) {
blobs.remove(i);
}
}
}
}
for (Blob b : blobs) {
b.show();
}
textAlign(RIGHT);
fill(0);
//text(currentBlobs.size(), width-10, 40);
//text(blobs.size(), width-10, 80);
textSize(24);
text("color threshold: " + threshold, width-10, 50);
text("distance threshold: " + distThreshold, width-10, 25);
}
float distSq(float x1, float y1, float x2, float y2) {
float d = (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1);
return d;
}
float distSq(float x1, float y1, float z1, float x2, float y2, float z2) {
float d = (x2-x1)*(x2-x1) + (y2-y1)*(y2-y1) +(z2-z1)*(z2-z1);
return d;
}
void mousePressed() {
// Save color where the mouse is clicked in trackColor variable
int loc = mouseX + mouseY*video.width;
trackColor = video.pixels[loc];
println(red(trackColor), green(trackColor), blue(trackColor));
}