import processing.video.*;
Capture video;
BlobDetector// blobDetector;the color blobColor;to track
color voidtrackColor;
setup()
{// a dimensional size(640, 480);
video = new Capture(this, width, height, 15);
video.start();
blobDetector = new BlobDetector();
blobColor = color(255, 0, 0);
smooth();
}
void drawarray to store marked pixels
boolean marks[][];
// the total marked pixels
int total = 0;
// the most top left pixel
PVector topLeft;
// the most bottom right pixel
PVector bottomRight;
void setup() {
if (video.available()) {size(640, 480);
// start video.read();
}
capture
video = new Capture(this, width, height, 15);
video.loadPixelsstart();
// forset blackinitial andtrack whitecolor imageto red
// video.filter(THRESHOLD,0.1);
image(video trackColor = color(255, 0, 0);
// initialize blobDetector.findBlob(blobColor, 20);marks array
marks blobDetector.drawBlob();
blobDetector.drawBoundingBox();
blobDetector.drawCenterOfMass()= new boolean[width][height];
}
void mousePresseddraw() {
int// locread =video mouseXframe + mouseY*video.width;if available
blobColorif = (video.pixels[loc];
}available()) {
class BlobDetector { video.read();
}
int
blobPoints[][]; // intdraw blobWeightvideo =image
0; PVector centerOfMassimage(video, 0, 0);
BlobDetector() {
blobPoints = new int[width][height];
centerOfMass = new PVector(0, 0// find track color with treshold
findBlob(20);
} // load canvas voidpixels
findBlob(color blobColor, int threshold) { loadPixels();
// blobWeight = 0;
draw blob
for (int x = 0; x < width; x ++ ) {
for (int y = 0; y < height; y ++ ) {
// get pixel location
int loc = x + y*width;
// colormake currentColorpixel = video.pixels[loc];
red if marked
PVector currColorVec = new PVector(red(currentColor), green(currentColor), blue(currentColor));if (marks[x][y]) {
PVector trackColorVecpixels[loc] = new PVector(red(blobColor), green(blobColor), blue(blobColor)color(255, 0, 0);
}
float diff = currColorVec.dist(trackColorVec); }
}
// set canvas ifpixels
(diff < thresholdupdatePixels();
{
// draw bounding box
stroke(255, blobPoints[x][y] = 10, 0);
noFill();
rect(topLeft.x, topLeft.y, bottomRight.x-topLeft.x, bottomRight.y-topLeft.y);
}
void blobWeight++;mousePressed() {
// save current pixel under mouse }as elsetrack {color
int loc = mouseX + mouseY*video.width;
blobPoints[x][y]trackColor = 0video.pixels[loc];
}
void findBlob(int threshold) {
}// reset total
total = }0;
// }prepare point trackers
int lowestX = width;
text(blobWeight, 20, 20);
}
void drawBlob() {
if (blobWeight > 200) {
for (int x = 0; x < width; x ++ ) {
int lowestY = height;
int highestX = 0;
int highestY = 0;
// prepare track color vector
PVector trackColorVec = new PVector(red(trackColor), green(trackColor), blue(trackColor));
// go through image pixel by pixel
for (int yx = 0; yx < heightwidth; yx ++ ) {
for (int y = 0; y < if (blobPoints[x][y] == 1height; y ++ ) {
// get pixel location
strokeWeight(5); int loc = x + y*width;
stroke(255, 0, 0); // get color of pixel
point(x, y);
color currentColor = video.pixels[loc];
// }get vector of pixel color
} PVector currColorVec = new }
PVector(red(currentColor), green(currentColor), blue(currentColor));
} // }get distance to track voidcolor
drawBoundingBox() { PVectorfloat Adist = new PVector(width, heightcurrColorVec.dist(trackColorVec);
PVector B =
new PVector(0, 0); // forreset (intmark
x = 0; x < width; marks[x ++ ) {][y] = false;
for// (intcheck yif =distance 0;is y < height; y ++ ) {
below threshold
if (blobPoints[x][y] == 1dist < threshold) {
if (y < A.y)
A.y = y;
if (y > B.y)
B.y = y;
if (x < A.x)
A.x = x;
if (x > B.x)
B.x = x;
}
}
}
strokeWeight(1);
noFill();
rect(A.x, A.y, B.x-A.x, B.y-A.y);
text("A "+A.x+","+A.y, A.x, A.y);
text("B "+B.x+","+B.y, B.x, B.y);
}
void drawCenterOfMass() { centerOfMass.set(0.0, 0.0, 0.0);
// mark pixel
for (int x = 0; marks[x][y] <= widthtrue;
x ++ ) { total++;
for (int y = 0; y < height;// yupdate ++point )trackers {
if (blobPoints[x][y] == 1) {
< lowestX) lowestX = x;
if centerOfMass.x +(x > highestX) highestX = x;
if (y < lowestY) centerOfMass.ylowestY += y;
}if (y }
}
> highestY) highestY = y;
}
centerOfMass.x /= blobWeight; }
centerOfMass.y}
/=
blobWeight; // save locations
noStroke(); topLeft = new fill(255PVector(lowestX, lowestY);
bottomRight = new ellipsePVector(centerOfMass.xhighestX, centerOfMass.y, 10, 10highestY);
}
}
|